Managing status of service worker in the page
Yarn
:
yarn add react-workbox
NPM
:
npm install react-workbox
- remove
serviceWorker.unregister()
orserviceWorker.register()
fromsrc/index.js
- delete
src/serverWorker.js
- Add
WorkBoxProvider
torender
method insrc/App.js
import {WorkBoxProvider} from "react-workbox";
render() {
return (
<WorkBoxProvider interval={30 * 1000}>
[...your app content in here]
</WorkBoxProvider>
);
}
- Use
UpdateAvailable
orUpdateActivated
in the app
<UpdateAvailable>
Update Available - Activate the new version first, and refresh the page afterwards
</UpdateAvailable>
<UpdateActivated>
Update Activated - Refresh the page
</UpdateActivated>
All sample projects are changes to generate new version every minute.
All sample projects check for update in 30 seconds interval.
In this sample, only want to notify to the user new version is available, and to activate the new version, all tabs/pages has to be closed and reopened
This scenario is implemented in https://github.com/NShahri/react-workbox/tree/master/packages/example
New service worker will activated asap and will start caching all network requests. The window will be refreshed on user acceptance.
This scenario is implemented in https://github.com/NShahri/react-workbox/tree/master/packages/example-skip-waiting
In this case, a notification will be displayed to user when update is available, and on user confirmation, it will be activated and the page will be refreshed. The gap between activating new service worker and refreshing the window is minimized.
This scenario is implemented in https://github.com/NShahri/react-workbox/tree/master/packages/example-skip-waiting-with-message
- UpdateError
- LastUpdateCheck
- CheckForUpdate
- Tests
- unregister
- logger
MIT