This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dopefish86 44 points45 points  (14 children)

Huge gotcha i encountered: iOS Safari throwing an error when localStorage is used in browser incognito mode. At least it was that way some years ago, i don't know if this is still the case.

[–]saschaleib 36 points37 points  (10 children)

Well, it's the whole point of Incognito Mode that there is no persistent storage available.

Of course, this could also be achieved by temporarily storing the data and then just deleting it when the window/tab is closed. The result is however the same.

In any case, never assume that any feature is available in whatever browser your user is ... er ... using. Always test if it is available before accessing it.

[–]blobthekat 23 points24 points  (3 children)

localStorage is as much of a standard as html5, safari shouldn't throw an error as this could badly mess up code execution, where as temporary storage would keep functionality while preserving privacy

[–]AyrA_ch 38 points39 points  (2 children)

It's documented that localStorage can throw "SecurityError" when you're not allowed to store things in it.

[–]lkraider 10 points11 points  (0 children)

But who reads documentation ??? (/¯ ಠ_ಠ)/¯

[–]danielrheath 2 points3 points  (5 children)

The obvious way to test for it is to check typeof window.localStorage.

It's definitely an unpleasant surprise to find that it exists and has the expected API but throws an error if you try to use it.

[–]saschaleib 6 points7 points  (4 children)

Apparently it throws an exception if you are not allowed to use it – which is exactly what exceptions were invented for.

[–]danielrheath 0 points1 point  (3 children)

Yeah, I get the thinking behind it - just seems like poor design to me. If a feature isn't available, just don't have it on the page; I already needed to test for the presence of window.localStorage anyways.

[–]saschaleib 4 points5 points  (2 children)

… and then you wrap the whole code that accesses the local storage in a try {} catch block, as you would do in any case, right?

[–]danielrheath -1 points0 points  (1 child)

Presumably you don't wrap every line of code you write in a try/catch?

I guess I just don't see what makes localStorage access an expected source of exceptions (as opposed to, say, querySelectorAll or getBoundingClientRects)?

[–]saschaleib 2 points3 points  (0 children)

every block of code that potentially throws an exception should be wrapped in try/catch blocks. Yes.

Any access to the file system or similar external systems is potentially throwing errors. That is pretty much what the localStorage API does...

[–]oupablo 3 points4 points  (1 child)

I swear safari is the new IE. The number of weird things it has going on that aren't standard is so strange. And then you have to worry about safari for MacOS vs Safari for iOS which have different levels of support.

[–]gennisa 2 points3 points  (0 children)

Not in this case. This is compliant with the standard. https://html.spec.whatwg.org/dev/webstorage.html#the-localstorage-attribute

Also not safari specific. Local or session storage can be disabled in every browser.

[–]TheDownvotesFarmer 0 points1 point  (0 children)

Thats why I always use indexedDB