Next.js Discord

Discord Forum

Retrieving parent slug inside dynamic route

Answered
Eastern yellowjacket posted this in #help-forum
Open in Discord
Avatar
Eastern yellowjacketOP
Hey,
I have this use case which is:
Folder structure
app/
Layout.tsx
Folder/
Page.tsx
[…slug]/
Page.tsx


My question is how can i retrieve the parent slug from the slug page in SSR ? I was only getting the slug of the dynamic route using params but i couldn’t get the parent with it, is it possible to do so or should i find another approach?
Answered by Eurasian Curlew
Okay, but that doesn't change the fact that the Folder route segment is static, so it's value is the same always.
View full answer

9 Replies

Avatar
Eurasian Curlew
Just so I understand your question, you want to get the Folder route slug from the [...slug] page?
Avatar
Eastern yellowjacketOP
Yes
Avatar
Eurasian Curlew
Folder is not a dynamic route segment, it's route slug will never change so you already know the value.
Avatar
Eastern yellowjacketOP
Yes but my use case is that i have similar slug queries but for different folders and this queries are connected with the folder name, so i want to filter them to check if the slug is related to that folder or not
Avatar
Eurasian Curlew
Okay, but that doesn't change the fact that the Folder route segment is static, so it's value is the same always.
Answer
Avatar
Eurasian Curlew
You are using catch-all dynamic routes so perhaps you are trying to do something like shown here: https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#catch-all-segments
where instead of app/shop/[...slug]/page.js for you it's app/folder/[...slug]/page.js and you can access any of those dynamic parts inside the slug array
Avatar
Eastern yellowjacketOP
Oh okay makes sense thanks 🙏
Avatar
Eurasian Curlew
No problem