-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
feat: e.skipWaiting() in sw-update event #462
Conversation
Thanks for helping! but sudden page refresh may interrupt readers' reading. |
This PR adds the API to implement refresh UI. |
swDest: path.resolve(outDir, 'service-worker.js'), | ||
globDirectory: outDir, | ||
globPatterns: ['**\/*.{js,css,html,png,jpg,jpeg,gif,svg,woff,woff2,eot,ttf,otf}'] | ||
}) | ||
await fs.writeFile( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the importScripts option instead?
https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion.
I compared it and the current code, I didn't feel that the option improves the build steps much. We need the following steps with the option:
- Copy the
service-worker/*.js
files tooutDir
manually. (Webpack doesn't handle the files for service worker because Workbox requires the files Webpack generated.) - Give the path to
service-worker/*.js
files to theimportScripts
option.
Step 1 is the same step as the current appending step essentially. Plus, there are some concerns:
- the
importScripts
option makes extra HTTPS requests. - Chrome has a bug that overlooks the changes of imported scripts (chromium#648295). This implies the change of the
service-worker/*.js
files will not trigger to update service worker's caches.
I updated this PR for |
14d9013
to
c7c0cd9
Compare
c992e38
to
c2eaff3
Compare
Thanks for the great work again, let's leverage #533 ! |
Refs #453.
This PR adds the
e.skipWaiting()
method into thesw-update
event in order to make that custom themes can implement reload buttons on thesw-update
event.Background:
I'm considering to use Vuepress to make the documentation of my OSSs. I will write the links to the documentation from changelogs. In the situation, Vuepress (with service workers) has a critical problem.
So people have to open the documentation and close it and open it again in order to see the latest documentation. It's inconvenient.
Now Vuepress provides the
sw-update
event. It's the best timing to show "refresh" button to visitors in order to reload contents. However, new service worker doesn't available until they close all clients of the old service worker. Browsers will load contents from the old service worker even if it callslocation.reload(true)
. So I couldn't implement "refresh" button.Details:
This PR adds the
e.skipWaiting()
method into thesw-update
event to resolve that problem. Thee.skipWaiting()
method activates new service worker immediately. So we can reload contents after thee.skipWaiting()
method is called.For example, eslint-utils:/docs/.vuepress/theme/Layout.vue is an implementation of "refresh" button. If people clicked the "refresh" button, it calls the
e.skipWaiting()
method to available the new service worker then callslocation.reload(true)
.Notes:
generateSW
method doesn't look to have the option which appends additional code. So I modifiedbuild.js
to append a code toservice-worker.js
after workbox generated it. I'm not sure if this is good.