Skip to content
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

fix: Avoid changing the URL of the service worker. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bmeurer
Copy link

@bmeurer bmeurer commented Dec 5, 2018

According to the official documentation[1], it's bad practice to change
the URL of the service worker. Also there's not really a point in having
time stamped URLs for the sw-register.js script. This also allows to
simplify the registration quite a bit, leading to less JavaScript to
parse and execute for the browser.

[1] https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle

According to the official documentation[1], it's bad practice to change
the URL of the service worker. Also there's not really a point in having
time stamped URLs for the sw-register.js script. This also allows to
simplify the registration quite a bit, leading to less JavaScript to
parse and execute for the browser.

[1] https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle
@PengXing
Copy link
Contributor

PengXing commented Dec 6, 2018

The original intention of sw-register.js is that we don't want developers care about the cache-control: no-cache header of sw.js. I think that is a little complicated.

The URL of sw.js in sw-register.js gets update using a new suffix which is BUILD_TIME, when you build project. And loading fresh sw-register.js using CURRENT_TIME as suffix, every time when user refreshes their pages. So, sw.js can get updated immediately once you re-deploy your project, without setting any response headers of any files.

If you can set cache-control of sw.js, you don't even need the sw-register.js file.

@bmeurer
Copy link
Author

bmeurer commented Dec 6, 2018

But why do you want to disable caching? That way you loose the benefits of caching for both .js files?

@PengXing
Copy link
Contributor

PengXing commented Dec 6, 2018

If don't disable caching of sw.js, service worker won't get updated immediately.

@bmeurer
Copy link
Author

bmeurer commented Dec 6, 2018

I see, thanks.

@jakearchibald
Copy link

The no-cache thing shouldn't be an issue now https://developers.google.com/web/updates/2018/06/fresher-sw.

The problem with changing the URL is it treats it as a new service worker even if nothing's changed.

@PengXing
Copy link
Contributor

PengXing commented Dec 9, 2018

@jakearchibald Thanks. I didn't notice that this problem has been fixed since Chrome 68. I created a new issue #19, and I'll remove sw-register.js when it's Chrome 68+.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants