πΊοΈ RFC: Optional Route Segments #9550
Closed
brophdawg11
started this conversation in
Proposals
Replies: 1 comment 1 reply
-
|
When this PR remix-run/remix#3970 lands, it should be relatively straightforward for Remix route config to generate multiple routes to the same file. FYI: |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal
Add support for optional route segments in react-router by using a
?as the last character in a path definition segment. When a path segment ends in a question mark, the entire slash-delimited segment will be considered optional.This solves a few types of use-cases:
/home?to support aliasing/and/home/dashboard/:widget1?/:widget2?/:widget3?to render a dashboard with an optional number of widgets/:lang?/posts/:slugto make the default language optional (alias/posts/spaces-or-tabsand/en/posts/spaces-or-tabs)Roadmap and Related Issues
Background
We'd like to add optional routing segments back into react-router. Thus far, the recommendation has been to manually duplicate your routes:
This works for simple cases, but doesn't scale well to larger cases. Here's two specific examples:
/product/slugand/en/product/slug)Note that in the second example, we even have a use-case for an optional state route segment in
/and/homewanting to use the same route element, thus effectively leading to a desire for/home?.Approach
The general idea for the approach is to allows users to "define" routes with optional params, but then we will automatically explode them into their enumeration of non-optional possibilities. This is effectively doing exactly what we ask users to do manually today. By doing it this way, we ensure that ranking/scoring all still works exactly the same - and it's as-if the user specified all those route paths individually. In the below example, the
/:p1/:p2/:p3path would have the highest score and would take precedence over it's shorter-path siblings.In order to implement this in such a way that it works for non-data-routers (i.e.,
BrowserRouter) and data-routers (RouterProvider), we have to perform this explosion during the current matching algorithm, presumably inside offlattenRoutes.Pros
BrowserRouterandRouterProvideruse the same route matching logicuseMatcheswill still return the defined route with a?in the pathCons
Remix Considerations
This route duplication is also painful in Remix, as shown by the duplication in Jacob's e-commerce example:
https://github.com/jacob-ebey/remix-ecommerce/tree/main/app/routes and https://github.com/jacob-ebey/remix-ecommerce/tree/main/app/routes/%24lang. So we'd want to support this in the conventional routes as well - presumably with just a
?in the file name:app/routes/home?.tsxapp/routes/$lang?.tsxThis will be a fast-follow undertaking to add support for optional route segments to Remix's conventional routes generation - potentially something that makes sense to do along with the Flat Routes. However, once this is implemented in
react-router-domand Remix is leveraging the updated version I think folks would be able to leverage this viadefineRoutes.Beta Was this translation helpful? Give feedback.
All reactions