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

Adding some logging for the precache details #682

Merged
merged 10 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ module.exports = {
'max-len': 0,
'no-invalid-this': 0,
},
}, {
files: ['gulp-tasks/*.js'],
rules: {
'no-console': 0,
},
}],
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {RequestWrapper} from '../../../../workbox-runtime-caching/src/index';
import WorkboxError from '../../../../../lib/workbox-error';
import logHelper from '../../../../../lib/log-helper';

/**
* This class handles the shared logic for caching revisioned and unrevisioned
Expand Down Expand Up @@ -111,8 +112,8 @@ class BaseCacheManager {
* This method will go through each asset added to the cache list and
* fetch and update the cache for assets which have a new revision hash.
*
* @return {Promise} The promise resolves when all the desired assets are
* cached and up -to-date.
* @return {Promise<Array<Object>>} The promise resolves when all the
* desired assets are cached and up -to-date.
*/
async install() {
if (this._entriesToCache.size === 0) {
Expand All @@ -136,13 +137,20 @@ class BaseCacheManager {
*
* @private
* @param {BaseCacheEntry} precacheEntry The entry to fetch and cache.
* @return {Promise} Returns a promise that resolves once the entry is fetched
* and cached.
* @return {Promise<Object>} Returns a promise that resolves once the entry
* has been fetched and cached or skipped if no update is needed. The
* promise resolved with details of the entry and whether it was
* updated or not.
*/
async _cacheEntry(precacheEntry) {
const isCached = await this._isAlreadyCached(precacheEntry);
const precacheDetails = {
url: precacheEntry.request.url,
revision: precacheEntry.revision,
wasUpdated: !isCached,
};
if (isCached) {
return;
return precacheDetails;
}

try {
Expand All @@ -153,7 +161,8 @@ class BaseCacheManager {
cleanRedirects: true,
});

return this._onEntryCached(precacheEntry);
await this._onEntryCached(precacheEntry);
return precacheDetails;
} catch (err) {
throw new WorkboxError('request-not-cached', {
url: precacheEntry.request.url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,71 @@ class RevisionedCacheManager extends BaseCacheManager {
return this._close();
});
}

/**
* Given an array of objects with a 'url', 'revision' value this
* method will create a friendly string to log.
* @private
* @param {Array<Object>} allCacheDetails
* @return {String} A log friendly string.
*/
_createLogFriendlyString(allCacheDetails) {
let stringVersion = `\n`;
allCacheDetails.forEach((cacheDetails) => {
stringVersion += ` URL: '${cacheDetails.url}' Revision: ` +
`'${cacheDetails.revision}'\n`;
});
return stringVersion;
}

/**
* This method will go through each asset added to the cache list and
* fetch and update the cache for assets which have a new revision hash.
*
* @return {Promise<Array<Object>>} The promise resolves when all the
* desired assets are cached and up -to-date.
*/
install() {
return super.install()
.then((allCacheDetails) => {
const updatedCacheDetails = [];
const notUpdatedCacheDetails = [];
allCacheDetails.forEach((cacheDetails) => {
if (cacheDetails.wasUpdated) {
updatedCacheDetails.push({
url: cacheDetails.url,
revision: cacheDetails.revision,
});
} else {
notUpdatedCacheDetails.push({
url: cacheDetails.url,
revision: cacheDetails.revision,
});
}
});

const logData = {};
if (updatedCacheDetails.length > 0) {
logData['New / Updated Precache URL\'s'] =
this._createLogFriendlyString(updatedCacheDetails);
}

if (notUpdatedCacheDetails.length > 0) {
logData['Up-to-date Precache URL\'s'] =
this._createLogFriendlyString(notUpdatedCacheDetails);
}

logHelper.log({
message: `Precache Details: ${updatedCacheDetails.length} requests ` +
`were added or updated and ` +
`${notUpdatedCacheDetails.length} request are already ` +
`cached and up-to-date.`,
data: logData,
});

return allCacheDetails;
});
}
}

export default RevisionedCacheManager;
2 changes: 2 additions & 0 deletions packages/workbox-precaching/test/static/skip-and-claim.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
workbox.logLevel = self.workbox.LOG_LEVEL.verbose;

self.addEventListener('install', (event) => event.waitUntil(self.skipWaiting()));

self.addEventListener('activate', (event) => event.waitUntil(self.clients.claim()));
32 changes: 30 additions & 2 deletions packages/workbox-precaching/test/sw/revisioned-caching.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
importScripts('/__test/mocha/sw-utils.js');
importScripts('/__test/bundle/workbox-precaching');

workbox.logLevel = self.workbox.LOG_LEVEL.verbose;

describe('sw/revisioned-caching()', function() {
let cacheManager;

Expand Down Expand Up @@ -240,7 +242,20 @@ describe('sw/revisioned-caching()', function() {
return {url, revision: 'dummy-revision'};
});
cacheManager.addToCacheList({revisionedFiles: firstRevisionedFiles});
await cacheManager.install();

const firstCacheDetails = await cacheManager.install();
let updatedCount = 0;
let notUpdatedCount = 0;
firstCacheDetails.forEach((cacheDetails) => {
if (cacheDetails.wasUpdated) {
updatedCount++;
} else {
notUpdatedCount++;
}
});
expect(updatedCount).to.equal(3);
expect(notUpdatedCount).to.equal(0);

await cacheManager.cleanup();

const firstIdbUrls = await cacheManager._revisionDetailsModel._idbHelper.getAllKeys();
Expand All @@ -254,7 +269,20 @@ describe('sw/revisioned-caching()', function() {
return {url, revision: 'dummy-revision'};
});
secondCacheManager.addToCacheList({revisionedFiles: secondRevisionedFiles});
await secondCacheManager.install();

const secondCacheDetails = await secondCacheManager.install();
updatedCount = 0;
notUpdatedCount = 0;
secondCacheDetails.forEach((cacheDetails) => {
if (cacheDetails.wasUpdated) {
updatedCount++;
} else {
notUpdatedCount++;
}
});
expect(updatedCount).to.equal(0);
expect(notUpdatedCount).to.equal(2);

await secondCacheManager.cleanup();

const secondIdbUrls = await secondCacheManager._revisionDetailsModel._idbHelper.getAllKeys();
Expand Down