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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/workbox-cache-expiration/Plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
limitations under the License.
*/

import {CacheExpiration} from './CacheExpiration.mjs';
import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
import {assert} from 'workbox-core/_private/assert.mjs';
import {CacheExpiration} from './CacheExpiration.mjs';
import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';

import './_version.mjs';

/**
Expand Down Expand Up @@ -86,6 +88,10 @@ class Plugin {
* @private
*/
_getCacheExpiration(cacheName) {
if (cacheName === cacheNames.getRuntimeName()) {
throw new WorkboxError('expire-custom-caches-only');
}

let cacheExpiration = this._cacheExpirations.get(cacheName);
if (!cacheExpiration) {
cacheExpiration = new CacheExpiration(cacheName, this._config);
Expand Down
4 changes: 4 additions & 0 deletions packages/workbox-core/models/messages/messages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export default {
return `The arguments passed into responsesAreSame() appear to be ` +
`invalid. Please ensure valid Responses are used.`;
},
'expire-custom-caches-only': () => {
return `You must provide a 'cacheName' property when using the ` +
`expiration plugin with a runtime caching strategy.`;
},
'unit-must-be-bytes': ({normalizedRangeHeader}) => {
if (!normalizedRangeHeader) {
throw new Error(`Unexpected input to 'unit-must-be-bytes' error.`);
Expand Down
1 change: 1 addition & 0 deletions packages/workbox-strategies/_default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {CacheOnly} from './CacheOnly.mjs';
import {NetworkFirst} from './NetworkFirst.mjs';
import {NetworkOnly} from './NetworkOnly.mjs';
import {StaleWhileRevalidate} from './StaleWhileRevalidate.mjs';

import './_version.mjs';

/**
Expand Down
11 changes: 11 additions & 0 deletions test/workbox-cache-expiration/node/test-Plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {devOnly} from '../../../infra/testing/env-it';

import {Plugin} from '../../../packages/workbox-cache-expiration/Plugin.mjs';
import {CacheExpiration} from '../../../packages/workbox-cache-expiration/CacheExpiration.mjs';
import {cacheNames} from '../../../packages/workbox-core/_private/cacheNames.mjs';

describe(`[workbox-cache-expiration] Plugin`, function() {
const sandbox = sinon.sandbox.create();
Expand Down Expand Up @@ -156,4 +157,14 @@ describe(`[workbox-cache-expiration] Plugin`, function() {
expect(CacheExpiration.prototype.expireEntries.callCount).to.equal(1);
});
});

describe(`_getCacheExpiration()`, function() {
it(`should reject when called with the default runtime cache name`, async function() {
const plugin = new Plugin({maxAgeSeconds: 1});
await expectError(
() => plugin._getCacheExpiration(cacheNames.getRuntimeName()),
'expire-custom-caches-only'
);
});
});
});
5 changes: 3 additions & 2 deletions test/workbox-strategies/node/test-default.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';

import {CacheFirst, CacheOnly, NetworkFirst, NetworkOnly, StaleWhileRevalidate} from '../../../packages/workbox-strategies/_public.mjs';
import strategies from '../../../packages/workbox-strategies/_default.mjs';

Expand Down Expand Up @@ -65,7 +66,7 @@ describe(`[workbox-strategies] Default Export`, function() {
});
});

describe(`StaleWhileRevalidate()`, function() {
describe(`staleWhileRevalidate()`, function() {
it(`should return a StaleWhileRevalidate instance`, function() {
const strategy = strategies.staleWhileRevalidate();
expect(strategy).to.be.an.instanceof(StaleWhileRevalidate);
Expand All @@ -75,7 +76,7 @@ describe(`[workbox-strategies] Default Export`, function() {
const strategy = strategies.staleWhileRevalidate({
plugins: [CUSTOM_PLUGIN],
});
// Stale while revalidate adds a plugin for opaque resposnes
// Stale while revalidate adds a plugin for opaque responses.
expect(strategy._plugins.length).to.equal(2);
expect(strategy._plugins[1]).to.equal(CUSTOM_PLUGIN);
});
Expand Down