redux-persist-cookie-storage icon indicating copy to clipboard operation
redux-persist-cookie-storage copied to clipboard

Not persisting cookies automatically

Open Jahans3 opened this issue 6 years ago • 9 comments

State changes aren't being persisted unless I call persistor.persist().

The initial state is generated, then stored in a cookie, but any changes after that aren't being saved.

Tried with default storage and localForage and it works fine, but I need to use cookies specifically. Does anybody know what could be the cause of this issue? My config and setup seem correct according to the docs, so I am pretty sure this is a bug

Jahans3 avatar Jul 10 '18 14:07 Jahans3

I had the same issue. Probably your cookie is getting set from the server, which will prevent local cookie changes for security reasons.

I solve this by flushing the persistor and then clearing all cookies before sending the response from the server.

await persistor.flush();
res.removeHeader('Set-Cookie');
res.send($.html());

https://github.com/codejamninja/reaction-base/blob/master/src/init/server.js#L71-L73

clayrisser avatar Jul 23 '18 14:07 clayrisser

If your cookie has the http property checked, then it was set from the server and can't be changed locally.

clayrisser avatar Jul 23 '18 14:07 clayrisser

I am having the exact same problem here. Was there ever a proper fix/solution?

ghost avatar Nov 21 '18 20:11 ghost

Not that I'm aware of. Did removing the Set-Cookie header fix it for you?

clayrisser avatar Nov 21 '18 22:11 clayrisser

The problem was we had exceeded the maximum cookie size without realising it. Our solution was to revert back to local storage to persist redux and to sync key data from our state with a cookie

Jahans3 avatar Nov 22 '18 14:11 Jahans3

Ah nice, does it work and is it fixed?

clayrisser avatar Nov 22 '18 14:11 clayrisser

Yeah all works perfectly :) been in production for a few months now

Jahans3 avatar Nov 23 '18 12:11 Jahans3

My issue with the cookie storage was that only one piece of my state was getting correctly persisted, while another was not. I saved this state in separate cookies, so cookie size probably doesn't apply here.

Switching back to session or local storage works perfectly fine for me.

ghost avatar Nov 23 '18 22:11 ghost

@atbmatt sounds like you could be hitting the cookie size limit without knowing or you're using http only cookies?

Jahans3 avatar Dec 07 '18 01:12 Jahans3