Skip to content

Commit 3c18b9f

Browse files
vtenfysm-allanson
authored andcommitted
feat(gatsby-plugin-offline): Allow configuring Workbox debug m… (#18123)
* Implement initial feature * improve reliability of regex * add test + fix default workboxConfig option * add docs * Fix unit tests * fix unit tests (take 2)
1 parent e99b961 commit 3c18b9f

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

packages/gatsby-plugin-offline/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ In `gatsby-plugin-offline` 3.x, the following options are available:
6767
workbox.routing.registerRoute(customRoute)
6868
```
6969

70+
- `debug` specifies whether Workbox should show debugging output in the browser console at runtime. When undefined, defaults to showing debug messages on `localhost` only.
71+
7072
- `workboxConfig` allows you to override the default Workbox options - see [Overriding Workbox configuration](#overriding-workbox-configuration). For example:
7173

7274
```javascript:title=gatsby-config.js

packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js

+22
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ describe(`onPostBuild`, () => {
5151
swText += text
5252
},
5353

54+
writeFileSync(file, text) {
55+
swText = text
56+
},
57+
5458
createReadStream() {
5559
return { pipe() {} }
5660
},
@@ -84,4 +88,22 @@ describe(`onPostBuild`, () => {
8488

8589
expect(swText).toContain(`console.log(\`Hello, world!\`)`)
8690
})
91+
92+
it(`configures the Workbox debug option`, async () => {
93+
swText = `workbox.setConfig({modulePathPrefix: "workbox-v4.3.1"});`
94+
95+
await gatsbyNode.onPostBuild(
96+
{
97+
pathPrefix: ``,
98+
reporter: {
99+
info(message) {
100+
console.log(message)
101+
},
102+
},
103+
},
104+
{ debug: true }
105+
)
106+
107+
expect(swText).toContain(`debug: true`)
108+
})
87109
})

packages/gatsby-plugin-offline/src/gatsby-node.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ function getPrecachePages(globs, base) {
6565

6666
exports.onPostBuild = (
6767
args,
68-
{ precachePages: precachePagesGlobs = [], appendScript = null, workboxConfig }
68+
{
69+
precachePages: precachePagesGlobs = [],
70+
appendScript = null,
71+
debug = undefined,
72+
workboxConfig = {},
73+
}
6974
) => {
7075
const { pathPrefix, reporter } = args
7176
const rootDir = `public`
@@ -162,6 +167,16 @@ exports.onPostBuild = (
162167
.then(({ count, size, warnings }) => {
163168
if (warnings) warnings.forEach(warning => console.warn(warning))
164169

170+
if (debug !== undefined) {
171+
const swText = fs
172+
.readFileSync(swDest, `utf8`)
173+
.replace(
174+
/(workbox\.setConfig\({modulePathPrefix: "[^"]+")}\);/,
175+
`$1, debug: ${JSON.stringify(debug)}});`
176+
)
177+
fs.writeFileSync(swDest, swText)
178+
}
179+
165180
const swAppend = fs
166181
.readFileSync(`${__dirname}/sw-append.js`, `utf8`)
167182
.replace(/%pathPrefix%/g, pathPrefix)

0 commit comments

Comments
 (0)