Retrieving parent slug inside dynamic route
Answered
Eastern yellowjacket posted this in #help-forum
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?
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.9 Replies
Eurasian Curlew
Just so I understand your question, you want to get the
Folder
route slug from the [...slug]
page?Eastern yellowjacketOP
Yes
Eurasian Curlew
Folder
is not a dynamic route segment, it's route slug will never change so you already know the value.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
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
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 arrayEastern yellowjacketOP
Oh okay makes sense thanks 🙏
Eurasian Curlew
No problem