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

Unrevisioned precaching warning #2262

Merged
merged 4 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ module.exports = (additionalManifestEntries) => {
const stringEntries = new Set();

for (const additionalEntry of additionalManifestEntries) {
// Warn about either a string or an object that lacks a precache property.
// (An object with a revision property set to null is okay.)
if (typeof additionalEntry === 'string') {
stringEntries.add(additionalEntry);
} else if (additionalEntry && additionalEntry.revision === undefined) {
stringEntries.add(additionalEntry.url);
}

manifest.push(additionalEntry);
Expand Down
12 changes: 7 additions & 5 deletions packages/workbox-build/src/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ module.exports = {
'bad-manifest-transforms-return-value': ol`The return value from a
manifestTransform should be an object with 'manifest' and optionally
'warnings' properties.`,
'string-entry-warning': ol`Some URLs were passed to additionalManifestEntries
as strings. Please ensure that these URLs contain versioning info
(e.g. 'https://example.com/v1.0/index.js'), as Workbox cannot maintain
its own revision info for them. To disable this warning, pass in an
'string-entry-warning': ol`Some items were passed to additionalManifestEntries
without revisioning info. Please ensure these URLs contain versioning info
(e.g. 'https://example.com/v1.0/index.js'), as Workbox will not update them
after they are initially precached. To disable this warning, pass in an
object with a 'url' property, and a 'revision' property set to null
(e.g. {url: 'https://example.com/v1.0/index.js', revision: null}).`,
(e.g. {url: 'https://example.com/v1.0/index.js', revision: null}). In a
future release, using revision: null will be required. Learn
more at https://bit.ly/wb-precache`,
'no-manifest-entries-or-runtime-caching': ol`Couldn't find configuration for
either precaching or runtime caching. Please ensure that the various glob
options are set to match one or more files, and/or configure the
Expand Down
27 changes: 27 additions & 0 deletions packages/workbox-precaching/src/PrecacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ class PrecacheController {
});
}

const urlsToWarnAbout: string[] = [];
for (const entry of entries) {
// See https://github.com/GoogleChrome/workbox/issues/2259
if (typeof entry === 'string') {
urlsToWarnAbout.push(entry);
} else if (entry && entry.revision === undefined) {
urlsToWarnAbout.push(entry.url);
}

const {cacheKey, url} = createCacheKey(entry);
const cacheMode = (typeof entry !== 'string' && entry.revision) ?
'reload' : 'default';
Expand All @@ -91,6 +99,25 @@ class PrecacheController {

this._urlsToCacheKeys.set(url, cacheKey);
this._urlsToCacheModes.set(url, cacheMode);

if (urlsToWarnAbout.length > 0) {
const warningMessage = `Workbox is precaching URLs without revision ` +
`info: ${urlsToWarnAbout.join(', ')}\nOnce precached, they ` +
`will never be updated.\nThis is only safe for URLs which ` +
`contain versioning info (e.g. /v1.0/app.js or /app.1234.js, not ` +
`/index.html).\nIf your URLs do contain revision info, pass in ` +
`a object with a 'url' property and a 'revision' property set to ` +
`null (e.g. {url: '/v1.0/index.js', revision: null}).\nIn a future ` +
`release, using revision: null will be required.\nLearn more at ` +
`https://bit.ly/wb-precache`;
jeffposnick marked this conversation as resolved.
Show resolved Hide resolved
if (process.env.NODE_ENV === 'production') {
// Use console directly to display this warning without bloating
// bundle sizes by pulling in all of the logger codebase in prod.
console.warn(warningMessage);
} else {
logger.warn(warningMessage);
}
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion test/all/node/test-prod-builds.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe(`[all] prod builds`, function() {
const invalidFiles = [];
buildFiles.forEach((filePath) => {
const fileContents = fs.readFileSync(filePath).toString();
if (fileContents.indexOf(`console`) > -1 ||
if ((fileContents.indexOf(`console`) > -1 &&
// See https://github.com/GoogleChrome/workbox/issues/2259
!filePath.includes('workbox-precaching')) ||
fileContents.indexOf(`%cworkbox`) > -1) {
invalidFiles.push(filePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,47 @@ describe(`[workbox-build] lib/additional-manifest-entries-transform.js`, functio
function getManifest() {
return [{
url: '/first',
revision: null,
}];
}

it(`should not make any changes when additionalManifestEntries is empty`, function() {
const transform = additionalManifestEntriesTransform([]);
expect(transform(getManifest())).to.eql({
manifest: [{url: '/first'}],
manifest: [{url: '/first', revision: null}],
warnings: [],
});
});

it(`should add the additionalManifestEntries to the end of the existing manifest`, function() {
const transform = additionalManifestEntriesTransform([
{url: '/second'},
{url: '/third'},
{url: '/second', revision: null},
{url: '/third', revision: null},
]);

expect(transform(getManifest())).to.eql({
manifest: [
{url: '/first'},
{url: '/second'},
{url: '/third'},
{url: '/first', revision: null},
{url: '/second', revision: null},
{url: '/third', revision: null},
],
warnings: [],
});
});

it(`should return a warning, along with the modified manifest, when additionalManifestEntries contains a string`, function() {
it(`should return a warning, along with the modified manifest, when additionalManifestEntries contains a string or an entry without revision`, function() {
const transform = additionalManifestEntriesTransform([
'/second',
{url: '/third'},
]);

expect(transform(getManifest())).to.eql({
manifest: [
{url: '/first'},
{url: '/first', revision: null},
'/second',
{url: '/third'},
],
warnings: [errors['string-entry-warning'] + '\n - /second\n'],
warnings: [errors['string-entry-warning'] + '\n - /second\n - /third\n'],
});
});
});
25 changes: 25 additions & 0 deletions test/workbox-precaching/sw/test-PrecacheController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ describe(`PrecacheController`, function() {
});
});

it(`should log a warning unless an entry has a revision property`, function() {
const logObject = process.env.NODE_ENV === 'production' ? console : logger;
const warnStub = sandbox.stub(logObject, 'warn');

const precacheController = new PrecacheController();

precacheController.addToCacheList([
'/should-warn',
]);
expect(warnStub.calledOnce).to.be.true;
warnStub.resetHistory();

precacheController.addToCacheList([
{url: '/also-should-warn'},
]);
expect(warnStub.calledOnce).to.be.true;
warnStub.resetHistory();

precacheController.addToCacheList([
{url: '/should-not-warn', revision: null},
{url: '/also-should-not-warn', revision: '1234abcd'},
]);
expect(warnStub.notCalled).to.be.true;
});

it(`should add url + revision objects to cache list`, async function() {
const precacheController = new PrecacheController();

Expand Down