admin管理员组

文章数量:1332404

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 duplicates 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, you're now able to see the properties.

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 duplicates 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, you're now able to see the properties.

Share Improve this question edited Oct 5, 2020 at 9:18 scharette 6351 gold badge9 silver badges26 bronze badges asked Jun 26, 2018 at 12:45 karthikkarthik 1,1089 silver badges22 bronze badges 4
  • 1 Are you telling us there is a bug? What is the question you're trying to ask here? – dimwittedanimal Commented Jun 26, 2018 at 12:48
  • 1 please post the code as well. We can't figure out where you are doing wrong – EvilsEmpire Commented Jun 26, 2018 at 12:48
  • @dimwittedanimal im saying this is bug and im looking for a workarround maybe i should edit the question – karthik Commented Jun 26, 2018 at 12:52
  • 1 I second OP, the behavior is inconsistent. When you duplicate a tab in in-private mode, about half of the time the localStorage is preserved, and the other half, it is cleared. I would expect it to behave consistently. Since in-private tabs are sharing cookies, they should also share localStorage. – scharette Commented May 18, 2020 at 17:00
Add a ment  | 

3 Answers 3

Reset to default 4

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

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.

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,
    pany: 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;
}());

本文标签: