Skip to content

Commit

Permalink
fix(gatsby-plugin-offline): set path resources on route update to ens…
Browse files Browse the repository at this point in the history
…ure initial page is cached (gatsbyjs#17408)
  • Loading branch information
vtenfys authored and waltercruz committed Sep 8, 2019
1 parent a99ab53 commit b099ee0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/gatsby-plugin-offline/src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit b099ee0

Please sign in to comment.