Skip to content

Commit 19e9f3e

Browse files
authored
fix(gatsby-plugin-offline): gracefully degrade if appshell isn't precached (#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.
1 parent 8ccd222 commit 19e9f3e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

packages/gatsby-plugin-offline/src/sw-append.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ const navigationRoute = new workbox.routing.NavigationRoute(({ event }) => {
1212
const offlineShell = `%pathPrefix%/offline-plugin-app-shell-fallback/index.html`
1313
const cacheName = workbox.core.cacheNames.precache
1414

15-
return caches.match(offlineShell, { cacheName })
15+
return caches.match(offlineShell, { cacheName }).then(cachedResponse => {
16+
if (!cachedResponse) {
17+
return fetch(offlineShell).then(response => {
18+
if (response.ok) {
19+
return caches.open(cacheName).then(cache =>
20+
// Clone is needed because put() consumes the response body.
21+
cache.put(offlineShell, response.clone()).then(() => response)
22+
)
23+
} else {
24+
return fetch(event.request)
25+
}
26+
})
27+
}
28+
29+
return cachedResponse
30+
})
1631
}
1732

1833
return fetch(event.request)

0 commit comments

Comments
 (0)