-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide callbacks in registerServiceWorker (1.x) #3375
Conversation
Just to add on from #3665
So that developers can opt for auto refresh instead of a toast asking for manual refresh |
# Conflicts: # packages/react-scripts/template/src/registerServiceWorker.js
@James2516 Refresh/ Restart is triggered either by application or browser restart - not by service worker and to my knowledge so it's not possible to add a callback there (it doesn't trigger any events, see ServiceWorker.state). You'd have to persist the state of info about new installed version across reloads somehow (for example reload location with added query param or save information in IndexedDB by service worker itself). It's far more easier to indicate that new version has been installed in the background than that there was an update to application which just has started. |
So the service worker can store the last refresh timestamp in local storage and use that to infer and call the Otherwise the application can do that as well by making use of local storage or URL redirects. |
It doesn't have to be a timestamp, just simple flag saved in IndexedDB should be enough (service worker doesn't have access to local storage). You could also use notification if your app has permission to show these. As service worker is generated by sw-precache-webpack-plugin to add listener to |
Would you please rebase against the We just disabled them by default in #3817, but it makes sense to add some conventional helpers for users who want to opt in. |
I'll prepare PR for next branch but still keep this one hoping that it may appear in v1.x release |
Thanks, but I don't think we'll be doing another 1.x release except for some critical fixes. |
Adding two callbacks in registerServiceWorker file:
onUpdate
- executed after console logs New content is available; please refresh.onSuccess
- executed after console logs Content is cached for offline use.This is a more lightweight change, compared to other related PR: #2426 as in my opinion problem should be separated into two parts:
registerServiceWorker.js
fileHow callbacks are provided or named is up to discussion, the point is to provide minimal working technique to detect update.
When this or similar PR is landed on master branch we can discuss how to notify user about the update.
To use it inside application to show notification (toast/ icon etc) register service worker inside App.js file: