Is "localStorage" in Firefox only working when the page is online?

As of Oct 5 2020, localStorage on Firefox seems to be broken again. Try this:

  1. Download Mozilla demo page: https://mdn.github.io/dom-examples/web-storage/
  2. Change the animal/color to something other than default.
  3. close the page's tab (or the browser).
  4. Download the page again. It's back to defaults. (Firefox 81:0 et.al.)

Even worse, if you do step 1 & 2 above and then open another copy of the demo in a new tab, not only does the new tab not get the saved data, but the original demo page (refresh it) has gone back to the defaults; as though the new tab STEPPED on the saved data.


Well, the linked document does say that

localStorage is the same as globalStorage[location.hostname], with the exception of being scoped to an HTML5 origin (scheme + hostname + non-standard port)

I don't want to claim that I understand 100% what that means, but the bit in brackets would suggest that the URL needs to have certain properties - in particular that the scheme and hostname are what Firefox considers an HTML 5 origin. I suspect that file:/// URLs don't match this, while your http://127.0.0.1/ does.

edit: Looking at the W3C's description of the Origin property, step 7 looks like it might be causing the problem. Depending on how the localStorage handling is implemented, it may be expecting a 3-tuple as returned by step 12, but for a file:// URL the return value may be just about anything.

So, er, I suppose it is by design. On reflection, chances are that this isn't really by design; there's no reason why localStorage shouldn't work for file:// URLs. It might just be a case of the output of one browser-specific implementation not matching the expectations of another.

As for workarounds, would globalStorage not do what you want here?


It seems a bug: Bug 507361 - localStorage doesn't work in file:/// documents
Hope is fixed soon!

2011-09-13: Bug fixed, implemented in 'Mozilla8'. I tested this with Firefox 8 and it works now.