Skip to content

Commit

Permalink
Adding some logging for the precache details (#682)
Browse files Browse the repository at this point in the history
* Adding some logging for the precache details

* Removing bad import

* Removing bad import

* Minor tidy up and a test for the install result

* minor tidy-up

* Adding singlequotes

* Fixing a linting issue

* Extend the length of the browser and sw tests

* Fixing failing test

* Add an additional test to cater for the edge case I hit
  • Loading branch information
Matt Gaunt authored Jul 14, 2017
1 parent 30335fc commit d7fc469
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 9 deletions.
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
Expand Up @@ -111,12 +111,12 @@ 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) {
return;
return [];
}

const cachePromises = [];
Expand All @@ -136,13 +136,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 +160,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 @@ -222,6 +222,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()));
38 changes: 36 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 @@ -239,7 +241,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 @@ -253,11 +268,30 @@ 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();
expect(secondIdbUrls).to.include.members(urls);
expect(secondIdbUrls).not.to.include.members([removedUrl]);
});

it('should return empty array from install() if no resources to precache', async function() {
const cacheDetails = await cacheManager.install();
(Array.isArray(cacheDetails)).should.equal(true);
(cacheDetails.length).should.equal(0);
});
});

0 comments on commit d7fc469

Please sign in to comment.