javascriptcookiessetcookiecookie-httponlyreact-cookie

how can we clear complete browser cookies by JavaScript or react?


When I work on my local setup or on server, I run into a cookie issue because I have to manually clear browser cookies before seeing new changes. I'm using react for my application.

By Javascript document.cookie I'm not able to access that cookies so can’t delete it.

I need to clear by code so the user can see latest changes at all times. Hard refresh will solve the problem, but that's not a good idea.

I tried unregistering the serviceworker by using serviceworker.unregister(), but it didn't work.

Note: My project will run only from the server, no cookies or cache will be needed.

If anyone has same issue or any suggestion please provide.


Solution

  • React is a JavaScript library. It can't do anything that JavaScript you write yourself can't do.

    document.cookie is the API provided for editing cookies from client-side JavaScript. If the cookie you want to edit does not appear there (typically because it is marked httpOnly or belongs to a different domain) then you can't access it with client-side JS.

    You'll need to find an approach to this which doesn't involve client-side JS.


    Hard refresh will solve the problem, but that's not a good idea.

    This makes it sound like your real problem is caching and whatever you to do delete cookies manually is also deleting your cache for that website. You should adjust your cache headers in development instead of worrying about cookies.