From 1f26ef118d52a6f8ccf736742411dad7350e98f4 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 12 Dec 2018 10:30:22 +0000 Subject: [PATCH 01/10] refactor(gatsby-plugin-offline): only reload if necessary; introduce new API for updates --- docs/docs/add-offline-support-with-a-service-worker.md | 4 ++-- packages/gatsby-plugin-offline/src/gatsby-browser.js | 3 +++ packages/gatsby/cache-dir/navigation.js | 10 ---------- packages/gatsby/cache-dir/register-service-worker.js | 4 ++-- packages/gatsby/src/utils/api-browser-docs.js | 8 ++++++++ 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/docs/docs/add-offline-support-with-a-service-worker.md b/docs/docs/add-offline-support-with-a-service-worker.md index 3ff18ac3a41f0..0e8799e6abfa5 100644 --- a/docs/docs/add-offline-support-with-a-service-worker.md +++ b/docs/docs/add-offline-support-with-a-service-worker.md @@ -45,10 +45,10 @@ Note: Service worker registers only in production builds (`gatsby build`). ### Displaying a message when a service worker updates -To display a custom message once your service worker finds an update, you can use the [`onServiceWorkerUpdateFound`](/docs/browser-apis/#onServiceWorkerUpdateFound) browser API in your `gatsby-browser.js` file. The following code will display a prompt asking the user whether they would like to refresh the page when an update is found: +To display a custom message once your service worker finds an update, you can use the [`onServiceWorkerUpdateReady`](/docs/browser-apis/#onServiceWorkerUpdateReady) browser API in your `gatsby-browser.js` file. The following code will display a prompt asking the user whether they would like to refresh the page when an update is found: ```javascript:title=gatsby-browser.js -exports.onServiceWorkerUpdateFound = () => { +exports.onServiceWorkerUpdateReady = () => { const answer = window.prompt( `This application has been updated. ` + `Reload to display the latest version?` diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index 947a2bc2649e6..9e10d34639ad6 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -7,6 +7,9 @@ exports.onServiceWorkerActive = ({ getResourceURLsForPathname, serviceWorker, }) => { + // reset whitelisted paths + serviceWorker.active.postMessage({ gatsbyApi: `resetWhitelist` }) + // grab nodes from head of document const nodes = document.querySelectorAll(` head > script[src], diff --git a/packages/gatsby/cache-dir/navigation.js b/packages/gatsby/cache-dir/navigation.js index 1932dfc64d491..eb905460c1e42 100644 --- a/packages/gatsby/cache-dir/navigation.js +++ b/packages/gatsby/cache-dir/navigation.js @@ -65,16 +65,6 @@ const navigate = (to, options = {}) => { pathname = parsePath(to).pathname } - // If we had a service worker update, no matter the path, reload window and - // reset the pathname whitelist - if (window.GATSBY_SW_UPDATED) { - const { controller } = navigator.serviceWorker - controller.postMessage({ gatsbyApi: `resetWhitelist` }) - - window.location = pathname - return - } - // Start a timer to wait for a second before transitioning and showing a // loader in case resources aren't around yet. const timeoutId = setTimeout(() => { diff --git a/packages/gatsby/cache-dir/register-service-worker.js b/packages/gatsby/cache-dir/register-service-worker.js index b2edd20569d4d..98290f1559706 100644 --- a/packages/gatsby/cache-dir/register-service-worker.js +++ b/packages/gatsby/cache-dir/register-service-worker.js @@ -23,8 +23,8 @@ if ( if (navigator.serviceWorker.controller) { // At this point, the old content will have been purged and the fresh content will // have been added to the cache. - // We set a flag so Gatsby Link knows to refresh the page on next navigation attempt - window.GATSBY_SW_UPDATED = true + // We call the onServiceWorkerUpdateReady API so users can show update prompts. + apiRunner(`onServiceWorkerUpdateReady`, { serviceWorker: reg }) } else { // At this point, everything has been precached. // It's the perfect time to display a "Content is cached for offline use." message. diff --git a/packages/gatsby/src/utils/api-browser-docs.js b/packages/gatsby/src/utils/api-browser-docs.js index c1067056b5c6b..b63184f4eb54d 100644 --- a/packages/gatsby/src/utils/api-browser-docs.js +++ b/packages/gatsby/src/utils/api-browser-docs.js @@ -209,6 +209,14 @@ exports.onServiceWorkerInstalled = true */ exports.onServiceWorkerUpdateFound = true +/** + * Inform plugins when a service worker has been updated in the background + * and the page is ready to reload to apply changes. + * @param {object} $0 + * @param {object} $0.serviceWorker The service worker instance. + */ +exports.onServiceWorkerUpdateReady = true + /** * Inform plugins when a service worker has become active. * @param {object} $0 From 30ddecdca14e47d8c60f06ac1a6473d47108dc86 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sat, 15 Dec 2018 10:41:38 +0000 Subject: [PATCH 02/10] Remove unneeded code; handle resource fail then SW update --- packages/gatsby-plugin-offline/sw-append.js | 17 +---------------- packages/gatsby/cache-dir/ensure-resources.js | 2 ++ .../gatsby/cache-dir/register-service-worker.js | 6 ++++++ 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/gatsby-plugin-offline/sw-append.js b/packages/gatsby-plugin-offline/sw-append.js index b209013dd20d8..0d6c4a191d467 100644 --- a/packages/gatsby-plugin-offline/sw-append.js +++ b/packages/gatsby-plugin-offline/sw-append.js @@ -12,22 +12,7 @@ const navigationRoute = new workbox.routing.NavigationRoute(({ event }) => { const offlineShell = `%pathPrefix%/offline-plugin-app-shell-fallback/index.html` const cacheName = workbox.core.cacheNames.precache - return caches.match(offlineShell, { cacheName }).then(cachedResponse => { - if (!cachedResponse) { - return fetch(offlineShell).then(response => { - if (response.ok) { - return caches.open(cacheName).then(cache => - // Clone is needed because put() consumes the response body. - cache.put(offlineShell, response.clone()).then(() => response) - ) - } else { - return fetch(event.request) - } - }) - } - - return cachedResponse - }) + return caches.match(offlineShell, { cacheName }) } return fetch(event.request) diff --git a/packages/gatsby/cache-dir/ensure-resources.js b/packages/gatsby/cache-dir/ensure-resources.js index 7b2f38244c043..0c37dd50333d6 100644 --- a/packages/gatsby/cache-dir/ensure-resources.js +++ b/packages/gatsby/cache-dir/ensure-resources.js @@ -129,6 +129,8 @@ class EnsureResources extends React.Component { render() { if (!this.hasResources(this.state.pageResources) && isInitialRender) { + window.___failedResources = true + // prevent hydrating throw new Error(`Missing resources for ${this.state.location.pathname}`) } diff --git a/packages/gatsby/cache-dir/register-service-worker.js b/packages/gatsby/cache-dir/register-service-worker.js index 98290f1559706..b5a666660b785 100644 --- a/packages/gatsby/cache-dir/register-service-worker.js +++ b/packages/gatsby/cache-dir/register-service-worker.js @@ -25,6 +25,12 @@ if ( // have been added to the cache. // We call the onServiceWorkerUpdateReady API so users can show update prompts. apiRunner(`onServiceWorkerUpdateReady`, { serviceWorker: reg }) + + // If resources failed for the current page, reload. + if (window.___failedResources) { + console.log(`resources failed, SW updated - reloading`) + window.reload() + } } else { // At this point, everything has been precached. // It's the perfect time to display a "Content is cached for offline use." message. From 69a46968a058e4c7a4a146232a49472ec2b1f68e Mon Sep 17 00:00:00 2001 From: David Bailey Date: Tue, 1 Jan 2019 21:55:30 +0000 Subject: [PATCH 03/10] Restore precautionary code --- packages/gatsby-plugin-offline/sw-append.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-offline/sw-append.js b/packages/gatsby-plugin-offline/sw-append.js index 0d6c4a191d467..b32f301a40505 100644 --- a/packages/gatsby-plugin-offline/sw-append.js +++ b/packages/gatsby-plugin-offline/sw-append.js @@ -12,7 +12,23 @@ const navigationRoute = new workbox.routing.NavigationRoute(({ event }) => { const offlineShell = `%pathPrefix%/offline-plugin-app-shell-fallback/index.html` const cacheName = workbox.core.cacheNames.precache - return caches.match(offlineShell, { cacheName }) + return caches.match(offlineShell, { cacheName }).then(cachedResponse => { + if (!cachedResponse) { + // This shouldn't happen since we precache the offline shell, but just in case + return fetch(offlineShell).then(response => { + if (response.ok) { + return caches.open(cacheName).then(cache => + // Clone is needed because put() consumes the response body. + cache.put(offlineShell, response.clone()).then(() => response) + ) + } else { + return fetch(event.request) + } + }) + } + + return cachedResponse + }) } return fetch(event.request) From 6d3fbb16efcee3e1ea23c1eac06619f7074bdf27 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 9 Jan 2019 09:53:13 +0000 Subject: [PATCH 04/10] refactor: move code outside of if-block to decrease indentation + show console error --- packages/gatsby-plugin-offline/sw-append.js | 32 +++++++++++---------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/gatsby-plugin-offline/sw-append.js b/packages/gatsby-plugin-offline/sw-append.js index b32f301a40505..e4d296e70fc6d 100644 --- a/packages/gatsby-plugin-offline/sw-append.js +++ b/packages/gatsby-plugin-offline/sw-append.js @@ -13,21 +13,23 @@ const navigationRoute = new workbox.routing.NavigationRoute(({ event }) => { const cacheName = workbox.core.cacheNames.precache return caches.match(offlineShell, { cacheName }).then(cachedResponse => { - if (!cachedResponse) { - // This shouldn't happen since we precache the offline shell, but just in case - return fetch(offlineShell).then(response => { - if (response.ok) { - return caches.open(cacheName).then(cache => - // Clone is needed because put() consumes the response body. - cache.put(offlineShell, response.clone()).then(() => response) - ) - } else { - return fetch(event.request) - } - }) - } - - return cachedResponse + if (cachedResponse) return cachedResponse + + console.error( + `The offline shell (${offlineShell}) was not found ` + + `while attempting to serve a response for ${pathname}` + ) + + return fetch(offlineShell).then(response => { + if (response.ok) { + return caches.open(cacheName).then(cache => + // Clone is needed because put() consumes the response body. + cache.put(offlineShell, response.clone()).then(() => response) + ) + } else { + return fetch(event.request) + } + }) }) } From dc35c9ec761e7054e404975a1218ee8f6b9dfcc2 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 9 Jan 2019 10:04:57 +0000 Subject: [PATCH 05/10] refactor: use cpx to copy and watch sw-append-js --- packages/gatsby-plugin-offline/.gitignore | 1 - packages/gatsby-plugin-offline/package.json | 7 +- .../{ => src}/sw-append.js | 0 yarn.lock | 162 ++++++++++++------ 4 files changed, 114 insertions(+), 56 deletions(-) rename packages/gatsby-plugin-offline/{ => src}/sw-append.js (100%) diff --git a/packages/gatsby-plugin-offline/.gitignore b/packages/gatsby-plugin-offline/.gitignore index 10cf84f0d2dc8..8c9686624a187 100644 --- a/packages/gatsby-plugin-offline/.gitignore +++ b/packages/gatsby-plugin-offline/.gitignore @@ -1,4 +1,3 @@ /*.js !index.js -!sw-append.js yarn.lock diff --git a/packages/gatsby-plugin-offline/package.json b/packages/gatsby-plugin-offline/package.json index 5a704a1502f6e..46578fed0fc43 100644 --- a/packages/gatsby-plugin-offline/package.json +++ b/packages/gatsby-plugin-offline/package.json @@ -9,6 +9,7 @@ "dependencies": { "@babel/runtime": "^7.0.0", "cheerio": "^1.0.0-rc.2", + "cpx": "^1.5.0", "idb-keyval": "^3.1.0", "lodash": "^4.17.10", "workbox-build": "^3.6.3" @@ -34,8 +35,10 @@ }, "repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-offline", "scripts": { - "build": "babel src --out-dir . --ignore **/__tests__", + "build": "npm run build:src && npm run build:sw-append", + "build:src": "babel src --out-dir . --ignore **/__tests__", + "build:sw-append": "cpx -v src/sw-append.js .", "prepare": "cross-env NODE_ENV=production npm run build", - "watch": "babel -w src --out-dir . --ignore **/__tests__" + "watch": "npm run build:sw-append -- --watch & npm run build:src -- --watch" } } diff --git a/packages/gatsby-plugin-offline/sw-append.js b/packages/gatsby-plugin-offline/src/sw-append.js similarity index 100% rename from packages/gatsby-plugin-offline/sw-append.js rename to packages/gatsby-plugin-offline/src/sw-append.js diff --git a/yarn.lock b/yarn.lock index 725f0b8c46665..f799f7fad28c2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4678,7 +4678,7 @@ cheerio@^1.0.0-rc.2: lodash "^4.15.0" parse5 "^3.0.1" -chokidar@^1.4.2, chokidar@^1.7.0: +chokidar@^1.4.2, chokidar@^1.6.0, chokidar@^1.7.0: version "1.7.0" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468" integrity sha1-eY5ol3gVHIB2tLNg5e3SjNortGg= @@ -5574,6 +5574,23 @@ cosmiconfig@^5.0.0, cosmiconfig@^5.0.2, cosmiconfig@^5.0.5, cosmiconfig@^5.0.6: js-yaml "^3.9.0" parse-json "^4.0.0" +cpx@^1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/cpx/-/cpx-1.5.0.tgz#185be018511d87270dedccc293171e37655ab88f" + integrity sha1-GFvgGFEdhycN7czCkxceN2VauI8= + dependencies: + babel-runtime "^6.9.2" + chokidar "^1.6.0" + duplexer "^0.1.1" + glob "^7.0.5" + glob2base "^0.0.12" + minimatch "^3.0.2" + mkdirp "^0.5.1" + resolve "^1.1.7" + safe-buffer "^5.0.1" + shell-quote "^1.6.1" + subarg "^1.0.0" + crc-32@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/crc-32/-/crc-32-1.2.0.tgz#cb2db6e29b88508e32d9dd0ec1693e7b41a18208" @@ -6092,6 +6109,13 @@ debug@^3.0.1, debug@^3.1.0: dependencies: ms "^2.1.1" +debug@^3.2.5: + version "3.2.6" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" + integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ== + dependencies: + ms "^2.1.1" + debug@^4.0.1, debug@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.0.tgz#373687bffa678b38b1cd91f861b63850035ddc87" @@ -6401,7 +6425,7 @@ detect-newline@^2.1.0: resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2" integrity sha1-9B8cEL5LAOh7XxPaaAdZ8sW/0+I= -detect-node@^2.0.3: +detect-node@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c" integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw== @@ -7504,6 +7528,13 @@ eventsource@0.1.6: dependencies: original ">=0.0.5" +eventsource@^1.0.7: + version "1.0.7" + resolved "https://registry.yarnpkg.com/eventsource/-/eventsource-1.0.7.tgz#8fbc72c93fcd34088090bc0a4e64f4b5cee6d8d0" + integrity sha512-4Ln17+vVT0k8aWq+t/bF5arcS3EpT9gYtW66EPacdj/mAFevznsnyoHLPy2BA8gbIQeIHoPsvwmfBftfcG//BQ== + dependencies: + original "^1.0.0" + evp_bytestokey@^1.0.0, evp_bytestokey@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz#7fcbdb198dc71959432efe13842684e0525acb02" @@ -7879,7 +7910,7 @@ faye-websocket@^0.10.0, faye-websocket@~0.10.0: dependencies: websocket-driver ">=0.5.1" -faye-websocket@~0.11.0: +faye-websocket@~0.11.0, faye-websocket@~0.11.1: version "0.11.1" resolved "https://registry.yarnpkg.com/faye-websocket/-/faye-websocket-0.11.1.tgz#f0efe18c4f56e4f40afc7e06c719fd5ee6188f38" integrity sha1-8O/hjE9W5PQK/H4Gxxn9XuYYjzg= @@ -8092,6 +8123,11 @@ find-cache-dir@^2.0.0: make-dir "^1.0.0" pkg-dir "^3.0.0" +find-index@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/find-index/-/find-index-0.1.1.tgz#675d358b2ca3892d795a1ab47232f8b6e2e0dde4" + integrity sha1-Z101iyyjiS15Whq0cjL4tuLg3eQ= + find-parent-dir@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/find-parent-dir/-/find-parent-dir-0.3.0.tgz#33c44b429ab2b2f0646299c5f9f718f376ff8d54" @@ -8683,6 +8719,13 @@ glob-to-regexp@^0.3.0: resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab" integrity sha1-jFoUlNIGbFcMw7/kSWF1rMTVAqs= +glob2base@^0.0.12: + version "0.0.12" + resolved "https://registry.yarnpkg.com/glob2base/-/glob2base-0.0.12.tgz#9d419b3e28f12e83a362164a277055922c9c0d56" + integrity sha1-nUGbPijxLoOjYhZKJ3BVkiycDVY= + dependencies: + find-index "^0.1.1" + glob@6.0.4: version "6.0.4" resolved "https://registry.yarnpkg.com/glob/-/glob-6.0.4.tgz#0f08860f6a155127b2fadd4f9ce24b1aab6e4d22" @@ -9105,10 +9148,10 @@ gzip-size@3.0.0: dependencies: duplexer "^0.1.1" -handle-thing@^1.2.5: - version "1.2.5" - resolved "https://registry.yarnpkg.com/handle-thing/-/handle-thing-1.2.5.tgz#fd7aad726bf1a5fd16dfc29b2f7a6601d27139c4" - integrity sha1-/Xqtcmvxpf0W38KbL3pmAdJxOcQ= +handle-thing@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/handle-thing/-/handle-thing-2.0.0.tgz#0e039695ff50c93fc288557d696f3c1dc6776754" + integrity sha512-d4sze1JNC454Wdo2fkuyzCr6aHcbL6PGGuFAz0Li/NcOm1tCHGnWDRmJP85dh9IhQErTc2svWFEX5xHIOo//kQ== handlebars@^4.0.11: version "4.0.11" @@ -13653,7 +13696,7 @@ object.values@^1.0.4: function-bind "^1.1.0" has "^1.0.1" -obuf@^1.0.0, obuf@^1.1.1: +obuf@^1.0.0, obuf@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/obuf/-/obuf-1.1.2.tgz#09bea3343d41859ebd446292d11c9d4db619084e" integrity sha512-PX1wu0AmAdPqOL1mWhqmlOd8kOIZQwGZw6rh7uby9fTc5lhaOWFLX3I6R1hrF9k3zUY40e6igsLGkDXK92LJNg== @@ -13758,7 +13801,7 @@ ordered-read-streams@^1.0.0: dependencies: readable-stream "^2.0.1" -original@>=0.0.5: +original@>=0.0.5, original@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/original/-/original-1.0.2.tgz#e442a61cffe1c5fd20a65f3261c26663b303f25f" integrity sha512-hyBVl6iqqUOJ8FqRe+l/gS8H+kKYjrEndd5Pm1MfBtsEKA038HkkdbAl/72EAXGyonD/PFsvmVG+EvcIpliMBg== @@ -15627,7 +15670,7 @@ read@1, read@^1.0.7, read@~1.0.1: dependencies: mute-stream "~0.0.4" -"readable-stream@1 || 2", readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.4, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.2.9, readable-stream@^2.3.0, readable-stream@^2.3.3, readable-stream@^2.3.5, readable-stream@^2.3.6, readable-stream@~2.3.3: +"readable-stream@1 || 2", readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.4, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.3.0, readable-stream@^2.3.3, readable-stream@^2.3.5, readable-stream@^2.3.6, readable-stream@~2.3.3: version "2.3.6" resolved "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" integrity sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw== @@ -15663,6 +15706,15 @@ readable-stream@2.2.7: string_decoder "~1.0.0" util-deprecate "~1.0.1" +readable-stream@^3.0.6: + version "3.1.1" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.1.1.tgz#ed6bbc6c5ba58b090039ff18ce670515795aeb06" + integrity sha512-DkN66hPyqDhnIQ6Jcsvx9bFjhw214O4poMBcIMgPVpQvNy9a0e0Uhg5SqySyDKAmUlwt8LonTBz1ezOnM8pUdA== + dependencies: + inherits "^2.0.3" + string_decoder "^1.1.1" + util-deprecate "^1.0.1" + readable-stream@~2.0.0: version "2.0.6" resolved "http://registry.npmjs.org/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e" @@ -17121,17 +17173,17 @@ sockjs-client@1.1.4: json3 "^3.3.2" url-parse "^1.1.8" -sockjs-client@1.1.5: - version "1.1.5" - resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.1.5.tgz#1bb7c0f7222c40f42adf14f4442cbd1269771a83" - integrity sha1-G7fA9yIsQPQq3xT0RCy9Eml3GoM= +sockjs-client@1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.3.0.tgz#12fc9d6cb663da5739d3dc5fb6e8687da95cb177" + integrity sha512-R9jxEzhnnrdxLCNln0xg5uGHqMnkhPSTzUZH2eXcR03S/On9Yvoq2wyUZILRUhZCNVu2PmwWVoyuiPz8th8zbg== dependencies: - debug "^2.6.6" - eventsource "0.1.6" - faye-websocket "~0.11.0" - inherits "^2.0.1" + debug "^3.2.5" + eventsource "^1.0.7" + faye-websocket "~0.11.1" + inherits "^2.0.3" json3 "^3.3.2" - url-parse "^1.1.8" + url-parse "^1.4.3" sockjs@0.3.19: version "0.3.19" @@ -17276,30 +17328,28 @@ spdx-license-ids@^3.0.0: resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.1.tgz#e2a303236cac54b04031fa7a5a79c7e701df852f" integrity sha512-TfOfPcYGBB5sDuPn3deByxPhmfegAhpDYKSOXZQN81Oyrrif8ZCodOLzK3AesELnCx03kikhyDwh0pfvvQvF8w== -spdy-transport@^2.0.18: - version "2.1.0" - resolved "https://registry.yarnpkg.com/spdy-transport/-/spdy-transport-2.1.0.tgz#4bbb15aaffed0beefdd56ad61dbdc8ba3e2cb7a1" - integrity sha512-bpUeGpZcmZ692rrTiqf9/2EUakI6/kXX1Rpe0ib/DyOzbiexVfXkw6GnvI9hVGvIwVaUhkaBojjCZwLNRGQg1g== +spdy-transport@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/spdy-transport/-/spdy-transport-3.0.0.tgz#00d4863a6400ad75df93361a1608605e5dcdcf31" + integrity sha512-hsLVFE5SjA6TCisWeJXFKniGGOpBgMLmerfO2aCyCU5s7nJ/rpAepqmFifv/GCbSbueEeAJJnmSQ2rKC/g8Fcw== dependencies: - debug "^2.6.8" - detect-node "^2.0.3" + debug "^4.1.0" + detect-node "^2.0.4" hpack.js "^2.1.6" - obuf "^1.1.1" - readable-stream "^2.2.9" - safe-buffer "^5.0.1" - wbuf "^1.7.2" + obuf "^1.1.2" + readable-stream "^3.0.6" + wbuf "^1.7.3" -spdy@^3.4.1: - version "3.4.7" - resolved "https://registry.yarnpkg.com/spdy/-/spdy-3.4.7.tgz#42ff41ece5cc0f99a3a6c28aabb73f5c3b03acbc" - integrity sha1-Qv9B7OXMD5mjpsKKq7c/XDsDrLw= +spdy@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/spdy/-/spdy-4.0.0.tgz#81f222b5a743a329aa12cea6a390e60e9b613c52" + integrity sha512-ot0oEGT/PGUpzf/6uk4AWLqkq+irlqHXkrdbk51oWONh3bxQmBuljxPNl66zlRRcIJStWq0QkLUCPOPjgjvU0Q== dependencies: - debug "^2.6.8" - handle-thing "^1.2.5" + debug "^4.1.0" + handle-thing "^2.0.0" http-deceiver "^1.2.7" - safe-buffer "^5.0.1" select-hose "^2.0.0" - spdy-transport "^2.0.18" + spdy-transport "^3.0.0" specificity@^0.4.0: version "0.4.1" @@ -17639,6 +17689,13 @@ string_decoder@^1.0.0, string_decoder@~1.1.1: dependencies: safe-buffer "~5.1.0" +string_decoder@^1.1.1: + version "1.2.0" + resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.2.0.tgz#fe86e738b19544afe70469243b2a1ee9240eae8d" + integrity sha512-6YqyX6ZWEYguAxgZzHGL7SsCeGx3V2TtOTqZz1xSTSWnqsbWwbptafNyvf/ACquZUXV3DANr5BDIwNYe1mN42w== + dependencies: + safe-buffer "~5.1.0" + string_decoder@~1.0.0: version "1.0.3" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab" @@ -19016,7 +19073,7 @@ user-home@^1.1.1: resolved "https://registry.yarnpkg.com/user-home/-/user-home-1.1.1.tgz#2b5be23a32b63a7c9deb8d0f28d485724a3df190" integrity sha1-K1viOjK2Onyd640PKNSFcko98ZA= -util-deprecate@^1.0.2, util-deprecate@~1.0.1: +util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8= @@ -19300,7 +19357,7 @@ watchpack@^1.5.0: graceful-fs "^4.1.2" neo-async "^2.5.0" -wbuf@^1.1.0, wbuf@^1.7.2: +wbuf@^1.1.0, wbuf@^1.7.3: version "1.7.3" resolved "https://registry.yarnpkg.com/wbuf/-/wbuf-1.7.3.tgz#c1d8d149316d3ea852848895cb6a0bfe887b87df" integrity sha512-O84QOnr0icsbFGLS0O3bI5FswxzRr8/gHwWkDlQFskhSPryQXvrTMxjxGP4+iWYoauLoBvfDpkrOauZ+0iZpDA== @@ -19337,17 +19394,14 @@ webpack-assets-manifest@^3.0.2: tapable "^1.0.0" webpack-sources "^1.0.0" -webpack-dev-middleware@3.2.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/webpack-dev-middleware/-/webpack-dev-middleware-3.2.0.tgz#a20ceef194873710052da678f3c6ee0aeed92552" - integrity sha512-YJLMF/96TpKXaEQwaLEo+Z4NDK8aV133ROF6xp9pe3gQoS7sxfpXh4Rv9eC+8vCvWfmDjRQaMSlRPbO+9G6jgA== +webpack-dev-middleware@3.4.0: + version "3.4.0" + resolved "https://registry.yarnpkg.com/webpack-dev-middleware/-/webpack-dev-middleware-3.4.0.tgz#1132fecc9026fd90f0ecedac5cbff75d1fb45890" + integrity sha512-Q9Iyc0X9dP9bAsYskAVJ/hmIZZQwf/3Sy4xCAZgL5cUkjZmUZLt4l5HpbST/Pdgjn3u6pE7u5OdGd1apgzRujA== dependencies: - loud-rejection "^1.6.0" memory-fs "~0.4.1" mime "^2.3.1" - path-is-absolute "^1.0.0" range-parser "^1.0.3" - url-join "^4.0.0" webpack-log "^2.0.0" webpack-dev-middleware@^3.0.1: @@ -19362,10 +19416,10 @@ webpack-dev-middleware@^3.0.1: url-join "^4.0.0" webpack-log "^2.0.0" -webpack-dev-server@^3.1.1: - version "3.1.8" - resolved "https://registry.yarnpkg.com/webpack-dev-server/-/webpack-dev-server-3.1.8.tgz#eb7a95945d1108170f902604fb3b939533d9daeb" - integrity sha512-c+tcJtDqnPdxCAzEEZKdIPmg3i5i7cAHe+B+0xFNK0BlCc2HF/unYccbU7xTgfGc5xxhCztCQzFmsqim+KhI+A== +webpack-dev-server@^3.1.14: + version "3.1.14" + resolved "https://registry.yarnpkg.com/webpack-dev-server/-/webpack-dev-server-3.1.14.tgz#60fb229b997fc5a0a1fc6237421030180959d469" + integrity sha512-mGXDgz5SlTxcF3hUpfC8hrQ11yhAttuUQWf1Wmb+6zo3x6rb7b9mIfuQvAPLdfDRCGRGvakBWHdHOa0I9p/EVQ== dependencies: ansi-html "0.0.7" bonjour "^3.5.0" @@ -19386,13 +19440,15 @@ webpack-dev-server@^3.1.1: portfinder "^1.0.9" schema-utils "^1.0.0" selfsigned "^1.9.1" + semver "^5.6.0" serve-index "^1.7.2" sockjs "0.3.19" - sockjs-client "1.1.5" - spdy "^3.4.1" + sockjs-client "1.3.0" + spdy "^4.0.0" strip-ansi "^3.0.0" supports-color "^5.1.0" - webpack-dev-middleware "3.2.0" + url "^0.11.0" + webpack-dev-middleware "3.4.0" webpack-log "^2.0.0" yargs "12.0.2" From a558daff5acbc2127f2786cd07be24e1be62ac90 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 9 Jan 2019 17:09:33 +0000 Subject: [PATCH 06/10] fix: window.reload() -> window.location.reload() Co-Authored-By: davidbailey00 <4248177+davidbailey00@users.noreply.github.com> --- packages/gatsby/cache-dir/register-service-worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/cache-dir/register-service-worker.js b/packages/gatsby/cache-dir/register-service-worker.js index b5a666660b785..bf06000871a22 100644 --- a/packages/gatsby/cache-dir/register-service-worker.js +++ b/packages/gatsby/cache-dir/register-service-worker.js @@ -29,7 +29,7 @@ if ( // If resources failed for the current page, reload. if (window.___failedResources) { console.log(`resources failed, SW updated - reloading`) - window.reload() + window.location.reload() } } else { // At this point, everything has been precached. From 91bd2d82bbaf581df6af3283f0f7ea9b79e215ef Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 11 Jan 2019 19:55:52 +0000 Subject: [PATCH 07/10] Ignore compiling sw-append.js with Babel --- packages/gatsby-plugin-offline/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-offline/package.json b/packages/gatsby-plugin-offline/package.json index 46578fed0fc43..d585902090acf 100644 --- a/packages/gatsby-plugin-offline/package.json +++ b/packages/gatsby-plugin-offline/package.json @@ -36,7 +36,7 @@ "repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-offline", "scripts": { "build": "npm run build:src && npm run build:sw-append", - "build:src": "babel src --out-dir . --ignore **/__tests__", + "build:src": "babel src --out-dir . --ignore **/__tests__,src/sw-append.js", "build:sw-append": "cpx -v src/sw-append.js .", "prepare": "cross-env NODE_ENV=production npm run build", "watch": "npm run build:sw-append -- --watch & npm run build:src -- --watch" From 8ca31d4c170ad259efd14a10806e8e31f6cec39f Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 25 Jan 2019 20:10:03 +0000 Subject: [PATCH 08/10] Restore previous behaviour --- packages/gatsby/cache-dir/navigation.js | 7 +++++++ packages/gatsby/cache-dir/register-service-worker.js | 3 +++ 2 files changed, 10 insertions(+) diff --git a/packages/gatsby/cache-dir/navigation.js b/packages/gatsby/cache-dir/navigation.js index eb905460c1e42..af030a25253e9 100644 --- a/packages/gatsby/cache-dir/navigation.js +++ b/packages/gatsby/cache-dir/navigation.js @@ -65,6 +65,13 @@ const navigate = (to, options = {}) => { pathname = parsePath(to).pathname } + // If we had a service worker update, no matter the path, reload window and + // reset the pathname whitelist + if (window.___swUpdated) { + window.location = pathname + return + } + // Start a timer to wait for a second before transitioning and showing a // loader in case resources aren't around yet. const timeoutId = setTimeout(() => { diff --git a/packages/gatsby/cache-dir/register-service-worker.js b/packages/gatsby/cache-dir/register-service-worker.js index bf06000871a22..acb6347b450b0 100644 --- a/packages/gatsby/cache-dir/register-service-worker.js +++ b/packages/gatsby/cache-dir/register-service-worker.js @@ -23,6 +23,9 @@ if ( if (navigator.serviceWorker.controller) { // At this point, the old content will have been purged and the fresh content will // have been added to the cache. + + // We set a flag so Gatsby Link knows to refresh the page on next navigation attempt + window.___swUpdated = true // We call the onServiceWorkerUpdateReady API so users can show update prompts. apiRunner(`onServiceWorkerUpdateReady`, { serviceWorker: reg }) From 97db1d099d7a15ba02dc52c9e1b467d85e808102 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sun, 27 Jan 2019 13:13:17 +0000 Subject: [PATCH 09/10] Prevent whitelisting/caching old pages/resources --- packages/gatsby-plugin-offline/src/gatsby-browser.js | 12 ++++++++++-- yarn.lock | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index 9e10d34639ad6..a9ee410438ff9 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -7,8 +7,12 @@ exports.onServiceWorkerActive = ({ getResourceURLsForPathname, serviceWorker, }) => { - // reset whitelisted paths - serviceWorker.active.postMessage({ gatsbyApi: `resetWhitelist` }) + // if the SW has just updated then reset whitelisted paths and don't cache + // stuff, since we're on the old revision until we navigate to another page + if (window.___swUpdated) { + serviceWorker.active.postMessage({ gatsbyApi: `resetWhitelist` }) + return + } // grab nodes from head of document const nodes = document.querySelectorAll(` @@ -66,6 +70,10 @@ function whitelistPathname(pathname, includesPrefix) { } exports.onPostPrefetchPathname = ({ pathname }) => { + // do nothing if the SW has just updated, since we still have old pages in + // memory which we don't want to be whitelisted + if (window.___swUpdated) return + whitelistPathname(pathname, false) // if SW is not installed, we need to record any prefetches diff --git a/yarn.lock b/yarn.lock index 43701499a10a1..ac803d3c20173 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3640,7 +3640,7 @@ babel-register@^6.26.0, babel-register@^6.9.0: mkdirp "^0.5.1" source-map-support "^0.4.15" -babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0, babel-runtime@^6.23.0, babel-runtime@^6.26.0: +babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0, babel-runtime@^6.23.0, babel-runtime@^6.26.0, babel-runtime@^6.9.2: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" integrity sha1-llxwWGaOgrVde/4E/yM3vItWR/4= From 3665102f716882a553ac916899f1f2d8d5935fcc Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Mon, 28 Jan 2019 17:40:42 +0100 Subject: [PATCH 10/10] update peer dep to version that will be released --- packages/gatsby-plugin-offline/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-offline/package.json b/packages/gatsby-plugin-offline/package.json index 0d1c7f6aefae0..433db8a0fa786 100644 --- a/packages/gatsby-plugin-offline/package.json +++ b/packages/gatsby-plugin-offline/package.json @@ -31,7 +31,7 @@ "license": "MIT", "main": "index.js", "peerDependencies": { - "gatsby": ">=2.0.53" + "gatsby": "^2.0.100" }, "repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-offline", "scripts": {