-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: remix optional segments #4706
Changes from 3 commits
a3d3106
109ad13
af0a5c5
4fa421f
a356be4
85801f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
"@remix-run/dev": minor | ||
--- | ||
|
||
feat: remix optional segments | ||
|
||
Allows for the creation of optional route segments by using parenthesis. For example: | ||
Creating the following file routes in remix `/($lang)/about` | ||
this will match the following routes | ||
``` | ||
/en/about | ||
/fr/about | ||
/about | ||
``` | ||
|
||
helpful for optional language paths. | ||
|
||
Another example `/(one)/($two)/(three).($four)` file routing would match | ||
``` | ||
/ | ||
/one | ||
/one/param1 | ||
/one/param1/three | ||
/one/param1/three/param2 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,7 @@ | |
- lili21 | ||
- lionotm | ||
- liranm | ||
- lordofthecactus | ||
- lpsinger | ||
- lswest | ||
- lucasdibz | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,47 @@ describe("createRoutePath", () => { | |
["[index]", "index"], | ||
["test/inde[x]", "test/index"], | ||
["[i]ndex/[[].[[]]", "index/[/[]"], | ||
|
||
// Optional segment routes | ||
["(routes)/$", "routes?/*"], | ||
["(routes)/($)", "routes?/*?"], // TODO: Fails, do we want to allow this? | ||
["(routes)/(sub)/$", "routes?/sub?/*"], | ||
["(routes).(sub)/$", "routes?/sub?/*"], | ||
["(routes.sub)/$", "routes?/sub?/*"], // TODO: Fails, do we want to allow this? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I don't think we need to add the complexity of being able to interpolate dots inside an optional segment - the line above is how I'd expect optional segments + dot-notation to work together. In this case I'd expect the generated React Router path to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The word "routes" there is confusing to me, so I'm changing it to
No, we don't. The React Router version won't have a way of "grouping" two segments as optional together either: // this is only for "sub"
<Route path="products/all?" />
// this is for both
<Route path="products?/all?" /> So in Remix it should be the same, only individual segments can be optional:
|
||
["(routes)/($slug)", "routes?/:slug?"], | ||
["(routes)/sub/($slug)", "routes?/sub/:slug?"], | ||
["(routes).sub/($slug)", "routes?/sub/:slug?"], | ||
["($)", "*?"], // TODO: Fails, do we want to allow this? | ||
["(nested)/$", "nested?/*"], | ||
["(flat).$", "flat?/*"], | ||
["($slug)", ":slug?"], | ||
["(nested)/($slug)", "nested?/:slug?"], | ||
["(flat).($slug)", "flat?/:slug?"], | ||
["flat.(sub)", "flat/sub?"], | ||
["(nested)/(index)", "nested?"], // Fails with `"flat?/index?"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to do an "optional" index route in react router since // You wouldn't need to do this since `/nested` already renders <Nested /> and
// therefore doesn't need the index route to add it
<Route path="nested" element={<Nested />}>
<Route index element={<Nested />} />
<Route path="child" element={<Child />} />
</Parent/>
// So you would just do and the `Outlet` inside of `<Nested/>` would be a no-op
// when you were at `/nested`
<Route path="nested" element={<Nested />}>
<Route path="child" element={<Child />} />
</Parent/> I haven't ever tried this but I assume if you want an actual There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes,
|
||
["(flat).(index)", "flat?"], // Fails with `"flat?/index?"` | ||
["(index)", undefined], // Fails with `"index?"`" | ||
["(__layout)/(index)", undefined], // Fails with __layout?/index? | ||
["__layout/(test)", "test?"], | ||
["__layout.(test)", "test?"], | ||
["__layout/($slug)", ":slug?"], | ||
["(nested)/__layout/($slug)", "nested?/:slug?"], | ||
["($slug[.]json)", ":slug.json?"], | ||
["(sub)/([sitemap.xml])", "sub?/sitemap.xml?"], | ||
["(sub)/[(sitemap.xml)]", "sub?/sitemap.xml?"], // TODO should this have been sub?/(sitemap.xml) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I think this looks like a bug since the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Working now! |
||
["(posts)/($slug)/([image.jpg])", "posts?/:slug?/image.jpg?"], | ||
[ | ||
"($[$dollabills]).([.]lol)[/](what)/([$]).($)", // TODO outputs ":$dollabills?/.lol?/what?/$?/:?" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is an escaped [
"($[$dollabills]).([.]lol)/(what)/([$]).($up)",
":$dollabills?/.lol?/what?/$?/:up?"
] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added! |
||
":$dollabills/.lol/what/$/*", | ||
], | ||
["(sub).([[])", "sub?/[?"], | ||
["(sub).(])", "sub?/)?"], | ||
["(sub).([([])])", "sub/[]"], // Fails with "sub?/([)]?" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤯 uhhh - I guess the intention here is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems from before there is not a clear handling of multiple brackets within brackets. Either you close on the first match or get the longest within multiple separators? feels like this should be a different PR and discussion. |
||
["(sub).([[])", "sub?/[?"], | ||
["(beef])", "beef]?"], | ||
["([index])", "index?"], | ||
["(test)/(inde[x])", "test?/index?"], | ||
["([i]ndex)/([[]).([[]])", "index?/[?/[]?"], | ||
]; | ||
|
||
for (let [input, expected] of tests) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to support optional splats as they're already optional.
path="child/*"
will match both/child
and/child/any/other/path
: See https://codesandbox.io/s/optional-splat-segments-xpn3kqMaybe we throw an error on
($)
syntax?