getting url value server side
Unanswered
Tonkinese posted this in #help-forum
TonkineseOP
Hey
I need to make use if of a url param (siteSlug) in a server component. How can I get access to this without passing the value from page.tsx/client component as a prop?
I need to make use if of a url param (siteSlug) in a server component. How can I get access to this without passing the value from page.tsx/client component as a prop?
9 Replies
There are many ways you can do this.
- In a middleware.ts you can set a header containing the value of the slug and then access it via a headers instance (more complex)
- Create a headers instance and then get the pathname by running
headers().get("host") and then filter out the origin and get your slug- Also
headers().get("referer") or headers().get("referrer")Look here for extra details.
https://stackoverflow.com/questions/75362636/how-can-i-get-the-url-pathname-on-a-server-component-next-js-13
https://stackoverflow.com/questions/75362636/how-can-i-get-the-url-pathname-on-a-server-component-next-js-13
@Tonkinese
TonkineseOP
Thanks!
No worries, hope this solves your problem.
Original message was deleted
If it does, don't forget this.