From bcb18599e4ff81ddb57f4821b43824638e0f01c5 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 5 Sep 2019 11:32:28 +0100 Subject: [PATCH 1/3] Set path resources on route update (for initial page load) --- .../src/gatsby-browser.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index 00f00998ae6d7..f9a9ffdd6ce44 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -55,7 +55,7 @@ exports.onServiceWorkerActive = ({ }) } -exports.onPostPrefetchPathname = ({ pathname, getResourceURLsForPathname }) => { +function setPathResources(path, getResourceURLsForPathname) { // 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 @@ -66,13 +66,23 @@ exports.onPostPrefetchPathname = ({ pathname, getResourceURLsForPathname }) => { if (serviceWorker.controller === null) { // if SW is not installed, we need to record any prefetches // that happen so we can then add them to SW cache once installed - prefetchedPathnames.push(pathname) + prefetchedPathnames.push(path) } else { + const resources = getResourceURLsForPathname(path) serviceWorker.controller.postMessage({ gatsbyApi: `setPathResources`, - path: pathname, - resources: getResourceURLsForPathname(pathname), + path, + resources, }) } } } + +exports.onRouteUpdate = ({ location, getResourceURLsForPathname }) => { + const pathname = location.pathname.replace(__PATH_PREFIX__, ``) + setPathResources(pathname, getResourceURLsForPathname) +} + +exports.onPostPrefetchPathname = ({ pathname, getResourceURLsForPathname }) => { + setPathResources(pathname, getResourceURLsForPathname) +} From 9ab96361b37588826944f95ff3d9c3cbd6725634 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 6 Sep 2019 15:00:00 +0100 Subject: [PATCH 2/3] use base path instead of path prefix --- packages/gatsby-plugin-offline/src/gatsby-browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index f9a9ffdd6ce44..4d63601980ebb 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -79,7 +79,7 @@ function setPathResources(path, getResourceURLsForPathname) { } exports.onRouteUpdate = ({ location, getResourceURLsForPathname }) => { - const pathname = location.pathname.replace(__PATH_PREFIX__, ``) + const pathname = location.pathname.replace(__BASE_PATH__, ``) setPathResources(pathname, getResourceURLsForPathname) } From 50b0205b98e516ac5e960d0f67fbeb42ae041b43 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 6 Sep 2019 15:01:53 +0100 Subject: [PATCH 3/3] fix min gatsby version --- 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 9926abcafbb98..2c4ef8d3847bc 100644 --- a/packages/gatsby-plugin-offline/package.json +++ b/packages/gatsby-plugin-offline/package.json @@ -34,7 +34,7 @@ "license": "MIT", "main": "index.js", "peerDependencies": { - "gatsby": "^2.0.100" + "gatsby": "^2.4.0" }, "repository": { "type": "git",