oedotme / generouted

Generated file-based routes for Vite
https://stackblitz.com/github.com/oedotme/generouted/tree/main/explorer
MIT License
1.06k stars 50 forks source link

Feature: Add support for optional splats #120

Closed lauri865 closed 1 year ago

lauri865 commented 1 year ago

React-router supports optional splats with "*?" path – would be great to use them with generouted as well a la [[...index]].tsx (nextjs style).

I did get it to work for my purpose with custom routes.ts file, but the types are not correct unfortunately.

oedotme commented 1 year ago

Hey @lauri865, there's a convention already for optional static/dynamic route segments in generouted by adding a minus/dash - sign in front of the file name: /blog/-en.tsx/blog/en?, /blog/-[slug].tsx/blog/:slug?

I've added support for it with splat route segments at v1.15.7, it should be similar: /blog/-[...all].tsx/blog/*?

oedotme commented 1 year ago

The types should also work as expected. Now /blog/-[...all].tsx should resolve to the path type: /blog/${string}?

lauri865 commented 1 year ago

Amazing, and fast – thanks a lot, works perfectly well!

Personally like the nextjs format of double brackets more than dashes in file names (as they're not within the parameters themselves, and makes it tougher to read), but that's a small nitpick.

oedotme commented 1 year ago

Glad to hear that!

That was the first option I thought of, but as the brackets already used for dynamic and splat segments it would be fine. For example [[slug]] and [[...all]], but it would be a problem for optional static segments.

I haven't seen optional static segments to be supported in any file-base routing so far. That's why I chose the - sign (as it can be subtracted/removed from the path) and for it to be the same with the all possibilities: -[slug], -[...all] and -en

Thanks!

lauri865 commented 1 year ago

Out of curiosity, what's an example use case for static optional segments though? I can't think of one personally, but maybe I just haven't come across it. All the use cases I can think of are better served with an optional splat and redirect, rather than optional static segments.

It seems as if optional splats don't work with dot based definition: e.g. test.-[...splat].tsx Ends up with a route path of: test/-*

oedotme commented 1 year ago

For example aliasing, /home? would resolve to / and /home, you can check this React Router discussion https://github.com/remix-run/react-router/discussions/9550

I see, that's because the optional pattern applies per segment. I was planning to update it for a different reason, which would fix this as well.

lauri865 commented 1 year ago

Thanks. Still don't see why it's a good idea though, or what problem it solves. Two static segments showing the same content – if you REALLY need it, you may as well export the same display component from two routes. For the example use case, a redirect to a single route is a better option though than having two paths showing the exact same content anyways.

oedotme commented 1 year ago

We could argue if it's a useful feature or not, but it's an available router feature. I haven't used it before myself, but I thought to consider it in the file-based conventions when it's needed.