fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Optional/default route param #86

Closed bliitzkrieg closed 7 years ago

bliitzkrieg commented 7 years ago

I am trying to add localization to my nextjs project and I was wondering if there is a way to optionally have a dynamic route for the language. For instance if my user went to /:lang/some-route/:slug it would use the language variable to determine the localization but optionally /some-route/:slug would work as well defaulting the user to my default language. Is this possible using next-routes?

I believe I can create two routes, one for each case but that doesn't seem elegant as I'd have to duplicate every route in my app. Looks like I can't have two duplicate names for a route :/

bliitzkrieg commented 7 years ago

I think I found a solution. If I put a ? on :lang it seems to work as I'd like.

routes.add('page', '/:lang?/page/:slug');

fridays commented 7 years ago

That's the right way! More info on possible route patterns can be found here: https://github.com/pillarjs/path-to-regexp

bliitzkrieg commented 7 years ago

Thanks! I closed the issue

jhvissotto commented 4 years ago

This works for initial load (from URL) but not works when the route is pushed with router.push('/page/article-blabla') resulting in error:

(/page/article-blabla) is incompatible with the href value (/[lang]/page).