I have this project migrating from AngularJS, so there is some requirements that need to be met. And I am having lots of problems with routing using NextJS
Let me explain the problem. The URL to this app will be something like:
https://some_domain/wafrToken/step1 or https://some_domain/step1.
wafrToken is just a placeholder for a 56 hexadecimal string, like ex:56e6a6d7bacd4db14eadbd7bf4eeee550ec13853c64c946f490aa574
This string varies. The above is just one example, and it’s always different.
So the actual URL might be like https://some_domain/ex:56e6a6d7bacd4db14eadbd7bf4eeee550ec13853c64c946f490aa574/step1
And the step1 is the actual route, that will change depending on where the user "is" on the application, so it can be step1, or step2, or step3, or viewer, well you get the ideia.
And in the other situation the URL might just be like https://some_domain/step1. In this case no wafrToken, just domain and the route.
So I need to have a routing solution in place that allows me to navigate to step1, or step2 whether we have wafrToken in the URL or not.
Dynamic routes doesn’t seem to work because wafrToken is not always available. Dynamic Optional routes also seem to not work because they must be the last part of the URL.
So my question is: how can I achieve this using NextJS App router or Page router?
Thx in advance for your help.
2
Answers
There are a few ways to do this. Here is what I would recommend:
Dynamic routes
You can use dynamic routes a feature built in to both the Next.js pages and app router. Then you can nest these in a similar structure to this
[slug-1]/[slug-2]
.Setup your project like so:
Then setup each page as shown:
See the code sandbox for more info.
This follows the exact plan that you had in mind with what you were doing with Angular.
Query strings
Only in the app router
Personally I think this a more ideal approach it is simpler and produces more dry code. Instead of using a dynamic route for
wafrToken
use a query string. This will follow this url structure:https://some_domain/step1?wafrToken=56e6a6d7bacd4db14eadbd7bf4eeee550ec13853c64c946f490aa574
.Setup your project like so:
Each page as shown:
And the Steps component:
Important: the Steps component must be a client component as it uses a React hook. Learn more.
See the code sandbox for more info.
Further considerations
null
.App router vs pages
In case you were unsure of which to use I thought I would include this bonus section.
The short answer is: almost always use the app router for new codebases. Since you are migrating start on the new app router.
Here is a more in depth comparison:
Credit krishnaacharyaa
As a general rule if you are unsure use the app router.
Conclusion
Circling back to the original scope of the question I would recommend using the query string approach using the app router.
@Ethan answer should be an ideal solution here and its best for you to go with NextJS 14 App router … Use your token as url query string and it should do your job
If you want to do Server side rendering then it is not possible to get URL query parameter in SSR (fetching SearchParams works only in client component read here). But you can still get your ‘query string’ by importing a child component(client) and doing all ‘query string’ related operation in the child component without compromising SSR.