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

Throw a WorkboxError when cacheExpiration is used without cacheName #1079

Merged
merged 6 commits into from
Dec 13, 2017

Conversation

jeffposnick
Copy link
Contributor

R: @addyosmani @gauntface

Fixes #1014

I don't know what the criteria is for doing this sort of thing in NODE_ENV !== 'production' vs. unconditionally, but I went with unconditionally.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 83.533% when pulling 2202357 on no-cache-expiration-on-default-cache into 7a74459 on v3.

Copy link

@gauntface gauntface left a comment

Choose a reason for hiding this comment

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

One thing that we may still face is that if the developer uses the plugin outside of this builder, the error will not throw.

registerRoute(new RegExp(...), strategies.cacheFirst({
  plugins: [
    new expiration.CacheExpirationPlugin()
  ]
}))

Should we add a check to: https://github.com/GoogleChrome/workbox/blob/v3/packages/workbox-cache-expiration/CacheExpirationPlugin.mjs#L114 before treating the issue as fixed?

@addyosmani
Copy link
Member

Should we add a check to: https://github.com/GoogleChrome/workbox/blob/v3/packages/workbox-cache-expiration/CacheExpirationPlugin.mjs#L114 before treating the issue as fixed?

I think adding the check there before we call this fully good would be useful.

Copy link
Member

@addyosmani addyosmani left a comment

Choose a reason for hiding this comment

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

LGTM

@jeffposnick
Copy link
Contributor Author

I'm going to revisit this PR once #1084 lands.

@jeffposnick jeffposnick added the Chillin' Not being actively worked on, or deferred for a point in the future. label Dec 1, 2017
@addyosmani
Copy link
Member

Feel free to re-explore now that #1084 has landed

@jeffposnick
Copy link
Contributor Author

One thing that we may still face is that if the developer uses the plugin outside of this builder, the error will not throw.

I do not believe that the Plugin constructor would have the necessarily state information to detect whether or not there's a cache name configured. The Plugin only finds out about the associated cache name once the runtime caching takes place:

_getCacheExpiration(cacheName) {

My inclination is that if we were to detect this sort of thing at runtime, it should be a warning rather than a fatal error.

But my preference would be to keep the original approach of throwing a fatal error when the strategy is misconfigured ahead of time vs. a warning at runtime.

How do other folks feel about those options?

@gauntface
Copy link

For dev we could a check of: 'If no cache name and a plugin of type "expiration.Plugin", throw an error' and for prod, throw at runtime.

@jeffposnick jeffposnick removed the Chillin' Not being actively worked on, or deferred for a point in the future. label Dec 4, 2017
@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 84.581% when pulling ad7c879 on no-cache-expiration-on-default-cache into 526b1a2 on v3.

@jeffposnick
Copy link
Contributor Author

I know that this was already approved, but I've made changes along the lines of #1079 (comment) and it would be great to have it looked over again prior to merging.

@@ -56,6 +58,15 @@ const mapping = {
const defaultExport = {};
Object.keys(mapping).forEach((keyName) => {
defaultExport[keyName] = (options = {}) => {
if (process.env.NODE_ENV !== 'production') {
if (options.cacheExpiration && !options.cacheName) {

Choose a reason for hiding this comment

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

This isn't needed anymore as the cacheExpiration option doesn't get used - developers have to explicitly create the Plugin.

devOnly.it(`should throw when cacheExpiration is used without cacheName`, async function() {
await expectError(
() => strategies.cacheFirst({
cacheExpiration: {

Choose a reason for hiding this comment

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

Like wise - this test should be failing since this option does nothing now.

@@ -86,6 +88,13 @@ class Plugin {
* @private
*/
_getCacheExpiration(cacheName) {
if (cacheName === cacheNames.getRuntimeName()) {
throw new WorkboxError('cache-expiration-requires-cache-name', {

Choose a reason for hiding this comment

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

NIT: Maybe this error code should be 'expire-custom-caches-only'

throw new Error(`Unexpected input to 'cache-expiration-requires-cache` +
`-name' error.`);
}
return `You must also configure 'cacheName' when using 'cacheExpiration' ` +

Choose a reason for hiding this comment

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

This message should be changed to something like You must provide a 'cacheName' property when using the expiration Plugin with a strategy..

Copy link

@gauntface gauntface left a comment

Choose a reason for hiding this comment

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

I think this branch needs updating with the latest strategy code since the cacheExpiration option isn't used anymore.

@workbox-pr-bot
Copy link
Collaborator

PR-Bot Size Plugin

Changed File Sizes

File Before After Change GZipped
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.13 KB 3.25 KB +4% 1.20 KB

New Files

No new files have been added.

All File Sizes

View Table
File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.17 KB 3.17 KB 0% 1.34 KB
packages/workbox-broadcast-cache-update/build/workbox-broadcast-cache-update.prod.js 1.05 KB 1.05 KB 0% 565 B
packages/workbox-build/build/_types.js 41 B 41 B 0% 61 B
packages/workbox-build/build/index.js 2.52 KB 2.52 KB 0% 1.06 KB
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.13 KB 3.25 KB +4% 1.20 KB
packages/workbox-cacheable-response/build/workbox-cacheable-response.prod.js 593 B 593 B 0% 343 B
packages/workbox-cli/build/app.js 4.57 KB 4.57 KB 0% 1.65 KB
packages/workbox-cli/build/bin.js 2.53 KB 2.53 KB 0% 1.07 KB
packages/workbox-core/build/workbox-core.prod.js 6.67 KB 6.67 KB 0% 2.61 KB
packages/workbox-google-analytics/build/workbox-google-analytics.prod.js 1.94 KB 1.94 KB 0% 985 B
packages/workbox-precaching/build/workbox-precaching.prod.js 4.86 KB 4.86 KB 0% 1.89 KB
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.66 KB 1.66 KB 0% 816 B
packages/workbox-routing/build/workbox-routing.prod.js 2.74 KB 2.74 KB 0% 1.23 KB
packages/workbox-strategies/build/workbox-strategies.prod.js 3.22 KB 3.22 KB 0% 1.00 KB
packages/workbox-sw/build/workbox-sw.js 1.46 KB 1.46 KB 0% 792 B
packages/workbox-webpack-plugin/build/index.js 7.29 KB 7.29 KB 0% 2.45 KB

Workbox Aggregate Size Plugin

☠️ WARNING ☠️

We are using 152% of our max size budget.

Total Size: 22.25KB
Percentage of Size Used: 152%

Gzipped: 8.85KB

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 84.77% when pulling fbddf38 on no-cache-expiration-on-default-cache into 1e980d6 on v3.

@jeffposnick
Copy link
Contributor Author

Unless I've missed something else obvious, this should have the required changes now, @gauntface. Thanks for your patience.

@gauntface gauntface merged commit b843094 into v3 Dec 13, 2017
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.

5 participants