Persist as part of `beforeunload` not working because of `setInterval` usage

Author: dtschustCreated Aug 16, 2018Updated Jul 22, 2026

I'm using redux-persist's more advanced hooks (using createPersistoid) to have more granular control over how often we persist the store state. Currently I am throttling persistoid updates to once every 30 seconds. However, if the user is closing the window or leaving the page I would like to take that opportunity to persist, by listening to the beforeunload event.

This doesn't work reliably, because persistoid.update uses setInterval with a timeout set to the config's throttle value here. In my case, this value is 0, but using setInterval to make this asynchronous means that the persistoid update doesn't complete before the tab is closed. What do you think the best approach would be to fix this? A couple of ideas:

  • if throttle is set to 0, don't use setInterval at all and execute synchronously.
  • a config boolean called disableThrottle or writeSynchronously that executes the update synchronously.

I'd be happy to cut a PR for either approach, I just wanted to know which would be preferred. Thanks so much for maintaining this library! <3