-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Watch localstorage for changes from other tabs #254
Comments
That's interesting, I'm just curious about performance impact of pushing this as a base feature when it can be added with another plugin. My take would be to make sure it works well with other plugins more than adding it in persistedstate 🤔 Also it's really storage-dependant, and would not work as easily with other storages, making the option dependant on another 🤔 |
Alternatively instead of broadcasting you can just use the storage event to listen for changes in localstorage from other tabs. |
We may consider doing so at present. const store = useStore()
// ... Other store ...
window.onstorage = ({key}) => {
switch (key) {
case <StorePersistKey>:
store.$hydrate()
break;
// ... Other store case ...
}
// Or, addEventListener() for each store
// The advantage is that it can be written together with the store declaration.
const store = useStore()
window.addEventListener('storage', (e) => {
if (e.key === <StorePersistKey>) {
store.$hydrate()
}
}) Of course, I also believe that having it as an optional configuration is essential, as it can help avoid having to write this externally for each store. |
Is there anyone working on this issue? It would be super cool to have this as a built-in feature. |
Me. Currently, progress is stuck at watching of cookies. |
Clear and concise description of the problem
pinia-plugin-persistedstate
is ensuring that when something changes in Pinia, it is written tolocalstorage
. But when another tab running the same web app changes something inlocalstorage
, it is not detected bypinia-plugin-persistedstate
. (Remember thatlocalstorage
is shared over all tabs that are running the same web app.)Suggested solution
pinia-plugin-persistedstate
documentation how to set up pinia-shared-state in such as way as to play nicely withpinia-plugin-persistedstate
to achieve the same effect.Alternative
I'm currently using pinia-shared-state to work around this problem. It sends a Broadcast to update the pinia values in other tabs. It also has an option for using
localstorage
instead ofBroadcast
to do the updates. The made me think that, if we could just listen to thelocalstorage
updates like they do inpinia-shared-state
, we would not needpinia-shared-state
for the stores that are already usingpinia-plugin-persistedstate
withlocalstorage
.Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: