-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Dynamic route not working with optional segment #6021
Comments
You should create a reproduction, I did this a few days ago, worked fine for me. |
Here you go: https://codesandbox.io/p/sandbox/weathered-feather-kmfkgb?file=README.md For some reason, I can't bump the version to 1.15, but the behavior appears to be the same. |
Misunderstood the question But the behavior you are getting is normal.
You can't expect the router to know that Also, you are referring to a splat route, but a spat route is something else. |
That's what I would have thought too, but take a look at the doc: My bad for the splat route, I changed to dynamic. |
Ah, didn't see that. Yeah that's just faulty docs afaik. |
I'm not so sure. I think it's a bug in the implementation. My understanding is that optional segments are lazily matched. It will always try to eagerly match the route without the optional segments first. When Remix sees a route with an optional segment, it generates multiple route definitions. Here's what it would look like, sorted by specificity.
|
In which case, index would match then? |
I don't know if That's why you need to be careful about ambiguous routes. |
I am experiencing this issue and would appreciate any help. Is it possible to have a single level dynamic routes with an optional segment? What about custom routing? |
Related to #4857 |
I agree the docs are incorrect in that specific example. This is behaving correctly at the moment because the index route carries more weight. This structure generates a routes tree of the following per <Routes>
<Route file="root.tsx">
<Route path=":lang?" index file="routes/($lang)/index.tsx" />
<Route path=":lang?/:pid" file="routes/($lang)/$pid.tsx" />
</Route>
</Routes> The implementation explodes these ahead of time to represent the routes with and without the optional segment, so the effective routing tree is: <Routes>
<Route file="root.tsx">
<Route path=":lang" index file="routes/($lang)/index.tsx" />
<Route path=":lang/:pid" file="routes/($lang)/$pid.tsx" />
<Route path="" index file="routes/($lang)/index.tsx" />
<Route path=":pid" file="routes/($lang)/$pid.tsx" />
</Route>
</Routes> And then when you only have one segment of the URL, If you have no desire for routes/
($lang)/
$pid.tsx
index.tsx Which will generate a route tree of: <Routes>
<Route file="root.tsx">
<Route path=":lang?/:pid" file="routes/($lang)/$pid.tsx" />
<Route index file="routes/index.tsx" />
</Route>
</Routes> Which explodes to the following which eliminates the ambiguity between <Routes>
<Route file="root.tsx">
<Route path=":lang/:pid" file="routes/($lang)/$pid.tsx" />
<Route path=":pid" file="routes/($lang)/$pid.tsx" />
<Route index file="routes/index.tsx" />
</Route>
</Routes> |
…l segment As outlined in the following issues, this route is not matched as stated in the documentation. Whether this is a bug with remix instead still seems up for discussion. I think this should actually work as currently documented, but it doesn't and this seems known for a while, so this change is just reflecting the current implementation. remix-run#6021 remix-run#6317 remix-run#6426
What version of Remix are you using?
1.15.0
Are all your remix dependencies & dev-dependencies using the same version?
Steps to Reproduce
https://codesandbox.io/p/sandbox/weathered-feather-kmfkgb
Expected Behavior
($lang)/$pid.tsx should match with and without the optional $lang parameter
Actual Behavior
The $pid.tsx route will only be accessible using /something/test
/test for example will not be matched
The text was updated successfully, but these errors were encountered: