Next-routes: Optional/default route param

Created on 21 Aug 2017  路  4Comments  路  Source: fridays/next-routes

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 :/

Most helpful comment

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');

All 4 comments

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');

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

Thanks! I closed the issue

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).

Was this page helpful?
0 / 5 - 0 ratings