Skip to content

Commit

Permalink
feat(gatsby-plugin-offline): Allow configuring Workbox debug m… (#18123)
Browse files Browse the repository at this point in the history
* Implement initial feature

* improve reliability of regex

* add test + fix default workboxConfig option

* add docs

* Fix unit tests

* fix unit tests (take 2)
  • Loading branch information
vtenfys authored and m-allanson committed Oct 10, 2019
1 parent e99b961 commit 3c18b9f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/gatsby-plugin-offline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ describe(`onPostBuild`, () => {
swText += text
},

writeFileSync(file, text) {
swText = text
},

createReadStream() {
return { pipe() {} }
},
Expand Down Expand Up @@ -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`)
})
})
17 changes: 16 additions & 1 deletion packages/gatsby-plugin-offline/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3c18b9f

Please sign in to comment.