rt2zz/redux-persist

How can we modify the `keyPrefix`

iroy2000 opened this issue ยท 12 comments

Currently the keyPrefix is hardcoded as constant, would that be a good feature if people can overwrite it ??

https://github.com/rt2zz/redux-persist/search?utf8=%E2%9C%93&q=keyPrefix

Ok, answering my own questions, you actually can do that in your application before you configure your store

require('redux-persist/constants').keyPrefix = 'something_else';

And your key will be changed.

rt2zz commented

Why do you want to change the key prefix? I think this will be a very rare use case, and your solution works.

One common use case is to store (non-sensitive) data for multiple users of the app. I would use a user ID as the key prefix.

Another is to serve multiple Redux apps on a single domain, with their persisted state namespaced.

I think it's important to have a first-class API for this.

jschr commented

+1 for this feature as well. Ideally you wouldn't set this globally but could set it on store creation (perhaps as input to a store enhancer?). My specific use case is in my app I create multiple redux stores for managing state (a single global store for db/auth state + local component stores for ui state). I'm only persisting global state at the moment but I could see adding the ability to persist certain parts of ui state as well.

Strictly for aesthetics, it would also be nice to just remove the prefix altogether. I'm using redux-persist in a chrome packaged app and wrote a small storage adapter to persist to chrome.storage.local where the key prefix isn't really necessary. This is definitely a minor nit-pick.

That's my 2 cents, thanks for the great lib regardless!

rt2zz commented

thanks for the input, sounds like we definitely need configurable keyPrefix :)

This would be trivial to implement as a config option keyPrefix

โค๏ธ

rt2zz commented

I will cut a release soon, hopefully tomorrow ๐Ÿ‘

It would be great to mention this in the docs.

๐Ÿ‘ But is that mentioned in the doc ?

rt2zz commented

It would be good to add this to the config options in the docs. PR appreciated!

Ok @rt2zz , PR is here #160

Very useful indeed.
@rt2zz I found this by accident and it was a luck: I have two different apps running on subpath /user and /admin and I'm using crosstabsync as well.
When both were opened in the browser they were endlessly "rehydrating" each other.
Thanks to prefix everything is fine (y)