Next.js Discord

Discord Forum

When using SSG, how do I get the path of the current page being rendered from within getStaticProps?

Answered
Masai Lion posted this in #help-forum
Open in Discord
Avatar
Masai LionOP
I'm struggling to find a way to get the current path being rendered. I don't need the hostname, I just need the relative path.
Answered by joulev
Inside getStaticProps then:
* dynamic params are available in the context: https://nextjs.org/docs/pages/api-reference/functions/get-static-props#context-parameter
* other parts of the pathname can be hardcoded
View full answer

4 Replies

Avatar
joulev
Avatar
Masai LionOP
How do you get access to router from getStaticProps ?
Avatar
joulev
Inside getStaticProps then:
* dynamic params are available in the context: https://nextjs.org/docs/pages/api-reference/functions/get-static-props#context-parameter
* other parts of the pathname can be hardcoded
Answer
Avatar
Masai LionOP
gotcha. After struggling to find another solution, I ended up hardcoding the known parts and using the params as you suggest. Thanks for clarifying that my eventual solution is correct, I appreciate you taking the time to help me out.