0

I'm building an app with many dynamic paths involved, but Remix only support single dynamic path out of the box. I searched the docs but can't find anything that would allow me to achieve the following.

My Route structure looks like this:

$postType
- $collectionType
- - $id.tsx
- - index.tsx
- $id.tsx
- index.tsx

So it's basically a structure of post and collection types that gives me possible paths like this:

  • /books - renders $postType/index.tsx
  • /books/the-shining - renders $postType/$id.tsx dynamically
  • /books/authors - renders $postType/$collectionType/index.tsx
  • /books/authors/steven-king - renders $postType/$collectionType/$id.tsx dynamically

Unfortunately, out of the box, it takes only the first dynamic path it finds, so /books/the-shining would point to the $postType/$collectionType/index.tsx file.

Is there any way to tell Remix from the loader, that the collection type with that slug was not found and to go to another dynamic path?

Thanks a lot for any help!

1 Answer 1

1

Remix tries to match the longest route. Since /books/the-shining and /books/authors are ambiguous, Remix can only choose the longest route.

You will need to either update your route structure to give Remix a hint:

$postType.c.$collectionType.index.tsx -> /books/c/authors

Or you can use a splat $.tsx route as a catch-all, then dynamically parse the URL to determine which layout/route to render.

export function loader({request, params}: LoaderArgs) {
  const splat = params['*']
  const parts = splat.split('/')
  //...
}

There is an RFC for optional route segments that will first be in React Router and will eventually make it to Remix.

https://github.com/remix-run/react-router/discussions/9550

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.