2

I am using a cookie in LWC to store some user settings.

This is create cookie method:

createCookie(name, value, days) {
        var expires;
        if (days) {
            const date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        }
        else {
            expires = "";
        }
        document.cookie = name + "=" + encodeURI(value) + expires + "; path=/";
    }

It works like a charm. However, the cookie never gets preserved for more than one day.

I tried setting days parameter as null(for unlimited cookie) and 365. In both cases the cookie is gone by the next day.

I checked my chrome settings and it is set to "Block third-party cookies in Incognito", which is default setting.

Is it an LWC limitation or am I missing something? Not really looking forward to the idea of using apex to store said settings in a custom object.

1
  • Hi, and welcome to SFSE! Please take a moment to read about How to Ask and take the tour. Also, I'd like to mention that this is an X-Y Problem. You're asking about a certain Y ("why can't my cookies persist more than a day?") rather than an X (e.g. "I'm trying to store data in the browser so I can..."). While I agree that this question is useful in its own right, the answer might not solve your underlying problem. Consider asking another question that speaks directly to your X. You can leave this question here, though, it might be an answer useful to someone.
    – sfdcfox
    Commented Jul 24, 2022 at 13:24

1 Answer 1

2

First, if you're logging in through an incognito tab, closing the tab deletes all kinds of tracking data, including cookies and Web storage. Make sure you're not in incognito mode if you expect your cookies to last beyond a private browsing session.

Second, context is important. In Experience Builder, you should make sure that you're using the GDPR-compliant lightning-user-consent-cookie module to get and set cookies. You also need to enable it for the site. If you're not in Experience Builder, this isn't available.

Third, make sure your cookie isn't exceeding the limits of your browser. I found this link that includes a test to see what your browser (probably) supports. The maximum safe limit appears to be 60 cookies per domain and 4095 bytes per cookie. If you have a lot of components all using cookies, you might be causing cookie loss. Check your browser's online documentation or write a test to determine how much cookie space you have.

Fourth, you need to check your browser for anti-virus or privacy extensions that may be messing with your cookies. Similarly, if you're running some anti-virus or anti-malware software on your computer, it could also be wiping out your cookies on a regular basis. There're simply too many variables to mention, just keep in mind that it might be that you're deleting these cookies yourself by accident.

Fifth, there's an alternative to cookies called the Web Storage API. If you want to persist data, localStorage has no expiration date and can store at least 5MB of data. You may want to consider using this mechanism instead of cookies as an alternative to storing data on the server. I personally prefer the use to Web Storage API for anything I plan on persisting for any length of time. I wrote a wrapper that demonstrates this.

In conclusion, I couldn't find any evidence that LWC has a one-day maximum cookie life span, although to test it, I'd need to write code and wait at least a day to verify if the cookie persisted or not. I doubt that the cookies would be getting set, then being wiped out by the framework. I'll look into it first, but I hope this answer provides some useful hints.

2
  • Thank you for the reply. It's a custom LWC page used inside Salesforce. A line editor on steroids. I use a single cookie to store width of each column. Size is definitely not getting exceeded. Good points about incognito and security software. I accessed the page on my home laptop, which only has windows defender, will check it in couple of days there. localStore is definetely worth exploring, thank you for pointing me out in the right direction. If I meet the dead end, nice to have a backup plan that doesn't involve apex.
    – Arhael
    Commented Jul 25, 2022 at 20:35
  • Seems like the cookie persist on my other computer. But I will definitely explore localStorage at some point. Thank you for your help
    – Arhael
    Commented Jul 27, 2022 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .