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

feat(gatsby-plugin-offline): Allow appending custom scripts to sw.js #11626

Merged
merged 21 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
77 changes: 72 additions & 5 deletions packages/gatsby-plugin-offline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,80 @@ in the service worker.
plugins: [`gatsby-plugin-offline`]
```

## Overriding options
## Available options

When adding this plugin to your `gatsby-config.js`, you can pass in options to
override the default [Workbox](https://developers.google.com/web/tools/workbox/modules/workbox-build) config.
As of `gatsby-plugin-offline` 3.0.0, the following options are available:

The default config is as follows. Warning: you can break the offline support by
changing these options, so tread carefully.
- `workboxConfig` allows you to override the default Workbox options - see [Overriding Workbox configuration](#overriding-workbox-configuration)
- `injectScript` lets you specify a file to be injected into the end of the generated service worker (`sw.js`)
vtenfys marked this conversation as resolved.
Show resolved Hide resolved

For example:

```javascript:title=gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-offline`,
options: {
workboxConfig: {
importWorkboxFrom: `cdn`,
},
injectScript: `src/custom-sw-code.js`,
},
},
]
```

```javascript:title=src/custom-sw-code.js
// show a notification after 15 seconds (the notification
// permission must be granted first)
setTimeout(() => {
self.registration.showNotification("Hello, world!")
}, 15000)

// register a custom navigation route
const customRoute = new workbox.routing.NavigationRoute(({ event }) => {
// ...
})
workbox.routing.registerRoute(customRoute)
```

## Upgrading from 2.x

To upgrade from 2.x to 3.x, move any existing options into the `workboxConfig` option. If you haven't specified any options, you have nothing to do.

For example, here is a 2.x config:

```javascript
plugins: [
{
resolve: `gatsby-plugin-offline`,
options: {
importWorkboxFrom: `cdn`,
},
},
]
```

Here is the equivalent 3.x config:

```javascript
plugins: [
{
resolve: `gatsby-plugin-offline`,
options: {
workboxConfig: {
importWorkboxFrom: `cdn`,
},
},
},
]
```

## Overriding Workbox configuration

When adding this plugin to your `gatsby-config.js`, you can use the option `workboxConfig` to override the default Workbox config. To see the full list of options, see [this article on Google Developers](https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config).

The default `workboxConfig` is as follows. Note that some of these options are configured automatically, e.g. `globPatterns`. If you're not sure about what all of these options mean, it's best to leave them as-is - otherwise, you may end up causing errors on your site, causing old files to be remain cached, or even breaking offline support.

```javascript
const options = {
Expand Down
16 changes: 10 additions & 6 deletions packages/gatsby-plugin-offline/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ exports.onPostBuild = (args, pluginOptions) => {
clientsClaim: true,
}

// pluginOptions.plugins is assigned automatically when the user hasn't
// specified custom options - Workbox throws an error with unsupported
// parameters, so delete it.
delete pluginOptions.plugins
vtenfys marked this conversation as resolved.
Show resolved Hide resolved
const combinedOptions = _.defaults(pluginOptions, options)
const combinedOptions = {
...options,
...pluginOptions.workboxConfig,
}

const idbKeyvalFile = `idb-keyval-iife.min.js`
const idbKeyvalSource = require.resolve(`idb-keyval/dist/${idbKeyvalFile}`)
Expand All @@ -125,8 +124,13 @@ exports.onPostBuild = (args, pluginOptions) => {
const swAppend = fs
.readFileSync(`${__dirname}/sw-append.js`, `utf8`)
.replace(/%pathPrefix%/g, pathPrefix)

fs.appendFileSync(`public/sw.js`, `\n` + swAppend)

if (pluginOptions.injectScript) {
vtenfys marked this conversation as resolved.
Show resolved Hide resolved
const userAppend = fs.readFileSync(pluginOptions.injectScript, `utf8`)
fs.appendFileSync(`public/sw.js`, `\n` + userAppend)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of wrapping this in a try catch here and printing a friendly error if the file doesn't exist? Definitely not a blocker for this PR though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'll implement this change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, just done this!

}

console.log(
`Generated ${swDest}, which will precache ${count} files, totaling ${size} bytes.`
)
Expand Down