I’m working with Next.js 14 and next-intl and trying to set up a routing mechanism where different paths render the same component based on the pathname. My application is multilingual, and each language version has its own URL path but should load the same component. Here are the paths I’m working with:
For the "Hi" component:
- http://localhost:3000/en/hi
- http://localhost:3000/fr/allo
- http://localhost:3000/vn/xin-chao
For the "Bye" component:
- http://localhost:3000/en/seeya
- http://localhost:3000/fr/bye
- http://localhost:3000/vn/tam-biet
For the "About Us" component:
- http://localhost:3000/en/about-us
- http://localhost:3000/fr/a-propos-de-nous
- http://localhost:3000/vn/ve-chung-toi
I’m looking for a way to configure the Next.js router so that these different URLs render the appropriate component without creating a separate page file for each path. Ideally, I’d like to manage this with dynamic routing.
Has anyone implemented something similar or can provide guidance on how to approach this in Next.js? Any help or pointers would be greatly appreciated!
I looked up for async redirects(), but it doesn’t keep the original url…Should I do a catch-all route and do many if else to check the pathname? Which is wierd for me honestly
2
Answers
Finally I found my answer which is the following inspired by @Devon Ray's answer
If you need it to be dynamic you will need to use dynamic params like this:
You can use static generation to build out the pages from a api.
You can also use headers to create rewrites from your middleware.
Edit:
You can create a file under
app/[lang]/[path]/page.js
then in your file you can access the path params like so:
for the static generation you can also add: