diff --git a/packages/gatsby-plugin-offline/README.md b/packages/gatsby-plugin-offline/README.md index c983b10f45b41..b8a62e7ce3d73 100644 --- a/packages/gatsby-plugin-offline/README.md +++ b/packages/gatsby-plugin-offline/README.md @@ -67,6 +67,8 @@ In `gatsby-plugin-offline` 3.x, the following options are available: workbox.routing.registerRoute(customRoute) ``` +- `debug` specifies whether Workbox should show debugging output in the browser console at runtime. When undefined, defaults to showing debug messages on `localhost` only. + - `workboxConfig` allows you to override the default Workbox options - see [Overriding Workbox configuration](#overriding-workbox-configuration). For example: ```javascript:title=gatsby-config.js diff --git a/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js index 35601edcb6a8a..af7103cd689ca 100644 --- a/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js @@ -51,6 +51,10 @@ describe(`onPostBuild`, () => { swText += text }, + writeFileSync(file, text) { + swText = text + }, + createReadStream() { return { pipe() {} } }, @@ -84,4 +88,22 @@ describe(`onPostBuild`, () => { expect(swText).toContain(`console.log(\`Hello, world!\`)`) }) + + it(`configures the Workbox debug option`, async () => { + swText = `workbox.setConfig({modulePathPrefix: "workbox-v4.3.1"});` + + await gatsbyNode.onPostBuild( + { + pathPrefix: ``, + reporter: { + info(message) { + console.log(message) + }, + }, + }, + { debug: true } + ) + + expect(swText).toContain(`debug: true`) + }) }) diff --git a/packages/gatsby-plugin-offline/src/gatsby-node.js b/packages/gatsby-plugin-offline/src/gatsby-node.js index f4b92e3e9a5d7..7319ce9f42211 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-node.js +++ b/packages/gatsby-plugin-offline/src/gatsby-node.js @@ -65,7 +65,12 @@ function getPrecachePages(globs, base) { exports.onPostBuild = ( args, - { precachePages: precachePagesGlobs = [], appendScript = null, workboxConfig } + { + precachePages: precachePagesGlobs = [], + appendScript = null, + debug = undefined, + workboxConfig = {}, + } ) => { const { pathPrefix, reporter } = args const rootDir = `public` @@ -162,6 +167,16 @@ exports.onPostBuild = ( .then(({ count, size, warnings }) => { if (warnings) warnings.forEach(warning => console.warn(warning)) + if (debug !== undefined) { + const swText = fs + .readFileSync(swDest, `utf8`) + .replace( + /(workbox\.setConfig\({modulePathPrefix: "[^"]+")}\);/, + `$1, debug: ${JSON.stringify(debug)}});` + ) + fs.writeFileSync(swDest, swText) + } + const swAppend = fs .readFileSync(`${__dirname}/sw-append.js`, `utf8`) .replace(/%pathPrefix%/g, pathPrefix)