-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Add message event listener when skipWaiting: false #3704
Comments
The script you posted is registered in the service worker by Workbox. You can send a message to the Service worker like this: navigator.serviceWorker.controller.postMessage({ type: 'SKIP_WAITING'}); a good place for that would be in the |
@LinusBorg updated (reg) {
console.log('New content is available; please refresh.')
window.addEventListener('refreshSW', function () {
reg.waiting.postMessage({ type: 'SKIP_WAITING' })
})
}, The problem is the ServiceWorker can't skip waiting after posting message, because it hasn't a listener to listen the self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
}); should be included in the |
Ah, yeah that's because we are still using workbox 3. Workbox 4 is on our roadmap though (see #3649), and with that update, this will just work. |
The roadmap is a great job, thanks for your contributions. Looking forward to v4. |
well, you can always use the |
Thanks. It works well. |
injectSW mode would bring other problems in service-worker.js, eg:
this hash code is auto generated by webpack which makes precache-manifest injection another headache |
Hi, how did you inject that code? I kind of also haunted by this problem. Thanks |
@wizardpisces pwa: {
workboxPluginMode: 'InjectManifest',
workboxOptions: {
swSrc: 'src/service-worker.js'
}
} |
Then how to inject generateSW Mode related configuration like |
Sorry, I did not use these configurations. You may have a try. |
thanks |
am also haunted by this problem.. @wizardpisces would you mind sharing your code/strategy on how you are injecting the listener into the generated service-worker.js after every build? |
also, out of curiosity, instead of going through all this trouble, why not just set skipwaiting:true in vue.config ? If your plan is to trigger skipWaiting every time the code is updated, doesn't this achieve the same? |
@vesper8 Set |
@loliconer I actually tried to set skipWaiting: true in vue.config.js but I couldn't figure out how to do it. Would you mind sharing how that can be set so it updates right away as you said? I'd appreciate it! Thanks |
pwa: {
workboxOptions: {
skipWaiting: true
}
} |
Which mode did you use ? more detailed infomation you could find here |
@LinusBorg @loliconer @wizardpisces I am now using the v4 alpha which comes with Workbox 4 I have tried adding this But this gives me the following error:
I have also tried adding
|
|
thank you! I actually applied this very solution by following this tutorial yesterday https://www.blog.plint-sites.nl/adding-workbox-to-a-vue-cli-pwa/ except that I am not exactly giving users a choice, I force them to update. That's because my app is still under heavy development and too often I roll out breaking changes to the API that pretty much require people to use the new front-end or else things are broken, so allowing users to skip the update just results in the site being broken. It seems I might have done something wrong however because the prompt to update only occurs after a user does a page refresh.. not when they are continuing to use the app and a new version has been pushed. Is that how it's supposed to work or is it supposed to detect that a new version is available while a user is using the current app? |
You could interval detect new version then update or give user a choice, also you could wait until refresh or second time opening app then detect update, it's very flexible. |
This did not work accordingly. I was expecting that visiting my site will automatically include the new updates. Or an existing tab with my website opened with just a simple refresh (not hard refresh) I am using workbox
|
I can see that the postMessage gets received by the service worker. The strange thing is that the event received in the service worker does not contain the data property. Instead the event only contains this: @LinusBorg Any idea why the event does not contain the postMessage: |
What problem does this feature solve?
When developing PWA, the default
workboxPluginMode
isgenerateSW
, and they setskipWaiting: false
. In this case, the generated service worker should allow our web app to triggerskipWaiting()
on demand, by usingpostMessage()
to communicate with our service worker.This feature was implemented in Workbox v4.1.0, but Vue CLI 3.x can't take advantage of it.
What does the proposed API look like?
Reference:
skipWaiting
option in workbox-webpack-pluginThe text was updated successfully, but these errors were encountered: