Skip to content

Commit

Permalink
fix(gatsby-plugin-offline): Improve reliability of JS detection (#18760)
Browse files Browse the repository at this point in the history
* Combine offline API with redirection to improve reliability over preload

* fix API route not working for navigation requests + make code order more logical

* check if API method exists before calling function

* add comments to relevant sections
  • Loading branch information
vtenfys authored and GatsbyJS Bot committed Nov 4, 2019
1 parent 4d5d780 commit ae6eab3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 37 deletions.
8 changes: 3 additions & 5 deletions packages/gatsby-plugin-offline/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ export const onRenderBody = ({ pathname, setHeadComponents }) => {

setHeadComponents([
<noscript key="disable-offline-shell">
<link
rel="preload"
as="fetch"
href="/.gatsby-plugin-offline:disableOfflineShell"
<meta
httpEquiv="refresh"
content="0;url=/.gatsby-plugin-offline:api=disableOfflineShell&redirect=true"
/>
<meta httpEquiv="refresh" content="0;url=" />
</noscript>,
])
}
99 changes: 67 additions & 32 deletions packages/gatsby-plugin-offline/src/sw-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,78 @@
importScripts(`idb-keyval-iife.min.js`)

const { NavigationRoute } = workbox.routing

let lastNavigationRequest = null
let offlineShellEnabled = true

// prefer standard object syntax to support more browsers
const MessageAPI = {
setPathResources: (event, { path, resources }) => {
event.waitUntil(idbKeyval.set(`resources:${path}`, resources))
},

clearPathResources: event => {
event.waitUntil(idbKeyval.clear())
},

enableOfflineShell: () => {
offlineShellEnabled = true
},

disableOfflineShell: () => {
offlineShellEnabled = false
},
}

self.addEventListener(`message`, event => {
const { gatsbyApi: api } = event.data
if (api) MessageAPI[api](event, event.data)
})

function handleAPIRequest({ event }) {
const { pathname } = new URL(event.request.url)

const params = pathname.match(/:(.+)/)[1]
const data = {}

if (params.indexOf(`=`) !== -1) {
params.split(`&`).forEach(param => {
const [key, val] = param.split(`=`)
data[key] = val
})
} else {
data.api = params
}

if (MessageAPI[data.api] !== undefined) {
MessageAPI[data.api]()
}

if (!data.redirect) {
return new Response()
}

return new Response(null, {
status: 302,
headers: {
Location: lastNavigationRequest,
},
})
}

const navigationRoute = new NavigationRoute(async ({ event }) => {
// handle API requests separately to normal navigation requests, so do this
// check first
if (event.request.url.match(/\/.gatsby-plugin-offline:.+/)) {
return handleAPIRequest({ event })
}

if (!offlineShellEnabled) {
return await fetch(event.request)
}

lastNavigationRequest = event.request.url

let { pathname } = new URL(event.request.url)
pathname = pathname.replace(new RegExp(`^%pathPrefix%`), ``)

Expand All @@ -36,35 +101,5 @@ const navigationRoute = new NavigationRoute(async ({ event }) => {

workbox.routing.registerRoute(navigationRoute)

// prefer standard object syntax to support more browsers
const MessageAPI = {
setPathResources: (event, { path, resources }) => {
event.waitUntil(idbKeyval.set(`resources:${path}`, resources))
},

clearPathResources: event => {
event.waitUntil(idbKeyval.clear())
},

enableOfflineShell: () => {
offlineShellEnabled = true
},

disableOfflineShell: () => {
offlineShellEnabled = false
},
}

self.addEventListener(`message`, event => {
const { gatsbyApi: api } = event.data
if (api) MessageAPI[api](event, event.data)
})

workbox.routing.registerRoute(/\/.gatsby-plugin-offline:.+/, ({ event }) => {
const { pathname } = new URL(event.request.url)

const api = pathname.match(/:(.+)/)[1]
MessageAPI[api]()

return new Response()
})
// this route is used when performing a non-navigation request (e.g. fetch)
workbox.routing.registerRoute(/\/.gatsby-plugin-offline:.+/, handleAPIRequest)

0 comments on commit ae6eab3

Please sign in to comment.