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

workbox-precaching rewrite to avoid dependency on activate handler #1820

Merged
merged 11 commits into from
Jan 11, 2019

Conversation

jeffposnick
Copy link
Contributor

@jeffposnick jeffposnick commented Jan 8, 2019

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 the activate event. Previously, the activate event was used to move precached Responses over from a -temp- cache into the active cache. Now, we just add all Responses directly in the activate cache in the install handler, using an extra URL query parameter to store any revision metadata that's provided. (This is the same model that sw-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 for url. 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 call cache.match(key) to retrieve a precached Response, and you need to know the specific key 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. Calling workbox.precaching.cleanupOutdatedCaches() means that the old, Workbox v1-v3 precached entries will be deleted after the new Workbox v4 service worker activates.

Copy link
Member

@philipwalton philipwalton left a 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',
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

packages/workbox-precaching/_default.mjs Outdated Show resolved Hide resolved
event.waitUntil(cleanupOutdatedCaches(cacheName).then((cachesDeleted) => {
if (cachesDeleted.length > 0) {
logger.log(`The following out-of-date precaches were cleaned up ` +
`automatically ${cachesDeleted}`);
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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.

Copy link
Contributor Author

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.)

Copy link
Member

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);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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) {
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: s/2018/2019

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: s/2018/2019/

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: s/2018/2019/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

@workbox-pr-bot
Copy link
Collaborator

PR-Bot Size Plugin

Changed File Sizes

File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.51 KB 3.60 KB +3% 1.56 KB
packages/workbox-broadcast-cache-update/build/workbox-broadcast-cache-update.prod.js 1.12 KB 1.88 KB +68% 939 B ☠️
packages/workbox-build/build/index.js 4.02 KB 3.64 KB -9% 1.36 KB
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.88 KB 3.15 KB -19% 1.29 KB 🎉
packages/workbox-cacheable-response/build/workbox-cacheable-response.prod.js 587 B 594 B +1% 350 B
packages/workbox-cli/build/app.js 6.76 KB 5.58 KB -17% 1.98 KB 🎉
packages/workbox-cli/build/bin.js 2.32 KB 1.16 KB -50% 580 B 🎉
packages/workbox-core/build/workbox-core.prod.js 7.47 KB 6.21 KB -17% 2.80 KB 🎉
packages/workbox-navigation-preload/build/workbox-navigation-preload.prod.js 660 B 667 B +1% 323 B
packages/workbox-precaching/build/workbox-precaching.prod.js 5.80 KB 4.24 KB -27% 1.73 KB 🎉
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.63 KB 1.53 KB -6% 764 B
packages/workbox-routing/build/workbox-routing.prod.js 2.87 KB 3.28 KB +14% 1.43 KB ☠️
packages/workbox-strategies/build/workbox-strategies.prod.js 5.09 KB 4.76 KB -7% 1.19 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.57 KB 1.39 KB -12% 683 B 🎉
packages/workbox-sw/build/workbox-sw.js 1.50 KB 1.51 KB +1% 818 B
packages/workbox-webpack-plugin/build/generate-sw.js 8.04 KB 5.29 KB -34% 1.84 KB 🎉
packages/workbox-webpack-plugin/build/index.js 742 B 349 B -53% 255 B 🎉
packages/workbox-webpack-plugin/build/inject-manifest.js 10.30 KB 6.91 KB -33% 2.40 KB 🎉

New Files

File Size GZipped
packages/workbox-google-analytics/build/workbox-offline-ga.prod.js 1.74 KB 877 B

All File Sizes

View Table
File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.51 KB 3.60 KB +3% 1.56 KB
packages/workbox-broadcast-cache-update/build/workbox-broadcast-cache-update.prod.js 1.12 KB 1.88 KB +68% 939 B ☠️
packages/workbox-build/build/_types.js 41 B 41 B 0% 61 B
packages/workbox-build/build/index.js 4.02 KB 3.64 KB -9% 1.36 KB
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.88 KB 3.15 KB -19% 1.29 KB 🎉
packages/workbox-cacheable-response/build/workbox-cacheable-response.prod.js 587 B 594 B +1% 350 B
packages/workbox-cli/build/app.js 6.76 KB 5.58 KB -17% 1.98 KB 🎉
packages/workbox-cli/build/bin.js 2.32 KB 1.16 KB -50% 580 B 🎉
packages/workbox-core/build/workbox-core.prod.js 7.47 KB 6.21 KB -17% 2.80 KB 🎉
packages/workbox-google-analytics/build/workbox-offline-ga.prod.js 1.74 KB 877 B
packages/workbox-navigation-preload/build/workbox-navigation-preload.prod.js 660 B 667 B +1% 323 B
packages/workbox-precaching/build/workbox-precaching.prod.js 5.80 KB 4.24 KB -27% 1.73 KB 🎉
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.63 KB 1.53 KB -6% 764 B
packages/workbox-routing/build/workbox-routing.prod.js 2.87 KB 3.28 KB +14% 1.43 KB ☠️
packages/workbox-strategies/build/workbox-strategies.prod.js 5.09 KB 4.76 KB -7% 1.19 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.57 KB 1.39 KB -12% 683 B 🎉
packages/workbox-sw/build/workbox-sw.js 1.50 KB 1.51 KB +1% 818 B
packages/workbox-webpack-plugin/build/generate-sw.js 8.04 KB 5.29 KB -34% 1.84 KB 🎉
packages/workbox-webpack-plugin/build/index.js 742 B 349 B -53% 255 B 🎉
packages/workbox-webpack-plugin/build/inject-manifest.js 10.30 KB 6.91 KB -33% 2.40 KB 🎉

Workbox Aggregate Size Plugin

9.38KB gzip'ed (63% of limit)
23.18KB uncompressed

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