WebKit Bug 199929 – sessionStorage/localStorage disconnected from persistent storage if accessed “too early”

Steps to Reproduce

  1. Clicking here will run:
  2. TinyUrl will redirect back to:
  3. This page will show the result of:

Actual Result

Result of :

Expected Result

After a click on the above link, the expected result is 'Hello World'. Safari 12.1.1 or Safari for iOS 12.3.1, however, usually show null (unless using the workaround from below).

Observations

  1. Chrome or Firefox always show the expected result.
  2. It appears that the WebKit bug occurs if sessionStorage or localStorage are accessed “too early.” In that case, they appear disconnected from the persistent storage for the entire time the user stays on the current page. This can also be seen in the Web Inspector.
  3. In the current window/tab, navigate to a URL with some other origin (say, https://bugs.webkit.org and then return to (since this URL has no query parameter, sessionStorage is not accessed during page load). The Web Inspector will show that Session Storage is not empty. In fact, the first click on the link at the top of this page (when the Session Storage was not accessed too early) properly created an entry.

Workaround

A workaround is for the JavaScript code to wait before accessing the storage. To verify, enter a wait time here (50ms or more should work) and click the link at the top of the page again: