From dd941247e8e593d9d3458e220c72a654c58d8dc2 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Thu, 15 Feb 2018 16:25:43 -0800 Subject: [PATCH 1/3] Adding a warning if cache-control is set but not max-age=o or no-cache --- packages/workbox-core/_default.mjs | 3 + packages/workbox-core/_private/assert.mjs | 4 +- .../_private/checkSWFileCacheHeaders.mjs | 82 +++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 packages/workbox-core/_private/checkSWFileCacheHeaders.mjs diff --git a/packages/workbox-core/_default.mjs b/packages/workbox-core/_default.mjs index 8bb809e64..749c67c76 100644 --- a/packages/workbox-core/_default.mjs +++ b/packages/workbox-core/_default.mjs @@ -19,6 +19,7 @@ import {WorkboxError} from './_private/WorkboxError.mjs'; import {cacheNames} from './_private/cacheNames.mjs'; import {logger} from './_private/logger.mjs'; import {assert} from './_private/assert.mjs'; +import {checkSWFileCacheHeaders} from './_private/checkSWFileCacheHeaders.mjs'; import {setLoggerLevel, getLoggerLevel} from './_private/logger.mjs'; import './_version.mjs'; @@ -61,6 +62,8 @@ class WorkboxCore { `${padding}https://github.com/GoogleChrome/workbox/issues/new` ); logger.groupEnd(); + + checkSWFileCacheHeaders(); } } diff --git a/packages/workbox-core/_private/assert.mjs b/packages/workbox-core/_private/assert.mjs index 270b5adea..f98328343 100644 --- a/packages/workbox-core/_private/assert.mjs +++ b/packages/workbox-core/_private/assert.mjs @@ -96,7 +96,7 @@ const isArrayOfClass = (value, expectedClass, } }; -const finalExports = process.env.NODE_ENV === 'production' ? null : { +const finalAssertExports = process.env.NODE_ENV === 'production' ? null : { hasMethod, isArray, isInstance, @@ -106,4 +106,4 @@ const finalExports = process.env.NODE_ENV === 'production' ? null : { isArrayOfClass, }; -export {finalExports as assert}; +export {finalAssertExports as assert}; diff --git a/packages/workbox-core/_private/checkSWFileCacheHeaders.mjs b/packages/workbox-core/_private/checkSWFileCacheHeaders.mjs new file mode 100644 index 000000000..250c3c778 --- /dev/null +++ b/packages/workbox-core/_private/checkSWFileCacheHeaders.mjs @@ -0,0 +1,82 @@ +/* + Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import {logger} from './logger.mjs'; +import '../_version.mjs'; + +const MAX_AGE_REGEX = /max-age\s*=\s*(\d*)/g; + +/** + * Logs a warning to the user recommending changing + * to max-age=0 or no-cache. + * + * @param {string} cacheControlHeader + * + * @private + */ +function showWarning(cacheControlHeader) { + const docsUrl = 'https://developers.google.com/web/tools/workbox/guides/service-worker-checklist#cache-control_of_your_service_worker_file'; + logger.warn(`You are setting a 'cache-control' header of ` + + `'${cacheControlHeader}' on your service worker file. This should be ` + + `set to 'max-age=0' or 'no-cache' to ensure the latest service worker ` + + `is served to your users. Learn more here: ${docsUrl}` + ); +} + +/** + * Checks for cache-control header on SW file and + * warns the developer if it exists with a value + * other than max-age=0 or no-cache. + * + * @private + */ +async function checkSWFileCacheHeaders() { + try { + const swFile = self.location.href; + const response = await fetch(swFile); + if (!response.ok) { + // Response failed so nothing we can check; + return; + } + + if (!response.headers.has('cache-control')) { + // No cache control header. + return; + } + + const cacheControlHeader = response.headers.get('cache-control'); + + const maxAgeResult = MAX_AGE_REGEX.exec(cacheControlHeader); + if (maxAgeResult) { + if (parseInt(maxAgeResult[1], 10) === 0) { + return; + } + } + + if (cacheControlHeader.indexOf('no-cache') !== -1) { + return; + } + + showWarning(cacheControlHeader); + } catch (err) { + // NOOP + } +} + +const finalCheckSWFileCacheHeaders = + process.env.NODE_ENV === 'production' ? null : checkSWFileCacheHeaders; + +export {finalCheckSWFileCacheHeaders as checkSWFileCacheHeaders}; From d98cd16e28cd0f3ac8a675d5e2e3939b0036dcaa Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Fri, 16 Feb 2018 12:24:24 -0800 Subject: [PATCH 2/3] Wrapping in iife --- .../_private/checkSWFileCacheHeaders.mjs | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/packages/workbox-core/_private/checkSWFileCacheHeaders.mjs b/packages/workbox-core/_private/checkSWFileCacheHeaders.mjs index 250c3c778..90eb83674 100644 --- a/packages/workbox-core/_private/checkSWFileCacheHeaders.mjs +++ b/packages/workbox-core/_private/checkSWFileCacheHeaders.mjs @@ -43,37 +43,41 @@ function showWarning(cacheControlHeader) { * * @private */ -async function checkSWFileCacheHeaders() { - try { - const swFile = self.location.href; - const response = await fetch(swFile); - if (!response.ok) { - // Response failed so nothing we can check; - return; - } +function checkSWFileCacheHeaders() { + // This is wrapped as an iife to allow async/await while making + // rollup exclude it in builds. + (async () => { + try { + const swFile = self.location.href; + const response = await fetch(swFile); + if (!response.ok) { + // Response failed so nothing we can check; + return; + } - if (!response.headers.has('cache-control')) { - // No cache control header. - return; - } + if (!response.headers.has('cache-control')) { + // No cache control header. + return; + } + + const cacheControlHeader = response.headers.get('cache-control'); - const cacheControlHeader = response.headers.get('cache-control'); + const maxAgeResult = MAX_AGE_REGEX.exec(cacheControlHeader); + if (maxAgeResult) { + if (parseInt(maxAgeResult[1], 10) === 0) { + return; + } + } - const maxAgeResult = MAX_AGE_REGEX.exec(cacheControlHeader); - if (maxAgeResult) { - if (parseInt(maxAgeResult[1], 10) === 0) { + if (cacheControlHeader.indexOf('no-cache') !== -1) { return; } - } - if (cacheControlHeader.indexOf('no-cache') !== -1) { - return; + showWarning(cacheControlHeader); + } catch (err) { + // NOOP } - - showWarning(cacheControlHeader); - } catch (err) { - // NOOP - } + })(); } const finalCheckSWFileCacheHeaders = From 3f61ca01c0b3c367621fdd19e99fb3075c475ef2 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Wed, 21 Feb 2018 10:20:25 -0800 Subject: [PATCH 3/3] Addressing PR Feedback --- packages/workbox-core/_default.mjs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/workbox-core/_default.mjs b/packages/workbox-core/_default.mjs index 749c67c76..0b783c769 100644 --- a/packages/workbox-core/_default.mjs +++ b/packages/workbox-core/_default.mjs @@ -63,7 +63,9 @@ class WorkboxCore { ); logger.groupEnd(); - checkSWFileCacheHeaders(); + if (typeof checkSWFileCacheHeaders === 'function') { + checkSWFileCacheHeaders(); + } } }