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
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
* 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
4 Replies
use [
router.pathname
](https://nextjs.org/docs/pages/api-reference/functions/use-router#router-object)Masai LionOP
How do you get access to
router
from getStaticProps
?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
* 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
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.