Leonardo
Leonardo

Reputation: 237

How to update the cache in the background?

I'm starting in the PWA world with the workbox. I currently use the vue-cli 3 tamplate for pwa, which uses the workbox.

I configured all offline access:

pwa: {
  workboxPluginMode: 'GenerateSW',
  workboxOptions: {
    skipWaiting: true
  }
}

The only possibility of updating my cache is when I enter the site, the generated SW checks if there is an update and if it exists, it is enabled so that the next reload is activated.

But is there a possibility to do this in the background? When I send a push, ask the cache to be updated? Can anyone give any hints how can I do this?

Upvotes: 1

Views: 668

Answers (1)

Jeff Posnick
Jeff Posnick

Reputation: 56044

What you're describing most closely matches the idea behind Periodic Background Sync, which would "wake" your service worker up at predefined intervals, and give it a chance to update itself.

Unfortunately, no browsers currently support Periodic Background Sync, and it appears to have stalled a bit in terms of getting standardized.

Note that while it is also possible to "wake" your service worker by using Push Notifications, users have to explicitly accept notification permissions first, and you need to show a notification message each time your service worker "wakes up". You can't just silently trigger a push event handler without showing a notification. Therefore, attempting to replicate Periodic Background Sync via Push Notifications isn't a great idea.

Upvotes: 3

Related Questions