Is there a way to persist state in an iOS Progressive Web App

Hello!

I've been using iOS's new Web Push support for progressive web apps. I am running into an issue where any state I store in the service worker (via postMessage from the main thread) gets lost while the PWA is swiped away and not running in the foreground.

My use case is I when I receive the push notification, I want to use some of that state. It seems the service worker running in the background loses context of that state. I tried working around this via Cache Storage and IndexedDB but those also seem to be lost. I was wondering if anyone has a good way to store state while the PWA app isn't in the foreground?

ServiceWorkers responding to a Web Push message in a push event handler should be able to interact with IndexedDB in a reliable manner.

But also keep in kind: The lifetime of the ServiceWorker in the push event handler is explicitly tied to the push event itself.

If you try to store some state in your IndexedDB, but then don't wait until that state is stored before you waitUntil() on the push event... The ServiceWorker can be terminated before the IndexedDB operation completes.

I am running into an issue where any state I store in the service worker (via postMessage from the main thread) gets lost while the PWA is swiped away and not running in the foreground.

I'm not exactly sure what you mean here, since ServiceWorkers handling push events are - by definition - in the background, and not the foreground app. Generally, push events would arrive while your web app isn't even running. So there's no window/main thread to postMessage to.

Is there a way to persist state in an iOS Progressive Web App
 
 
Q