0

Background
In an application I'm working on, I've found that I can define values in sessionStorage in Chrome 62 on Windows 10, and that apparently changing that value in one tab affects other tabs that point to the same key.

I was operating under the assumption that localStorage is supposed to persist information across all browser windows, while sessionStorage is only supposed to persist information for a specific window or tab.

More specifically, I have an AngularJS service I'm using as a layer for sessionStorage interactions:

export class PersistenceSvc {
    public static $inject: string[] = ['$window'];
    public constructor(public $window: ng.IWindowService) {}

    public save<T>(name: string, data: T): void {
        const saveData: string = JSON.stringify(data);
        this.$window.sessionStorage.setItem(name, saveData);
    }

    public load<T>(name: string): T {
        const loadData: string = this.$window.sessionStorage.getItem(name);
        const result: T = JSON.parse(loadData) as T;
        return result;
    }
}

...That I use from a run block in order to implement some data persistence in my application.

export function persistSomeData(
    someSvc: Services.SomeService, 
    userAgentSvc: Services.UserAgentSvc,
    persistenceSvc: Services.PersistenceSvc,
    $window: ng.IWindowService) {
    if(userAgentSvc.isMobileSafari()) {
        // Special instructions for iOS devices.
        return;
    }

    const dataToPersist: Models.DataModel = persistenceSvc.load<Models.DataModel>('SomeData');
    if(dataToPersist) {
        // Set up the state of someSvc with the data loaded.
    } else {
        // Phone home to the server to get the data needed.
    }

    $window.onbeforeunload = () => {
        persistenceSvc.save<Models.DataModel>('SomeData', someSvc.dataState);
    };
}

persistSomeData.$inject = [
    // All requisite module names, omitted from example because lazy.
];
angular
    .module('app')
    .run(persistSomeData);

When only operating using a single tab, this works fine (unless running from an iOS device, but that's tangential to what I'm encountering.) When you do the following though, you start seeing some more interesting behavior...

Steps:
1. Open a Chrome instance. Create a new tab, and drag that out such that it becomes its own window.
2. Navigate to your site, that's using the above code.
3. Do things on your site that cause someSvc's data state to have different data in the first browser.
4. Do things on your site that cause someSvc's data state to have different data in the second browser.
5. Do something on your site that draws upon someSvc's data state in the first browser.
6. Observe that the data utilized on the first browser instance, was sourced by the second browser instance. (This is the problem, right here.)

Question:
In the past I haven't done a lot of cookie/localStorage/sessionStorage programming, so it's very possible that I've terribly misunderstood something. Bearing that in mind, why is it that window.sessionStorage is behaving in a way that the MDN documentation as well as the winning answer to this SO question says it shouldn't be behaving in?

EDIT: It turns out there is a problem, but it's not clientside. Closing this question, as I was operating under the assumption that the client was the problem.

Andrew Gray
  • 3,756
  • 3
  • 39
  • 75

1 Answers1

1

There is something wrong with your code as a quick and easy test on the browser console shows that sessionStorage only impacts the browser tab that is open. A change in the right tab is not reflecting to the left tab:

enter image description here

basarat
  • 261,912
  • 58
  • 460
  • 511
  • After looking at my code, there is indeed a problem, but it's not client side - it's server side. You're correct that `sessionStorage` is working correctly. I'm going to close this question, since I was operating on a bad assumption. – Andrew Gray Feb 22 '18 at 14:29
  • Also there was a typo in the `persistenceSvc.load` function I transcribed: I wasn't returning `result`. That's fixed in the original post, too. – Andrew Gray Feb 22 '18 at 16:41