Unfortunately, localStorage
and sessionStorage
seem to be unreliable in some browsers. Safari 12.1.1 exhibits a similar behavior as described in this StackOverflow post. Both localStorage
and sessionStorage
sometimes appear empty if our function handlePotentialOauthRedirect()
is called “too soon” (and they remain empty while staying on the same page). Unlike hypothesized in the StackOverflow discussion, however, the issue in Safari still occurs when this function is called only after the DOM-ready event. The workaround mentioned on StackOverflow -- accessing sessionStorage.length
-- does not help.
Interestingly, after one or sometimes multiple reloads (in the same window/tab), Safari suddenly does show the expected key/value pairs (as expected in the first place -- since we previously created them in goToOauthPage()
). Since Safari switches to a new process when goToOauthPage()
sets window.location.href
, it’s possible that instead of the new process accessing sessionStorage
“too soon,” it is actually the previous process that persists sessionStorage
“too late.” Indeed, Safari writes sessionStorage
updates to disk with a delay of up to 1 second:
https://trac.webkit.org/browser/webkit/releases/Apple/Safari%2012.1.1/WebKit/UIProcess/WebStorage/LocalStorageDatabase.cpp#L235
Other relevant source code in WebKit:
The following FIXME in the WebKit code could be a contributor to the problem, too:
Interestingly, the FIXME suggestion could be done easily, given this changeset:
There is also another StackOverflow post mentioning the above StackOverflow post (though no conclusive answer, either).