What causes the session storage to be accessed across various browser sessions?

Scenario
While working on an application, I discovered an intriguing behavior in Chrome 62 on Windows 10 related to defining values in sessionStorage. Surprisingly, changing a value in one tab affected other tabs that shared the same key.

Initially, I believed that localStorage was intended to persist information across all browser windows, whereas sessionStorage was meant for specific window or tab persistence.

Specifically, I have an AngularJS service dedicated to handling interactions with sessionStorage:

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;
    }
}

...which is utilized from a run block to facilitate data persistence within the 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) {
        // Update someSvc state with loaded data.
    } else {
        // Fetch necessary data from the server.
    }

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

persistSomeData.$inject = [
    // All necessary module names, not shown in this example.
];
angular
    .module('app')
    .run(persistSomeData);

When using a single tab, everything functions correctly (excluding iOS device issues). However, when following these steps, unexpected behavior arises...

Steps:
1. Open Chrome and create a new tab which is dragged out into its own window.
2. Visit your site utilizing the code mentioned above.
3. Perform actions causing data state changes in the first browser instance.
4. Do the same in the second browser instance.
5. Interact with the site in a manner that accesses the data state of the first browser.
6. Notice that the data retrieved in the first browser actually came from the second browser instance. (This is the issue at hand.)

Inquiry:
Given my limited experience with cookie/localStorage/sessionStorage coding, it's entirely possible that I've misunderstood something. With that said, why does window.sessionStorage exhibit behavior contrary to what's suggested by both MDN documentation and the top answer on this SO question?

EDIT: There seems to be an issue, but it's unrelated to client-side problems. Closing this inquiry as my assumption about the client being responsible was mistaken.

Answer №1

Your code seems to have an issue based on a simple test in the browser console, which indicates that sessionStorage only affects the active browser tab. Changes made in one tab do not reflect in another tab:

https://i.stack.imgur.com/XinB6.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I encountered a TypeError when attempting to load MDX in Next.js with the mdx-js/react library

My Goals and Assumptions for the Project Please note that this question has been translated using Deepl Translations. I aim to integrate mdx-js/react into Next.js for loading mdx files. Environment Details: Operating System: macOS Big Sur Node Version: ...

Uniting Django and Angular.js

I'm looking for guidance on integrating Django with AngularJS. I have a web application developed using Django and in my views.py file, I need to find a way to use JSON to dump data from the database. Unfortunately, I am quite lost and don't know ...

Sending information from popup to primary controller wit the use of AngularJS - (Plunker example provided) with an autocomplete field embedded in the popup

My scenario is a bit unique compared to passing regular data from a modal to the main controller. The input field in my modal has an autocomplete feature. Here is the Plunker I have included for reference: http://plnkr.co/edit/lpcg6pPSbspjkjmpaX1q?p=prev ...

Using redux action in the onPaginationChange function instead of setPaginationState in the given example for the TanStack table - is there a way to

Provided this sample Is there a way to utilize by dispatching a redux action rather than using useState - setPaginationState? onPaginationChange: state => dispatch(browseItemModalActions.setPagination(state)) An error is appearing in the console: ...

Tips for dynamically implementing a pipe in Angular 5

In my Angular application, I have implemented a filter using a pipe to search for option values based on user input. This filter is applied at the field level within a dynamically generated form constructed using an ngFor loop and populated with data from ...

The syntax for an AngularJS controller using the "as" keyword

Incorporating AngularJS 1.6.9 with AngularJS Material has been a challenge for me. I am attempting to integrate a menu item feature following the basic usage, but unfortunately, it did not work as expected due to structural differences. After reviewing th ...

I am facing an issue with my useFetch hook causing excessive re-renders

I'm currently working on abstracting my fetch function into a custom hook for my Expo React Native application. The goal is to enable the fetch function to handle POST requests. Initially, I attempted to utilize and modify the useHook() effect availab ...

When the in-app API calls fail to connect, Node and Angular will refuse to load

I am currently utilizing a node / angular application generated by yo-angular. While everything runs smoothly when running the application locally, an issue arises when attempting to asynchronously load API calls from an external API endpoint through my pr ...

Navigating from an AngularJS page to a ReactJS page on separate ports: A quick guide

I am currently working on an application with a landing page built using AngularJS. The login functionality is also AngularJS-based, and after a successful login, the user's token needs to be stored in local storage and then redirected to the landing ...

Navigating the proper utilization of exports and subpaths in package.json with TypeScript

As a newbie in creating npm packages using TypeScript, I've encountered some issues that I believe stem from misinterpreting the documentation. Currently, I am working with Node 16.16.0 and npm 8.13.2. Here is the structure of my project: src/ ├─ ...

Instructions on invoking a function when a button is clicked

I am attempting to trigger a dataUpdate function upon clicking the edit() button I am striving to modify the record Current Version: Angular CLI: 10.0.6 Angular: 10.0.10 registration.component.html <div> <button type="button&quo ...

Retrieve functions with varying signatures from another function with precise typing requirements

My code includes a dispatch function that takes a parameter and then returns a different function based on the parameter value. Here is a simplified version: type Choice = 'start' | 'end'; function dispatch(choice: Choice) { switch ...

What is the best way to retrieve an object from a loop only once the data is fully prepared?

Hey, I'm just stepping into the world of async functions and I could use some help. My goal is to return an object called name_dates, but unfortunately when I check the console it's empty. Can you take a look at my code? Here's what I have ...

Determine in JavaScript if one date occurs exactly one week after another date

Currently, I am tackling a date comparison task for our application. The main objective is to compare the Start Date inputted by the user with the Operator/Region Effective Date, which signifies when a new list of product prices becomes valid. Our aim is t ...

Limit users to entering either numbers or letters in the input field

How can I enforce a specific sequence for user input, restricting the first two characters to alphabets, the next two to numbers, the following two to characters, and the last four to numbers? I need to maintain the correct format of an Indian vehicle regi ...

What is the reason behind TypeScript requiring me to initialize a property even though I am retrieving its value from a local reference?

I am just beginning to explore Angular. This is the template for my custom component: <div class="row"> <div class="col-xs-12"> <form action=""> <div class="ro"> <d ...

Unexpected token { in Fuse-Box when using Typescript

Here's the beginning of my fuse.ts file import { CSSPluginOptions } from 'fuse-box/plugins/stylesheet/CSSplugin'; import { argv } from 'yargs'; import * as path from 'path'; import { CSSPlugin, CSSResourcePlugin, Env ...

"Error: The property $notify is not found in the type" - Unable to utilize an npm package in Vue application

Currently integrating this npm package for notification functionalities in my Vue application. Despite following the setup instructions and adding necessary implementations in the main.ts, encountering an error message when attempting to utilize its featur ...

Tips for inserting a component into a div selector using Angular 2

Could someone please help me figure out how to inject a component into a div selector using a class or id? I'm familiar with injecting components into other components, but not sure how to do it specifically within a div. Any guidance would be greatly ...

Learn how to implement data-binding in AngularJS using the ui-router and ControllerAs syntax, all without the

I have been following the guidelines laid out by John Papa, but I am facing difficulty in binding values from child controllers to parent controllers using ui-router, ControllerAs, and vm variables instead of $scope. In my attempts, I created two example ...