From aeb3cfd26a23b135e8cb78aeaeb0bc6d8d6c00ca Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 15 Dec 2017 10:33:38 -0800 Subject: [PATCH] Adds ability to add plugins to precaching lifecycle --- packages/workbox-precaching/_default.mjs | 17 +++++++- .../controllers/PrecacheController.mjs | 19 +++++++-- .../controllers/test-PrecacheController.mjs | 28 +++++++++++++ .../node/controllers/test-default.mjs | 39 ++++++++++++++++++- .../node/test-precaching-module.mjs | 1 + 5 files changed, 98 insertions(+), 6 deletions(-) diff --git a/packages/workbox-precaching/_default.mjs b/packages/workbox-precaching/_default.mjs index 0b08a6119..147dedfff 100644 --- a/packages/workbox-precaching/_default.mjs +++ b/packages/workbox-precaching/_default.mjs @@ -28,6 +28,7 @@ if (process.env.NODE_ENV !== 'production') { let installActivateListenersAdded = false; let fetchListenersAdded = false; let suppressWarnings = false; +let plugins = []; const cacheName = cacheNames.getPrecacheName(); const precacheController = new PrecacheController(cacheName); @@ -149,7 +150,10 @@ moduleExports.precache = (entries) => { installActivateListenersAdded = true; self.addEventListener('install', (event) => { - event.waitUntil(precacheController.install({suppressWarnings})); + event.waitUntil(precacheController.install({ + suppressWarnings, + plugins, + })); }); self.addEventListener('activate', (event) => { event.waitUntil(precacheController.cleanup()); @@ -254,4 +258,15 @@ moduleExports.suppressWarnings = (suppress) => { suppressWarnings = suppress; }; +/** + * Add plugins to precaching. + * + * @param {Array} newPlugins + * + * @alias workbox.precaching.suppressWarnings + */ +moduleExports.addPlugins = (newPlugins) => { + plugins = plugins.concat(newPlugins); +}; + export default moduleExports; diff --git a/packages/workbox-precaching/controllers/PrecacheController.mjs b/packages/workbox-precaching/controllers/PrecacheController.mjs index 368b52659..4b5f1fcf3 100644 --- a/packages/workbox-precaching/controllers/PrecacheController.mjs +++ b/packages/workbox-precaching/controllers/PrecacheController.mjs @@ -151,6 +151,15 @@ class PrecacheController { if (options.suppressWarnings !== true) { showWarningsIfNeeded(this._entriesToCacheMap); } + + if (options.plugins) { + assert.isArray(options.plugins, { + moduleName: 'workbox-precaching', + className: 'PrecacheController', + funcName: 'install', + paramName: 'plugins', + }); + } } const entriesToPrecache = []; @@ -166,7 +175,7 @@ class PrecacheController { // Wait for all requests to be cached. await Promise.all(entriesToPrecache.map((precacheEntry) => { - return this._cacheEntry(precacheEntry); + return this._cacheEntry(precacheEntry, options.plugins); })); if (process.env.NODE_ENV !== 'production') { @@ -185,14 +194,18 @@ class PrecacheController { * * @private * @param {BaseCacheEntry} precacheEntry The entry to fetch and cache. + * @param {Array} plugins Array of plugins to apply to fetch and + * caching. * @return {Promise} Returns a promise that resolves once the entry * has been fetched and cached or skipped if no update is needed. The * promise resolves with true if the entry was cached / updated and * false if the entry is already cached and up-to-date. */ - async _cacheEntry(precacheEntry) { + async _cacheEntry(precacheEntry, plugins) { let response = await fetchWrapper.fetch( precacheEntry._networkRequest, + null, + plugins, ); if (response.redirected) { @@ -200,7 +213,7 @@ class PrecacheController { } await cacheWrapper.put(this._cacheName, - precacheEntry._cacheRequest, response); + precacheEntry._cacheRequest, response, plugins); await this._precacheDetailsModel._addEntry(precacheEntry); diff --git a/test/workbox-precaching/node/controllers/test-PrecacheController.mjs b/test/workbox-precaching/node/controllers/test-PrecacheController.mjs index eb664049b..d93de7afe 100644 --- a/test/workbox-precaching/node/controllers/test-PrecacheController.mjs +++ b/test/workbox-precaching/node/controllers/test-PrecacheController.mjs @@ -9,6 +9,8 @@ import {prodOnly, devOnly} from '../../../../infra/testing/env-it'; import {_private} from '../../../../packages/workbox-core/index.mjs'; import {logger} from '../../../../packages/workbox-core/_private/logger.mjs'; import PrecacheController from '../../../../packages/workbox-precaching/controllers/PrecacheController.mjs'; +import {fetchWrapper} from '../../../../packages/workbox-core/_private/fetchWrapper.mjs'; +import {cacheWrapper} from '../../../../packages/workbox-core/_private/cacheWrapper.mjs'; const {cacheNames} = _private; @@ -380,6 +382,32 @@ describe(`[workbox-precaching] PrecacheController`, function() { expect(logger.log.callCount).to.be.gt(0); } }); + + it('it should precache with plugins', async function() { + sandbox.spy(fetchWrapper, 'fetch'); + sandbox.spy(cacheWrapper, 'put'); + + const precacheController = new PrecacheController(); + const cacheList = [ + '/index.1234.html', + {url: '/example.1234.css'}, + {url: '/scripts/index.js', revision: '1234'}, + {url: '/scripts/stress.js?test=search&foo=bar', revision: '1234'}, + ]; + precacheController.addToCacheList(cacheList); + + const testPlugins = [{ + name: 'plugin1', + }, { + name: 'plugin2', + }]; + await precacheController.install({ + plugins: testPlugins, + }); + + expect(fetchWrapper.fetch.args[0][2]).to.equal(testPlugins); + expect(cacheWrapper.put.args[0][3]).to.equal(testPlugins); + }); }); describe(`cleanup()`, function() { diff --git a/test/workbox-precaching/node/controllers/test-default.mjs b/test/workbox-precaching/node/controllers/test-default.mjs index 559f81c6d..354d7e484 100644 --- a/test/workbox-precaching/node/controllers/test-default.mjs +++ b/test/workbox-precaching/node/controllers/test-default.mjs @@ -345,9 +345,44 @@ describe(`[workbox-precaching] default export`, function() { await installPromise; - expect(PrecacheController.prototype.install.args[0][0]).to.deep.equal({ - suppressWarnings: true, + expect(PrecacheController.prototype.install.args[0][0].suppressWarnings).to.equal(true); + }); + }); + + describe(`addPlugins()`, function() { + it(`should add plugins during install`, async function() { + let eventCallbacks = {}; + sandbox.stub(self, 'addEventListener').callsFake((eventName, cb) => { + eventCallbacks[eventName] = cb; }); + sandbox.spy(PrecacheController.prototype, 'install'); + + const precacheArgs = ['/']; + + const plugin1 = { + name: 'plugin1', + }; + const plugin2 = { + name: 'plugin2', + }; + + precaching.precache(precacheArgs); + precaching.addPlugins([plugin1]); + precaching.addPlugins([plugin2]); + + const installEvent = new ExtendableEvent('install'); + let installPromise; + installEvent.waitUntil = (promise) => { + installPromise = promise; + }; + eventCallbacks['install'](installEvent); + + await installPromise; + + expect(PrecacheController.prototype.install.args[0][0].plugins).to.deep.equal([ + plugin1, + plugin2, + ]); }); }); }); diff --git a/test/workbox-precaching/node/test-precaching-module.mjs b/test/workbox-precaching/node/test-precaching-module.mjs index 4c8f846a5..237e772ca 100644 --- a/test/workbox-precaching/node/test-precaching-module.mjs +++ b/test/workbox-precaching/node/test-precaching-module.mjs @@ -34,6 +34,7 @@ describe(`[workbox-precaching] Module`, function() { 'addRoute', 'precacheAndRoute', 'suppressWarnings', + 'addPlugins', ]); });