I would like users to be able to update on the spot when a new service worker is available and waiting? I'm already able to show a pop up when new update is available but I would like to add a button to force update on the spot. I understand this can be achieved with calling skipWaiting but not sure how to implement it with a Create React App. Have anyone able to achieve this? Would appreciate the help. Thank you!
相关问题
- How to toggle on Order in ReactJS
- Refreshing page gives Cannot GET /page_url in reac
- Adding a timeout to a render function in ReactJS
- React Native Inline style for multiple Text in sin
- Issue with React.PropTypes.func.isRequired
相关文章
- Why would we use useEffect without a dependency ar
- Is it possible to get ref of props.children?
- Stateless function components cannot be given refs
- React testing library: Test attribute / prop
- React/JestJS/Enzyme: How to test for ref function?
- Material-UI [v0.x] RaisedButton on hover styles
- Remove expo from react native
- ReactJS toLowerCase is not a function
The CRA
build\service-worker.js
file now (v3 plus) includes code to handleskipWaiting
:The register function
serviceWorker.js
file that is called inindex.js
now accepts a config parameter:This will skip waiting and then refresh the page once the update has been applied.
the answer from @user10532439 did not work for me. I ended up using https://github.com/bbhlondon/cra-append-sw and adding
along with
I used a package called https://github.com/bbhlondon/cra-append-sw to append the following code to call trigger skipWaiting:
You should call the skipWaiting method in the service worker's install event. The service worker is decoupled from the UI/React code. So nothing do do with react really.
Right now I have a pattern where I refactored the install event code to its own method, so my service workers tend to look like this:
Also: donb't call skipWaiting if you need to sync cached assets with the active UI. If your service worker is being updated and could possibly break the UI then don't call skipWaiting. Instead wait for the user to refresh the page. You could use the messaging infrastructure to inform the user an update is available have them refresh as an example.
At serviceWorker.js file can find this code
So implement the config.onUpdate funtion
Create a file swConfig.js
At index.js send the implement function to serviceWorker
Check out this repo https://github.com/wgod58/create_react_app_pwaupdate
I know that it has been answered but I found a solution that does not require any additional packages except workbox of course.
at the package.json :
serviceWorkerBuild.js
and serviceWorkerRules.js where you can add your rules:
Also add
window.location.reload();
at serviceWorker.js at line #78.