Skip to content

Commit b843094

Browse files
jeffposnickMatt Gaunt
authored andcommitted
Throw a WorkboxError when cacheExpiration is used without cacheName (#1079)
* Throw a WorkboxError when cacheExpiration is used without cacheName. * Include the message. * Review feedback. * Linting.
1 parent 1e980d6 commit b843094

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

packages/workbox-cache-expiration/Plugin.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
limitations under the License.
1212
*/
1313

14+
import {CacheExpiration} from './CacheExpiration.mjs';
1415
import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
1516
import {assert} from 'workbox-core/_private/assert.mjs';
16-
import {CacheExpiration} from './CacheExpiration.mjs';
17+
import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';
18+
1719
import './_version.mjs';
1820

1921
/**
@@ -86,6 +88,10 @@ class Plugin {
8688
* @private
8789
*/
8890
_getCacheExpiration(cacheName) {
91+
if (cacheName === cacheNames.getRuntimeName()) {
92+
throw new WorkboxError('expire-custom-caches-only');
93+
}
94+
8995
let cacheExpiration = this._cacheExpirations.get(cacheName);
9096
if (!cacheExpiration) {
9197
cacheExpiration = new CacheExpiration(cacheName, this._config);

packages/workbox-core/models/messages/messages.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ export default {
179179
return `The arguments passed into responsesAreSame() appear to be ` +
180180
`invalid. Please ensure valid Responses are used.`;
181181
},
182+
'expire-custom-caches-only': () => {
183+
return `You must provide a 'cacheName' property when using the ` +
184+
`expiration plugin with a runtime caching strategy.`;
185+
},
182186
'unit-must-be-bytes': ({normalizedRangeHeader}) => {
183187
if (!normalizedRangeHeader) {
184188
throw new Error(`Unexpected input to 'unit-must-be-bytes' error.`);

packages/workbox-strategies/_default.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {CacheOnly} from './CacheOnly.mjs';
1818
import {NetworkFirst} from './NetworkFirst.mjs';
1919
import {NetworkOnly} from './NetworkOnly.mjs';
2020
import {StaleWhileRevalidate} from './StaleWhileRevalidate.mjs';
21+
2122
import './_version.mjs';
2223

2324
/**

test/workbox-cache-expiration/node/test-Plugin.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {devOnly} from '../../../infra/testing/env-it';
1919

2020
import {Plugin} from '../../../packages/workbox-cache-expiration/Plugin.mjs';
2121
import {CacheExpiration} from '../../../packages/workbox-cache-expiration/CacheExpiration.mjs';
22+
import {cacheNames} from '../../../packages/workbox-core/_private/cacheNames.mjs';
2223

2324
describe(`[workbox-cache-expiration] Plugin`, function() {
2425
const sandbox = sinon.sandbox.create();
@@ -156,4 +157,14 @@ describe(`[workbox-cache-expiration] Plugin`, function() {
156157
expect(CacheExpiration.prototype.expireEntries.callCount).to.equal(1);
157158
});
158159
});
160+
161+
describe(`_getCacheExpiration()`, function() {
162+
it(`should reject when called with the default runtime cache name`, async function() {
163+
const plugin = new Plugin({maxAgeSeconds: 1});
164+
await expectError(
165+
() => plugin._getCacheExpiration(cacheNames.getRuntimeName()),
166+
'expire-custom-caches-only'
167+
);
168+
});
169+
});
159170
});

test/workbox-strategies/node/test-default.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {expect} from 'chai';
2+
23
import {CacheFirst, CacheOnly, NetworkFirst, NetworkOnly, StaleWhileRevalidate} from '../../../packages/workbox-strategies/_public.mjs';
34
import strategies from '../../../packages/workbox-strategies/_default.mjs';
45

@@ -65,7 +66,7 @@ describe(`[workbox-strategies] Default Export`, function() {
6566
});
6667
});
6768

68-
describe(`StaleWhileRevalidate()`, function() {
69+
describe(`staleWhileRevalidate()`, function() {
6970
it(`should return a StaleWhileRevalidate instance`, function() {
7071
const strategy = strategies.staleWhileRevalidate();
7172
expect(strategy).to.be.an.instanceof(StaleWhileRevalidate);
@@ -75,7 +76,7 @@ describe(`[workbox-strategies] Default Export`, function() {
7576
const strategy = strategies.staleWhileRevalidate({
7677
plugins: [CUSTOM_PLUGIN],
7778
});
78-
// Stale while revalidate adds a plugin for opaque resposnes
79+
// Stale while revalidate adds a plugin for opaque responses.
7980
expect(strategy._plugins.length).to.equal(2);
8081
expect(strategy._plugins[1]).to.equal(CUSTOM_PLUGIN);
8182
});

0 commit comments

Comments
 (0)