How to get full route access in server components such as layout?
Unanswered
Irish Water Spaniel posted this in #help-forum
Irish Water SpanielOP
Hello everyone!
I am building a NextJS application with the following URL scheme:
The data is stored in an API with an identical structure:
Thus, when a user navigates to
My question is, how do I obtain the [business_id] and [post_id] slugs in the [comment_id] page? The documentation only limits itself by showing how to get the last slug:
But how can I get the others?
I am building a NextJS application with the following URL scheme:
http://website.example.org/businesses/[business_id]/posts/[post_id]/comments/[comment_id]The data is stored in an API with an identical structure:
http://data.example.org/businesses/[business_id]/posts/[post_id]/comments/[comment_id]Thus, when a user navigates to
http://website.example.org/businesses/business_1/posts/post_2/comments/comment_3, the page will need to perform a fetch request to the corresponding API url, and I need to know the slugs for [business_id] and [post_id].My question is, how do I obtain the [business_id] and [post_id] slugs in the [comment_id] page? The documentation only limits itself by showing how to get the last slug:
export default function Page({ params }: { params: { slug: string } }) {
return <div>My Post: {params.slug}</div>
}But how can I get the others?
3 Replies
@Irish Water Spaniel Hello everyone!
I am building a NextJS application with the following URL scheme:
`http://website.example.org/businesses/[business_id]/posts/[post_id]/comments/[comment_id]`
The data is stored in an API with an identical structure:
`http://data.example.org/businesses/[business_id]/posts/[post_id]/comments/[comment_id]`
Thus, when a user navigates to `http://website.example.org/businesses/business_1/posts/post_2/comments/comment_3`, the page will need to perform a fetch request to the corresponding API url, and I need to know the slugs for [business_id] and [post_id].
My question is, how do I obtain the [business_id] and [post_id] slugs in the [comment_id] page? The documentation only limits itself by showing how to get the last slug:
export default function Page({ params }: { params: { slug: string } }) {
return <div>My Post: {params.slug}</div>
}
But how can I get the others?
Firstly that identical structure thing wont work. You cant have page.tsx and route.tsx in the same route.
Secondly in the code you gave you shouldn't be using slug. Its gonna be the name that you are using for the dynamic route.
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#typescript
Secondly in the code you gave you shouldn't be using slug. Its gonna be the name that you are using for the dynamic route.
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#typescript
Check that out. Its basically "destructuring".
@Irish Water Spaniel is your problem solved?