From f1660540ead46d17569cffccb0e9ac60f3b0eb84 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Tue, 11 Dec 2018 17:55:22 +0000 Subject: [PATCH 1/3] Move sw-append.js out of src --- packages/gatsby-plugin-offline/.gitignore | 1 + packages/gatsby-plugin-offline/{src => }/sw-append.js | 1 + 2 files changed, 2 insertions(+) rename packages/gatsby-plugin-offline/{src => }/sw-append.js (99%) diff --git a/packages/gatsby-plugin-offline/.gitignore b/packages/gatsby-plugin-offline/.gitignore index 8c9686624a187..10cf84f0d2dc8 100644 --- a/packages/gatsby-plugin-offline/.gitignore +++ b/packages/gatsby-plugin-offline/.gitignore @@ -1,3 +1,4 @@ /*.js !index.js +!sw-append.js yarn.lock diff --git a/packages/gatsby-plugin-offline/src/sw-append.js b/packages/gatsby-plugin-offline/sw-append.js similarity index 99% rename from packages/gatsby-plugin-offline/src/sw-append.js rename to packages/gatsby-plugin-offline/sw-append.js index b209013dd20d8..f9f32d740b304 100644 --- a/packages/gatsby-plugin-offline/src/sw-append.js +++ b/packages/gatsby-plugin-offline/sw-append.js @@ -1,4 +1,5 @@ /* global importScripts, workbox, idbKeyval */ +/* revision %revision% */ importScripts(`idb-keyval-iife.min.js`) const WHITELIST_KEY = `custom-navigation-whitelist` From 38a7b829d1308e1e2f32bd2b0e9a4e6b669700ea Mon Sep 17 00:00:00 2001 From: David Bailey Date: Tue, 11 Dec 2018 17:56:35 +0000 Subject: [PATCH 2/3] Fix ignoring static files + add a random hash to generated sw.js --- packages/gatsby-plugin-offline/README.md | 4 ++-- packages/gatsby-plugin-offline/src/gatsby-node.js | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-plugin-offline/README.md b/packages/gatsby-plugin-offline/README.md index 01ebf254e5d91..073aa58715007 100644 --- a/packages/gatsby-plugin-offline/README.md +++ b/packages/gatsby-plugin-offline/README.md @@ -40,12 +40,12 @@ const options = { cacheId: `gatsby-plugin-offline`, // Don't cache-bust JS or CSS files, and anything in the static directory, // since these files have unique URLs and their contents will never change - dontCacheBustUrlsMatching: /(\.js$|\.css$|\/static\/)/, + dontCacheBustUrlsMatching: /(\.js$|\.css$|static\/)/, runtimeCaching: [ { // Use cacheFirst since these don't need to be revalidated (same RegExp // and same reason as above) - urlPattern: /(\.js$|\.css$|\/static\/)/, + urlPattern: /(\.js$|\.css$|static\/)/, handler: `cacheFirst`, }, { diff --git a/packages/gatsby-plugin-offline/src/gatsby-node.js b/packages/gatsby-plugin-offline/src/gatsby-node.js index c522b426b51f1..c725dadf6cabb 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-node.js +++ b/packages/gatsby-plugin-offline/src/gatsby-node.js @@ -2,6 +2,7 @@ const fs = require(`fs`) const workboxBuild = require(`workbox-build`) const path = require(`path`) const slash = require(`slash`) +const crypto = require(`crypto`) const _ = require(`lodash`) const getResourcesFromHTML = require(`./get-resources-from-html`) @@ -82,12 +83,12 @@ exports.onPostBuild = (args, pluginOptions) => { cacheId: `gatsby-plugin-offline`, // Don't cache-bust JS or CSS files, and anything in the static directory, // since these files have unique URLs and their contents will never change - dontCacheBustUrlsMatching: /(\.js$|\.css$|\/static\/)/, + dontCacheBustUrlsMatching: /(\.js$|\.css$|static\/)/, runtimeCaching: [ { // Use cacheFirst since these don't need to be revalidated (same RegExp // and same reason as above) - urlPattern: /(\.js$|\.css$|\/static\/)/, + urlPattern: /(\.js$|\.css$|static\/)/, handler: `cacheFirst`, }, { @@ -125,8 +126,9 @@ exports.onPostBuild = (args, pluginOptions) => { const swAppend = fs .readFileSync(`${__dirname}/sw-append.js`, `utf8`) .replace(/%pathPrefix%/g, pathPrefix) + .replace(/%revision%/g, crypto.randomBytes(16).toString(`hex`)) - fs.appendFileSync(`public/sw.js`, swAppend) + fs.appendFileSync(`public/sw.js`, `\n${swAppend}`) console.log( `Generated ${swDest}, which will precache ${count} files, totaling ${size} bytes.` ) From 16c50c0729ad45e1b493560ed8c7ae9c7aa2233d Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 12 Dec 2018 10:01:51 +0000 Subject: [PATCH 3/3] Remove random revision stuff --- packages/gatsby-plugin-offline/src/gatsby-node.js | 4 +--- packages/gatsby-plugin-offline/sw-append.js | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-node.js b/packages/gatsby-plugin-offline/src/gatsby-node.js index c725dadf6cabb..4d014c439ed8a 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-node.js +++ b/packages/gatsby-plugin-offline/src/gatsby-node.js @@ -2,7 +2,6 @@ const fs = require(`fs`) const workboxBuild = require(`workbox-build`) const path = require(`path`) const slash = require(`slash`) -const crypto = require(`crypto`) const _ = require(`lodash`) const getResourcesFromHTML = require(`./get-resources-from-html`) @@ -126,9 +125,8 @@ exports.onPostBuild = (args, pluginOptions) => { const swAppend = fs .readFileSync(`${__dirname}/sw-append.js`, `utf8`) .replace(/%pathPrefix%/g, pathPrefix) - .replace(/%revision%/g, crypto.randomBytes(16).toString(`hex`)) - fs.appendFileSync(`public/sw.js`, `\n${swAppend}`) + fs.appendFileSync(`public/sw.js`, `\n` + swAppend) console.log( `Generated ${swDest}, which will precache ${count} files, totaling ${size} bytes.` ) diff --git a/packages/gatsby-plugin-offline/sw-append.js b/packages/gatsby-plugin-offline/sw-append.js index f9f32d740b304..b209013dd20d8 100644 --- a/packages/gatsby-plugin-offline/sw-append.js +++ b/packages/gatsby-plugin-offline/sw-append.js @@ -1,5 +1,4 @@ /* global importScripts, workbox, idbKeyval */ -/* revision %revision% */ importScripts(`idb-keyval-iife.min.js`) const WHITELIST_KEY = `custom-navigation-whitelist`