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

fix(gatsby-plugin-offline): Improve reliability of JS detection #18760

Merged
merged 6 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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:.+/)) {
vtenfys marked this conversation as resolved.
Show resolved Hide resolved
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)
vtenfys marked this conversation as resolved.
Show resolved Hide resolved