Skip to content

Commit

Permalink
Adds ability to add plugins to precaching lifecycle (#1138)
Browse files Browse the repository at this point in the history
* Adds ability to add plugins to precaching lifecycle

* Fixing JSDocs
  • Loading branch information
Matt Gaunt authored and jeffposnick committed Dec 15, 2017
1 parent 7103d7b commit 08d323e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
17 changes: 16 additions & 1 deletion packages/workbox-precaching/_default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -254,4 +258,15 @@ moduleExports.suppressWarnings = (suppress) => {
suppressWarnings = suppress;
};

/**
* Add plugins to precaching.
*
* @param {Array<Object>} newPlugins
*
* @alias workbox.precaching.addPlugins
*/
moduleExports.addPlugins = (newPlugins) => {
plugins = plugins.concat(newPlugins);
};

export default moduleExports;
21 changes: 18 additions & 3 deletions packages/workbox-precaching/controllers/PrecacheController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class PrecacheController {
*
* @param {Object} options
* @param {boolean} options.suppressWarnings Suppress warning messages.
* @param {Array<Object>} options.plugins Plugins to be used for fetching
* and caching during install.
* @return {
* Promise<module:workbox-precaching.PrecacheController.InstallResult>}
*/
Expand All @@ -151,6 +153,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 = [];
Expand All @@ -166,7 +177,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') {
Expand All @@ -185,22 +196,26 @@ class PrecacheController {
*
* @private
* @param {BaseCacheEntry} precacheEntry The entry to fetch and cache.
* @param {Array<Object>} plugins Array of plugins to apply to fetch and
* caching.
* @return {Promise<boolean>} 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) {
response = await cleanRedirect(response);
}

await cacheWrapper.put(this._cacheName,
precacheEntry._cacheRequest, response);
precacheEntry._cacheRequest, response, plugins);

await this._precacheDetailsModel._addEntry(precacheEntry);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
39 changes: 37 additions & 2 deletions test/workbox-precaching/node/controllers/test-default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
});
});
});
1 change: 1 addition & 0 deletions test/workbox-precaching/node/test-precaching-module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe(`[workbox-precaching] Module`, function() {
'addRoute',
'precacheAndRoute',
'suppressWarnings',
'addPlugins',
]);
});

Expand Down

0 comments on commit 08d323e

Please sign in to comment.