Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-offline): reload when missing resources and SW was updated + add "onServiceWorkerUpdateReady" API #10432

Merged
merged 18 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions packages/gatsby-plugin-offline/sw-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's safe to remove that yet. This handles stuff before runtime actually run, so we can't really rely that any changes we make in runtime are actually in effect (not to mention that people can use "missmatched" versions of gatsby and offline plugin in this could "brick" their sites still in those still not figured out scenarios)

}

return fetch(event.request)
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/cache-dir/ensure-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class EnsureResources extends React.Component {

render() {
if (!this.hasResources(this.state.pageResources) && isInitialRender) {
window.___failedResources = true
pieh marked this conversation as resolved.
Show resolved Hide resolved

// prevent hydrating
throw new Error(`Missing resources for ${this.state.location.pathname}`)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby/cache-dir/register-service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
pieh marked this conversation as resolved.
Show resolved Hide resolved
window.reload()
vtenfys marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
Expand Down