Skip to content
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

Could you provide a "sharedSessionStorage" storage object? #13

Open
FezVrasta opened this issue May 21, 2020 · 11 comments
Open

Could you provide a "sharedSessionStorage" storage object? #13

FezVrasta opened this issue May 21, 2020 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@FezVrasta
Copy link

FezVrasta commented May 21, 2020

Could this library provide a "sharedSessionStorage" that would work just like "sessionStorage", but with synchronization between different tabs?

It could use BroadcastChannel to keep them in sync.

I think it should look like this:

const bc = new BroadcastChannel('sharedSessionStorage');
bc.onmessage = (evt) => {
  const { action, key, value } = JSON.parse(evt.data);
  switch (action) {
    case 'setItem':
      sessionStorage.setItem(key, value);
      break;
    case 'removeItem':
      sessionStorage.removeItem(key);
      break;
    default:
  }
};
export const sharedSessionStorage = {
  getItem: (key) => sessionStorage.getItem(key),
  setItem: (key, value) => {
    sessionStorage.setItem(key, value);
    bc.postMessage(JSON.stringify({ action: 'setItem', key, value }));
  },
  removeItem: (key) => {
    sessionStorage.removeItem(key);
  }
};

But it doesn't seem to notify the useStorageState hooks when a new value is set from a BroadcastChannel message.

@FezVrasta
Copy link
Author

FezVrasta commented May 21, 2020

I just tried this package (https://www.npmjs.com/package/shared-session-storage) but I get the same issue, could it be a bug in this hook implementation?

(in the Chrome Dev Tools I see the correct values)

@soyguijarro
Copy link
Owner

Hi @FezVrasta! The library does synchronize data between tabs... Did you actually try it and didn't work? Then it'd be a bug. Please let me know. Thanks!

@FezVrasta
Copy link
Author

Yes I tried but the components don’t seem to receive the updated values

@soyguijarro
Copy link
Owner

Yes I tried but the components don’t seem to receive the updated values

If you can provide a minimal reproducible example, I can try looking into it. Thanks again!

@soyguijarro soyguijarro added the bug Something isn't working label May 22, 2020
@FezVrasta
Copy link
Author

FezVrasta commented May 26, 2020

You can try this one https://codesandbox.io/s/friendly-satoshi-dg2p2?file=/src/App.js

Open the app in two different tabs, click the "Random" button, observe the values are not in sync between the two tabs, but if you look into the Chrome Dev Tools, in the Session Storage you see the number key with the correct synced value.

Here's a gif:
Kapture 2020-05-26 at 12 46 21

@soyguijarro
Copy link
Owner

You can try this one https://codesandbox.io/s/friendly-satoshi-dg2p2?file=/src/App.js

Open the app in two different tabs, click the "Random" button, observe the values are not in sync between the two tabs, but if you look into the Chrome Dev Tools, in the Session Storage you see the number key with the correct synced value.

Here's a gif:
Kapture 2020-05-26 at 12 46 21

Okay, it does work if I change it to localStorage, so issue is with sessionStorage specifically. I'll try to look into it. Thanks again for reporting!

@soyguijarro soyguijarro self-assigned this May 27, 2020
@pedro-lb
Copy link

According to MDN docs: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

  • Opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window.

This is working as it should, only localStorage is shared between tabs.

@FezVrasta
Copy link
Author

According to MDN docs: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

  • Opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window.

This is working as it should, only localStorage is shared between tabs.

Of course, but the broadcast channel syncs it across tabs.

@pedro-lb
Copy link

According to MDN docs: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

  • Opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window.

This is working as it should, only localStorage is shared between tabs.

Of course, but the broadcast channel syncs it across tabs.

This worries me since it seems (from this issue, couldn't find anything in repo) that we're sharing sessionStorage between tabs. This behavior goes against the idea of sessionStorage - based on this first comment from @soyguijarro:

Hi @FezVrasta! The library does synchronize data between tabs... Did you actually try it and didn't work? Then it'd be a bug. Please let me know. Thanks!

Does this lib do anything related to this?

I expect it to work just fine with localStorage, but sessionStorage should be isolated (except when using broadcast channels, of course).

@FezVrasta
Copy link
Author

FezVrasta commented Aug 19, 2020

The point of my request is to have some shared state across tabs, that is not persisted after the tabs are closed.

Also, looking at my demo you can see everything in the synchronization logic works, it's just not reflected to React.

@minorgod
Copy link

If this lib is ever maintained again it would be a nice optional behavior, just don't make it the default behavior. Given all the unmerged PRs, my guess is this lib is abandoned. Bummer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants