Skip to content

Commit

Permalink
fix(gatsby-plugin-offline): gracefully degrade if appshell isn't prec…
Browse files Browse the repository at this point in the history
…ached (#10329)

If app shell is not in precache (for whatever reason) - try to fetch it. If we fetch successfully - put it in precache for any future requests so we can use happy path again. If we can't fetch app shell - fallback to fetching actual html content of the page.
  • Loading branch information
pieh committed Dec 6, 2018
1 parent 8ccd222 commit 19e9f3e
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/gatsby-plugin-offline/src/sw-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,22 @@ 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) {
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)
Expand Down

0 comments on commit 19e9f3e

Please sign in to comment.