diff --git a/packages/workbox-cache-expiration/Plugin.mjs b/packages/workbox-cache-expiration/Plugin.mjs index 63abee7e5..e63c30320 100644 --- a/packages/workbox-cache-expiration/Plugin.mjs +++ b/packages/workbox-cache-expiration/Plugin.mjs @@ -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'; /** @@ -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); diff --git a/packages/workbox-core/models/messages/messages.mjs b/packages/workbox-core/models/messages/messages.mjs index 1e30e9176..686545d24 100644 --- a/packages/workbox-core/models/messages/messages.mjs +++ b/packages/workbox-core/models/messages/messages.mjs @@ -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.`); diff --git a/packages/workbox-strategies/_default.mjs b/packages/workbox-strategies/_default.mjs index fa7a5bff8..6e04d9ecd 100644 --- a/packages/workbox-strategies/_default.mjs +++ b/packages/workbox-strategies/_default.mjs @@ -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'; /** diff --git a/test/workbox-cache-expiration/node/test-Plugin.mjs b/test/workbox-cache-expiration/node/test-Plugin.mjs index 2cbb19350..d6facbbf1 100644 --- a/test/workbox-cache-expiration/node/test-Plugin.mjs +++ b/test/workbox-cache-expiration/node/test-Plugin.mjs @@ -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(); @@ -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' + ); + }); + }); }); diff --git a/test/workbox-strategies/node/test-default.mjs b/test/workbox-strategies/node/test-default.mjs index 88976f99d..0aee295da 100644 --- a/test/workbox-strategies/node/test-default.mjs +++ b/test/workbox-strategies/node/test-default.mjs @@ -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'; @@ -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); @@ -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); });