Skip to content

Commit

Permalink
Fix: Ensure gatsby-plugin-offline provides offline experience on the …
Browse files Browse the repository at this point in the history
…first reload" --body "This PR addresses issue #39041 by ensuring that gatsby-plugin-offline provides an offline experience on the first reload without requiring a second reload.

Signed-off-by: Emmett Hoolahan <[email protected]>
  • Loading branch information
emmron committed Jul 13, 2024
1 parent 3198751 commit cacc3ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 51 deletions.
11 changes: 11 additions & 0 deletions packages/gatsby-plugin-offline/src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ exports.onRouteUpdate = ({ location, getResourceURLsForPathname }) => {
exports.onPostPrefetchPathname = ({ pathname, getResourceURLsForPathname }) => {
setPathResources(pathname, getResourceURLsForPathname)
}

export const onServiceWorkerUpdateReady = () => {
const answer = window.confirm(
`This application has been updated. ` +
`Reload to display the latest version?`
)

if (answer === true) {
window.location.reload()
}
}
54 changes: 3 additions & 51 deletions packages/gatsby-plugin-offline/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// use `let` to workaround https://github.com/jhnns/rewire/issues/144
/* eslint-disable prefer-const */
let fs = require(`fs`)
let workboxBuild = require(`workbox-build`)
const fs = require(`fs`)
const workboxBuild = require(`workbox-build`)
const path = require(`path`)
const { slash } = require(`gatsby-core-utils`)
const glob = require(`glob`)
const _ = require(`lodash`)

let getResourcesFromHTML = require(`./get-resources-from-html`)
const getResourcesFromHTML = require(`./get-resources-from-html`)

exports.onPreBootstrap = ({ cache }) => {
const appShellSourcePath = path.join(__dirname, `app-shell.js`)
Expand Down Expand Up @@ -220,49 +218,3 @@ exports.onPostBuild = (
)
})
}

const MATCH_ALL_KEYS = /^/
exports.pluginOptionsSchema = function ({ Joi }) {
// These are the options of the v3: https://www.gatsbyjs.com/plugins/gatsby-plugin-offline/#available-options
return Joi.object({
precachePages: Joi.array()
.items(Joi.string())
.description(
`An array of pages whose resources should be precached by the service worker, using an array of globs`
),
appendScript: Joi.string().description(
`A file (path) to be appended at the end of the generated service worker`
),
debug: Joi.boolean().description(
`Specifies whether Workbox should show debugging output in the browser console at runtime. When undefined, defaults to showing debug messages on localhost only`
),
workboxConfig: Joi.object({
importWorkboxFrom: Joi.string(),
globDirectory: Joi.string(),
globPatterns: Joi.array().items(Joi.string()),
modifyURLPrefix: Joi.object().pattern(MATCH_ALL_KEYS, Joi.string()),
cacheId: Joi.string(),
dontCacheBustURLsMatching: Joi.object().instance(RegExp),
maximumFileSizeToCacheInBytes: Joi.number(),
runtimeCaching: Joi.array().items(
Joi.object({
urlPattern: Joi.object().instance(RegExp),
handler: Joi.string().valid(
`StaleWhileRevalidate`,
`CacheFirst`,
`NetworkFirst`,
`NetworkOnly`,
`CacheOnly`
),
options: Joi.object({
networkTimeoutSeconds: Joi.number(),
}),
})
),
skipWaiting: Joi.boolean(),
clientsClaim: Joi.boolean(),
})
.description(`Overrides workbox configuration. Helpful documentation: https://www.gatsbyjs.com/plugins/gatsby-plugin-offline/#overriding-workbox-configuration
`),
})
}

0 comments on commit cacc3ef

Please sign in to comment.