-
Notifications
You must be signed in to change notification settings - Fork 800
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
workbox-precaching rewrite to avoid dependency on activate handler #1820
Conversation
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.
Left some comments on an initial read-through of the code. I'd also like to play with the implementation a bit before approving, but I probably won't get to that until later.
googleAnalytics: 'googleAnalytics', | ||
precache: 'precache', | ||
precache: 'precache-v4', |
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.
Not crazy about this, but I suppose we need to choose another name, correct?
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.
Right. Another option is 'precache-v2'
, implying that it's the second precache version, rather than using the version number associated with Workbox.
Would you prefer that?
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.
Hmmm, yeah, I think I do like that better. Because most major version bumps will not require a cache name change.
On a similar note, did you test the behavior in the case of someone who's providing their own cache name?
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.
If you're following the guidance at https://developers.google.com/web/tools/workbox/guides/configure-workbox#configure_cache_names and changing prefix
, then you'll automatically get the new 'precache-v2'
as part of your cache name.
If you're bypassing that cache name mechanism entirely (which... I'm assuming not too many people are, since you have to use PrecacheController
directly vs. using workbox.precaching
) then your cache name will not end up changing. As for how bad a problem that is... I think the worst case scenario is ending up with extra entries in the named cache that would never be cleaned up, because the new workbox-precaching
code isn't tracking them.
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.
Cool. Probably worth mentioning in the upgrade notes that anyone in that latter case should probably choose a new name for v4.
event.waitUntil(cleanupOutdatedCaches(cacheName).then((cachesDeleted) => { | ||
if (cachesDeleted.length > 0) { | ||
logger.log(`The following out-of-date precaches were cleaned up ` + | ||
`automatically ${cachesDeleted}`); |
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 this be multiple caches? Should we log it as-is rather than converting to a string?
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.
I'll remove the implicit string conversion.
/** | ||
* Call this method from a service work install event to start | ||
* precaching assets. | ||
* Call this method from the service worker install event to precache new and |
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.
I'd keep the method description first and then add the usage recommendation second.
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.
Changed.
* @param {Event} [options.event] The install event (if needed). | ||
* @param {Array<Object>} [options.plugins] Plugins to be used for fetching | ||
* and caching during install. | ||
* and caching during install. |
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.
Nit: these should be indented 4 spaces per Google style. We haven't done it consistently throughout, so maybe it makes sense to keep it consistent within each file, but at some point we should make it consistent for all of Workbox.
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.
There are enough instances that would need to be changed that I'm keeping as-is. (And our JSDoc linter doesn't complain.)
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.
SG. We can make them consistent in another PR sometime in the future.
`file${alreadyPrecachedCount === 1 ? ' is' : 's are'} already cached.`; | ||
} | ||
|
||
logger.groupCollapsed(message); |
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.
Should this all be inside a NODE_ENV !== 'production'
conditional?
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.
The code that calls this module is inside a NODE_ENV
check.
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.
But uglify may not be able to figure that out. Can you confirm that it doesn't appear in the minified file?
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.
http://storage.googleapis.com/workbox-cdn/releases/4.0.0-beta.0/workbox-precaching.prod.js suggests that it's not in the prod
bundle, due to what I guess is tree-shaking.
This PR changes some of the logging logic in this file, but shouldn't lead to changes in how things are bundled up.
* @private | ||
* @memberof module:workbox-precaching | ||
*/ | ||
export default function(urlObject, ignoreUrlParametersMatching) { |
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.
Same comment as above, can we make this a named export?
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.
Changed.
@@ -0,0 +1,44 @@ | |||
/* | |||
Copyright 2018 Google LLC |
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.
Nit: s/2018/2019
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.
Changed.
@@ -0,0 +1,98 @@ | |||
/* | |||
Copyright 2018 Google LLC |
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.
Nit: s/2018/2019/
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.
Changed.
@@ -0,0 +1,22 @@ | |||
/* | |||
Copyright 2018 Google LLC |
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.
Nit: s/2018/2019/
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.
Changed.
PR-Bot Size PluginChanged File Sizes
New Files
All File SizesView Table
Workbox Aggregate Size Plugin9.38KB gzip'ed (63% of limit) |
R: @philipwalton
Fixes #1793, fixes #1783
This is a pretty significant rewrite of the
workbox-precaching
core behavior. The goal is to ensure that only cleanup work is done in theactivate
event. Previously, theactivate
event was used to move precachedResponse
s over from a-temp-
cache into the active cache. Now, we just add allResponse
s directly in the activate cache in theinstall
handler, using an extra URL query parameter to store anyrevision
metadata that's provided. (This is the same model thatsw-precache
previously used.)Moving to this model means that we no longer have a dependency on IndexedDB within
workbox-precaching
, which should reduce the overall bundle size and lead to what's hopefully less complex code.In addition to that change, there are two new functions exposed on
workbox.precaching
as part of this PR:workbox.precaching.getCacheKeyForUrl(url)
, which will return the key that corresponds to the cache entry forurl
. This takes into account the fact that the key might have an extra URL query parameter appended to it. It's useful if you need to manually callcache.match(key)
to retrieve a precachedResponse
, and you need to know the specifickey
to use.workbox.precaching.cleanupOutdatedCaches()
is not called by default, but developers can try it out by explicitly calling it. It will iterate through all of the caches in scope for the current service worker, and attempt to identify and delete "out of date" precaches. Developers upgrading from earlier versions of Workbox to Workbox v4 might find this useful, since we have a breaking change in precache compatibility in v4, and a brand-new precache is created as part of the upgrade. Callingworkbox.precaching.cleanupOutdatedCaches()
means that the old, Workbox v1-v3 precached entries will be deleted after the new Workbox v4 service worker activates.