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

After purge it stops persisting

Open gastonmorixe opened this issue 6 years ago • 13 comments

After purging there are no more calls to persistence. I call purge when user logs-out, but if they re-login Persist does not work anymore. I need to crash the app or refresh the website to start persisting again.

This is happening on React Native as well as in web.

Will post there if so and try to dig more why this is happening.

gastonmorixe avatar Aug 09 '18 04:08 gastonmorixe

@gastonmorixe You shouldn't be calling purge manually. There's something wrong on how you're setting up this library and that's why it's not working consistently. For me it's working perfectly fine and i'm not purging or persisting nothing by hand. Check: https://github.com/rt2zz/redux-persist/issues/579#issuecomment-412571169 , i uploaded my example source code for someone with similar issue

tommyalvarez avatar Aug 14 '18 15:08 tommyalvarez

@tommyalvarez I am using https://github.com/rematch/rematch persistor plugin which uses redux-persist under the hood. If you see how they encourage to do this in their tests it's exactly as I am doing, calling purge() after log-out. I have little control over their configuration.

gastonmorixe avatar Aug 14 '18 15:08 gastonmorixe

@gastonmorixe in that case i can't help you much and maybe you should open an issue in that github repository. Nonetheless, i eye peaked that framework and seems that redux-persist comes as a plugin, that is documented and should work out of the box. You don't need to purge anything because when you sign out, you "clear" your session/auth whatever you call it reducer state. That "cleanup" of the session reducer state should end up persisted and hence, the user should not be able to visit authenticable views any more.

You should see in your browser's localstorage console tab, that when you access your app for the first time, you have an initial persisted state under the root key you configured in the plugin. That state is the serialized version initial state of each reducer that is whitelisted for persistance. So... if you reset your session reducer state back to its initialState on log out, everything should be ok

tommyalvarez avatar Aug 14 '18 18:08 tommyalvarez

What if I don't want to clear state using a reducer? It's a lot more work to do that instead of just purging the storage.

ghost avatar Aug 29 '18 14:08 ghost

@waynebloss The persistor.purge method uses a reducer under the hood to clear the state (it actually clears the localStorage and on rehydrate it loads the new 'cleared' state depending on how you set up stateReconciler.

I was just pointing out the general intended way of using this library for the particular use case of sign out and proposing a common used solution, which is clearing your store session state and let redux-persist handle the rest by itself. Anyway, it's impossible to figure out why it's not persisting the state anymore when using purge without seeing initialization code example and the localStorage value after purge. We need more info. @gastonmorixe if you be so kind to provide it :)

tommyalvarez avatar Aug 29 '18 16:08 tommyalvarez

I will make a demo soon. Or you might see rematch tests how they purge storage and that’s when it stops working.

gastonmorixe avatar Aug 29 '18 16:08 gastonmorixe

@tommyalvarez Thank you! Much appreciate the explanation.

ghost avatar Aug 30 '18 15:08 ghost

Thanks @tommyalvarez for your explanation above as well. I opted to use purgeStoredState(config) along with setting reducer state back to initialState to clear state as part of logout.

prcbass avatar Sep 19 '18 07:09 prcbass

Any solution ?

marko-ignjatovic avatar Jun 06 '19 18:06 marko-ignjatovic

Works fine for me

persistor.pause()
persistor.flush().then(() => { return persistor.purge() })

and then depends on your app logic resume it by persistor.persist()

e.g in login page ?

xyz1hang avatar Jun 19 '19 10:06 xyz1hang

I've also found that clearing state with purgeStoredState causes ReduxPersist to stop persisting the cleared keys. I prefer not to use the persistor.purge() approach because it leads to import cycles.

EDIT: Actually the issue may be coming from implementing a store reset action as described here: https://stackoverflow.com/a/64303998/1345206 Not sure why the reset breaks persistence.

In the end I decided to just implement reset handlers in all persisted reducers. It's verbose but it prevents the need for purges.

jmrossy avatar Feb 24 '21 18:02 jmrossy

Here is a patch that will alleviate the issue:

diff --git a/node_modules/redux-persist/lib/persistReducer.js b/node_modules/redux-persist/lib/persistReducer.js
index 1116881..ab4559d 100644
--- a/node_modules/redux-persist/lib/persistReducer.js
+++ b/node_modules/redux-persist/lib/persistReducer.js
@@ -81,6 +81,9 @@ function persistReducer(config, baseReducer) {
 
       _paused = false; // @NOTE only ever create persistoid once, ensure we call it at least once, even if _persist has already been set
 
+      //@NOTE PERSIST should resume after purge, see REHYDRATE handler
+      _purge = false;
+      
       if (!_persistoid) _persistoid = (0, _createPersistoid.default)(config); // @NOTE PERSIST can be called multiple times, noop after the first
 
       if (_persist) {

tnortman-jabra avatar Jun 07 '23 14:06 tnortman-jabra

@tnortman-jabra Tnanks it worked for me.

sergeushenecz avatar Apr 12 '24 12:04 sergeushenecz