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

V3 precaching minification #828

Merged
merged 3 commits into from
Sep 29, 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
4 changes: 2 additions & 2 deletions packages/workbox-core/_private.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import logger from './utils/logger.mjs';
import WorkboxError from './models/WorkboxError.mjs';
import fetchWrapper from './utils/fetchWrapper.mjs';
import cacheWrapper from './utils/cacheWrapper.mjs';
import * as cacheNameProvider from './models/cacheNameProvider.mjs';
import * as cacheNames from './models/cacheNames.mjs';
import indexedDBHelper from './utils/indexedDBHelper.mjs';

export {
logger,
fetchWrapper,
cacheWrapper,
WorkboxError,
cacheNameProvider,
cacheNames,
indexedDBHelper,
};
6 changes: 3 additions & 3 deletions packages/workbox-core/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class WorkboxCore {
*/
get cacheNames() {
return {
precache: _private.cacheNameProvider.getPrecacheName(),
runtime: _private.cacheNameProvider.getRuntimeName(),
precache: _private.cacheNames.getPrecacheName(),
runtime: _private.cacheNames.getRuntimeName(),
};
}

Expand Down Expand Up @@ -72,7 +72,7 @@ class WorkboxCore {
}
}

_private.cacheNameProvider.updateDetails(details);
_private.cacheNames.updateDetails(details);
}

/**
Expand Down
43 changes: 25 additions & 18 deletions packages/workbox-precaching/controllers/PrecacheController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export default class PrecacheController {
* @param {string} cacheName;
*/
constructor(cacheName) {
this._cacheName = _private.cacheNameProvider.getPrecacheName(cacheName);
this._cacheName = _private.cacheNames.getPrecacheName(cacheName);
this._entriesToCacheMap = new Map();
this._precacheEntriesModel = new PrecachedDetailsModel(this._cacheName);
this._precacheDetailsModel = new PrecachedDetailsModel(this._cacheName);
if (process.env.NODE_ENV !== 'production') {
this._checkEntryRevisioning = true;
}
Expand Down Expand Up @@ -56,24 +56,31 @@ export default class PrecacheController {
_parseEntry(input) {
switch (typeof input) {
case 'string': {
if (input.length === 0) {
throw new _private.WorkboxError('add-to-cache-list-unexpected-type', {
entry: input,
});
if (process.env.NODE_ENV !== 'production') {
if (input.length === 0) {
throw new _private.WorkboxError(
'add-to-cache-list-unexpected-type', {
entry: input,
}
);
}
}

return new PrecacheEntry(input, input, input, new Request(input));
return new PrecacheEntry(input, input, input);
}
case 'object': {
if (!input || !input.url) {
throw new _private.WorkboxError('add-to-cache-list-unexpected-type', {
entry: input,
});
if (process.env.NODE_ENV !== 'production') {
if (!input || !input.url) {
throw new _private.WorkboxError(
'add-to-cache-list-unexpected-type', {
entry: input,
}
);
}
}

const cacheBust = input.revision ? true : false;
return new PrecacheEntry(input, input.url, input.revision || input.url,
new Request(input.url), cacheBust);
return new PrecacheEntry(
input, input.url, input.revision || input.url, !!input.revision);
}
default:
throw new _private.WorkboxError('add-to-cache-list-unexpected-type', {
Expand Down Expand Up @@ -158,7 +165,7 @@ export default class PrecacheController {
* false if the entry is already cached and up-to-date.
*/
async _cacheEntry(precacheEntry) {
if (await this._precacheEntriesModel.isEntryCached(precacheEntry)) {
if (await this._precacheDetailsModel._isEntryCached(precacheEntry)) {
return false;
}

Expand All @@ -173,7 +180,7 @@ export default class PrecacheController {
await _private.cacheWrapper.put(this._cacheName,
precacheEntry._cacheRequest, response);

await this._precacheEntriesModel.addEntry(precacheEntry);
await this._precacheDetailsModel._addEntry(precacheEntry);

return true;
}
Expand Down Expand Up @@ -242,7 +249,7 @@ export default class PrecacheController {
* from indexedDB.
*/
async _cleanupDetailsModel(expectedCacheUrls) {
const revisionedEntries = await this._precacheEntriesModel.getAllEntries();
const revisionedEntries = await this._precacheDetailsModel._getAllEntries();
const allDetailUrls = Object.keys(revisionedEntries);

const detailsToDelete = allDetailUrls.filter((detailsUrl) => {
Expand All @@ -252,7 +259,7 @@ export default class PrecacheController {

await Promise.all(
detailsToDelete.map(
(detailsId) => this._precacheEntriesModel.deleteEntry(detailsId)
(detailsId) => this._precacheDetailsModel._deleteEntry(detailsId)
)
);

Expand Down
20 changes: 12 additions & 8 deletions packages/workbox-precaching/models/PrecacheEntry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export default class PrecacheEntry {
* This class ensures all cache list entries are consistent and
* adds cache busting if required.
* @param {*} originalInput
* @param {string} entryId
* @param {string} url
* @param {string} revision
* @param {Request} request
* @param {boolean} shouldCacheBust
*/
constructor(originalInput, entryId, revision, request, shouldCacheBust) {
constructor(originalInput, url, revision, shouldCacheBust) {
this._originalInput = originalInput;
this._entryId = entryId;
this._entryId = url;
this._revision = revision;
this._cacheRequest = request;
const requestAsCacheKey = new Request(url);
this._cacheRequest = requestAsCacheKey;
this._networkRequest = shouldCacheBust ?
this._cacheBustRequest(request) : request;
this._cacheBustRequest(requestAsCacheKey) : requestAsCacheKey;
}

/**
Expand All @@ -36,9 +36,13 @@ export default class PrecacheEntry {
requestOptions.cache = 'reload';
} else {
const parsedURL = new URL(url, location);

// This is done so the minifier can mangle 'global.encodeURIComponent'
const _encodeURIComponent = encodeURIComponent;

parsedURL.search += (parsedURL.search ? '&' : '') +
encodeURIComponent(`_workbox-precaching`) + '=' +
encodeURIComponent(this._revision);
_encodeURIComponent(`_workbox-precaching`) + '=' +
_encodeURIComponent(this._revision);
url = parsedURL.toString();
}

Expand Down
17 changes: 10 additions & 7 deletions packages/workbox-precaching/models/PrecachedDetailsModel.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {_private} from 'workbox-core';

// Allows minifier to mangle this name
const REVISON_IDB_FIELD = 'revision';

/**
* This model will track the relevant information of entries that
* are cached and their matching revision details.
Expand All @@ -10,7 +13,7 @@ export default class PrecachedDetailsModel {
* @param {string} cacheName
*/
constructor(cacheName) {
this._cacheName = _private.cacheNameProvider.getPrecacheName(cacheName);
this._cacheName = _private.cacheNames.getPrecacheName(cacheName);
}

/**
Expand All @@ -19,7 +22,7 @@ export default class PrecachedDetailsModel {
* @param {PrecacheEntry} precacheEntry
* @return {boolean}
*/
async isEntryCached(precacheEntry) {
async _isEntryCached(precacheEntry) {
const revisionDetails = await this._getRevision(precacheEntry._entryId);
if (revisionDetails !== precacheEntry._revision) {
return false;
Expand All @@ -33,7 +36,7 @@ export default class PrecachedDetailsModel {
/**
* @return {Promise<Array>}
*/
async getAllEntries() {
async _getAllEntries() {
const db = await this._getDb();
return await db.getAll();
}
Expand All @@ -46,25 +49,25 @@ export default class PrecachedDetailsModel {
async _getRevision(entryId) {
const db = await this._getDb();
const data = await db.get(entryId);
return data ? data.revision : null;
return data ? data[REVISON_IDB_FIELD] : null;
}

/**
* Add an entry to the details model.
* @param {PrecacheEntry} precacheEntry
*/
async addEntry(precacheEntry) {
async _addEntry(precacheEntry) {
const db = await this._getDb();
await db.put(precacheEntry._entryId, {
revision: precacheEntry._revision,
[REVISON_IDB_FIELD]: precacheEntry._revision,
});
}

/**
* Delete entry from details model;
* @param {string} entryId
*/
async deleteEntry(entryId) {
async _deleteEntry(entryId) {
const db = await this._getDb();
await db.delete(entryId);
}
Expand Down
9 changes: 4 additions & 5 deletions packages/workbox-precaching/utils/cleanRedirect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export default async (response) => {
const body = await bodyPromise;

// new Response() is happy when passed either a stream or a Blob.
return new Response(body, {
headers: clonedResponse.headers,
status: clonedResponse.status,
statusText: clonedResponse.statusText,
});
return new Response(body, ['headers', 'status', 'statusText'].map((key) => {
return clonedResponse[key];
})
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const MOCK_LOCATION = 'https://example.com';
describe(`[workbox-precaching] PrecacheController`, function() {
const sandbox = sinon.sandbox.create();
let logger;
let cacheNameProvider;
let cacheNames;

before(function() {
global.indexedDB = new IDBFactory();
Expand All @@ -34,10 +34,10 @@ describe(`[workbox-precaching] PrecacheController`, function() {
const coreModule = await import('../../../../packages/workbox-core/index.mjs');

logger = coreModule._private.logger;
cacheNameProvider = coreModule._private.cacheNameProvider;
cacheNames = coreModule._private.cacheNames;

const cacheNames = await caches.keys();
await Promise.all(cacheNames.map((cacheName) => {
let usedCacheNames = await caches.keys();
await Promise.all(usedCacheNames.map((cacheName) => {
return caches.delete(cacheName);
}));

Expand Down Expand Up @@ -256,7 +256,7 @@ describe(`[workbox-precaching] PrecacheController`, function() {
expect(updateInfo.notUpdatedEntries.length).to.equal(0);


const cache = await caches.open(cacheNameProvider.getPrecacheName());
const cache = await caches.open(cacheNames.getPrecacheName());
const keys = await cache.keys();
expect(keys.length).to.equal(cacheList.length);

Expand Down Expand Up @@ -347,7 +347,7 @@ describe(`[workbox-precaching] PrecacheController`, function() {
sandbox.stub(logger, 'warn');
sandbox.stub(logger, 'debug');
const logStub = sandbox.stub(logger, 'log');
const cache = await caches.open(cacheNameProvider.getPrecacheName());
const cache = await caches.open(cacheNames.getPrecacheName());

/*
First precache some entries
Expand Down Expand Up @@ -429,7 +429,7 @@ describe(`[workbox-precaching] PrecacheController`, function() {
sandbox.stub(logger, 'debug');
const logStub = sandbox.stub(logger, 'log');

const cache = await caches.open(cacheNameProvider.getPrecacheName());
const cache = await caches.open(cacheNames.getPrecacheName());

/*
First precache some entries
Expand Down Expand Up @@ -505,7 +505,7 @@ describe(`[workbox-precaching] PrecacheController`, function() {
const precacheController = new PrecacheController();
await precacheController.cleanup();

const hasCache = await caches.has(cacheNameProvider.getPrecacheName());
const hasCache = await caches.has(cacheNames.getPrecacheName());
expect(hasCache).to.equal(false);
});

Expand Down
6 changes: 3 additions & 3 deletions test/workbox-precaching/node/models/test-PrecacheEntry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ describe('[workbox-precaching] PrecacheEntry', function() {
it(`should use search param if 'cache' option is not supported`, async function() {
const PrecacheEntryModule = await import(PRECACHE_ENTRY_PATH);
const PrecacheEntry = PrecacheEntryModule.default;
const entry = new PrecacheEntry('example', '/url', '1234', new Request('/url'), true);
const entry = new PrecacheEntry('example', '/url', '1234', true);
expect(entry._networkRequest.url).to.equal(`${MOCK_LOCATION}/url?_workbox-precaching=1234`);
});

it(`should use search param if 'cache' option is not supported and keep previous search params`, async function() {
const PrecacheEntryModule = await import(PRECACHE_ENTRY_PATH);
const PrecacheEntry = PrecacheEntryModule.default;
const entry = new PrecacheEntry('example', '/url', '1234', new Request('/url?foo=bar'), true);
const entry = new PrecacheEntry('example', '/url?foo=bar', '1234', true);
expect(entry._networkRequest.url).to.equal(`${MOCK_LOCATION}/url?foo=bar&_workbox-precaching=1234`);
});

Expand All @@ -62,7 +62,7 @@ describe('[workbox-precaching] PrecacheEntry', function() {
}
sinon.stub(global, 'Request').value(FakeRequest);

const entry = new PrecacheEntry('example', '/url', '1234', new Request('/url'), true);
const entry = new PrecacheEntry('example', '/url', '1234', true);
expect(entry._networkRequest.url).to.equal(`/url`);
expect(entry._networkRequest.cache).to.equal(`reload`);
});
Expand Down
Loading