Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You cannot use different slug names for the same dynamic path Nextjs

I'm using the default dynamic routing technique of nextjs in my project which is based on the folder structure. I have a route which is:

pages/[language]/location/[location_id]

Now I'm coming across a use case where I need the above route exactly the same except the last parameter of [location_id], I need [route_id] here.

When I created a file [route_id].js for this new route, I'm facing this error:

 throw new Error(`You cannot use different slug names for the same dynamic path ('${previousSlug}' !== '${nextSlug}').`);

I'm aware of the error and the problem about why it's showing me this after doing some research but I'm unable to understand how I can solve this problem. I just want a solution how I can implement these two routes in my app:

Route 1: pages/[language]/location/[location_id]
Route 2: pages/[language]/location/[route_id]

PS: I've already studied this: https://github.com/zeit/next.js/issues/9081

like image 751
Rehan Sattar Avatar asked Mar 06 '20 14:03

Rehan Sattar


People also ask

Which method is used in Nextjs for statically generating dynamic routes?

The companion life-cycle method getStaticPaths of getStaticProps lets us use the data we have at build-time to specify which dynamic routes (like /pages/blog/[slug] ) we want to generate statically.

How does next JS router work?

Next.js has a file-system based router built on the concept of pages. When a file is added to the pages directory, it's automatically available as a route. The files inside the pages directory can be used to define most common patterns.

How do you get a query parameter in Nextjs?

The easiest way to get query parameters from a URL in Next. js is by calling the UseRouter() method. The UseRouter() method gives the developer a router object that has a property called query that contains a dictionary of the query parameters for a given URL. There are 3 scenarios where Next.


1 Answers

The two routes you have provided are exactly equivalent and are not at all different. Therefore, they will be handled by a single page. There is no example of two urls that will route to different nextjs pages for the above two routes.

Anything written in [] will handle any type of value e.g 1 or 2 or any other value for that matter.

You can not make two pages to handle same request because there is no way for next or anyone else for that matter to know whether you are using route_id or location_id because these are both variables that are representing any value.

If you want to differentitate between them, either create a new route with

/route/[route_id] instead of

/location/[location_id], or use queryparams e.g

pages/[language]/location?locationid=[location_id]

And

pages/[language]/location?routeid=[route_id]
like image 179
Hassaan Tauqir Avatar answered Sep 24 '22 17:09

Hassaan Tauqir