Ilja
Ilja

Reputation: 46527

How to know if service worker was updated in gatsby-plugin-offline

I've installed gatsby-plugin-offline, It works fine, but I want to listen for sw update event so I can notify user that new version of app is available.

Prior to this I used offline-plugin Which I speculate is behinde gatsby's plugin? They have doc explaining how to achieve what I want here: https://github.com/NekR/offline-plugin/blob/master/docs/updates.md but I can't figure out how to get these events via gatsby, any suggestions guys?

Upvotes: 3

Views: 1190

Answers (2)

dknaack
dknaack

Reputation: 60516

I was looking for exactly the same answer.

There is an event you can hook into called "onServiceWorkerUpdateFound". Just create (if you don't have already) a gatsby-browser.js file and do something like this.

gatsby-browser.js

exports.onServiceWorkerUpdateFound = () => {
    // do something
};

I created a <div> that shows a "new version available" message with an onClick event that reloads the page (this then will activate the new service worker).

More Information: Gatsby Browser APIs

Upvotes: 2

Mark Michon
Mark Michon

Reputation: 735

Currently gatsby-plugin-offline is a wrapper around sw-precache, and doesn't provide any direct hooks into much beyond the caching settings shown in the config options. It looks like there are a few ways around this via sw-precache, so it might be worth working on a PR or feature request over on the gatsby issues page.

Upvotes: 2

Related Questions