Can't able to access localStorage in second tab when opened in incognito [Mozilla Firefox]

放肆的年华 提交于 2020-07-10 06:41:45

问题


Question:

How do I create a workaround to save state between incognito windows on Mozilla?


Description:

I have an application which is dependent on localStorage. I went into a weird situation where when a user is using the application in incognito mode. And when he duplicate the window in Mozilla incognito mode(second window).

The properties in localStorage are missing. When he duplicates the window again(third window), we are able to access localStorage properties.

This is only happening in Firefox private window and everything else is working fine in Chrome.

I need a workaround for this rather than using cookies.


Test Case:

Here is how to reproduce this.

Open this example in Mozilla private window W3 Webstorage Local now open console and check the localStorage now duplicate the same window and look for localStorage (Properties missing) do the same in the third window can able to see the properties.


回答1:


Check out PersistJS.

PersistJS is a JavaScript client-side persistent storage library.

One more reasonable thing you could do would be to use some form of a database to store your data in.

Got this directly from HTMLUI Fact #3


LocalStorage values created in "incognito" mode are isolated When you fire-up a browser in private/incognito/safe mode (sometimes more crudely- and accurately- referred to as "porn mode"), it will create a new, temporary database for LocalStorage values. That means anything saved to LocalStorage will be destroyed when the private browsing session is closed, making LocalStorage behave more like SessionStorage.

Additionally, since a browser's "Session Restore" feature does not re-open private mode sessions, anything created in SessionStorage will also be lost after the browser window is closed. Really, in short, any data put in Local or SessionStorage during a private browsing session will be lost as soon as the browser window is closed (intentionally or not).

Key sentence: any data put in Local or SessionStorage during a private browsing session will be lost as soon as the browser window is closed (intentionally or not).

Ironically enough, it's not a bug, it's a feature.

As mentioned by Hyyan Abo Fakher, you can find the same information on Web Storage API MDN




回答2:


This is indeed a bug reported on Firefox 59. It looks like it got fixed at some point, but was reported broken again on build 71. As of the time of writing of this answer it is still broken on FF 76.




回答3:


As mentioned above, it does not have a solution for this because it is the operating logic of the anonymous mode. However, a workaround would be to create a separate session class, ie create a global instance when starting the application so the client will not depend on the sessionStorage or internet connection. The idea would be to start the session global instance at the time of user authentication and after that use the same through the getter and setter to handle whatever is needed. The only bad side is the use of memory in abuse, you have to always clean up what you do not use and use maximum optimization that you can. I used this in some test applications with a good data load (thing of 50 thousand records being manipulated) and I had no problems with performance, sometimes it can help you :)

EX:

// IN TYPESCRIPT

interface CompanyInterface { 
    name: string,
    enable : boolean
}

interface UserInterface {
    name: string,
    age: number,
    company: CompanyInterface
}

interface SessionDataInterface {
    token: string;
    user: UserInterface;
}

class Session {
    private data: SessionDataInterface; 

    public getData(key: string) {
        return this.data[key];
    }

    public setData(key: string, value: any) {
        return this.data[key] = value;
    }

    constructor(token: string | false = false) { 

        if (!token || token.length <= 10) {
            // You Exception.....
        }               
    }
}

// IN JAVASCRIPT

var Session = /** @class */ (function () {
    function Session(token) {
        if (token === void 0) { token = false; }
        if (!token || token.length <= 10) {
            // You Exception.....
        }
    }
    Session.prototype.getData = function (key) {
        return this.data[key];
    };
    Session.prototype.setData = function (key, value) {
        return this.data[key] = value;
    };
    return Session;
}());


来源:https://stackoverflow.com/questions/51043286/cant-able-to-access-localstorage-in-second-tab-when-opened-in-incognito-mozill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!