Search code examples
next.jscookies

Cookie shown in cookies tab, but disappears after a second [NextJS & cookies-next]


I'm using cookies-next to set a cookie in NextJS, like so (pages router, client side as per docs):

const cookieOptions = {
  expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 399), // 399 days (max cookie age in chrome is 400),
};


setCookie("segmentation", segmentation, cookieOptions);

the cookie is properly set, and works, if navigated around the site with normal next js routing:

enter image description here

however, after manually reloading, or closing the tab and coming back to the site, the cookies is shown for a brief second and then disapears.


Solution

  • Well, the issue was, incorrectly set up cookie compliance script, that on load deleted the cookie (ergo why it only deleted on refresh, and not when client-side routing)

    Not really helpful I guess, but if anyone is dealing with cookie issues, this is your reminder to check if any external scripts aren't messing it up ;)