I’m using the pages router in next.js v14.
I’ve set a cookie after a user completes a certain action, and I want this cookie restricted to only 1 page.
I create the cookie using the path “/my-page” and I can see it in my browser as having been set. In my pages, I’m accessing the cookies via getServerSideProps
to get the req.cookies
object.
If this is the first page to load, everything works fine and I have access to the cookie in question. However if I am accessing the page via a navigation, and it is not the first page to load, the cookie is not sent in the headers because the path does not match (according to chrome). This makes some sense as instead of the path being “https://my-site.com/my-page” it is “https://my-site.com/_data/development/my-page.json”. This makes some sense, given how nextjs works. But certainly it seems like there should be a way to access this cookie from a navigation request, in addition to a page load request. Am I missing something here?
I’m using universal-cookie to set the cookie, if that makes a difference. And the cookie is being set client-side, as it’s a result of a user action.
Thanks.