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: Use the Workbox webpack plugin in pwa template #769

Merged
merged 2 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 44 additions & 0 deletions packages/@vue/cli-plugin-pwa/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
# @vue/cli-plugin-pwa

> pwa plugin for vue-cli

## Configuration

Configuration is handled via the `pwa` property of either the `vue.config.js`
file, or the `"vue"` field in `package.json`.

### pwa.workboxPluginMode

This allows you to the choose between the two modes supported by the underlying
[`workbox-webpack-plugin`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin).

- `'GenerateSW'` (default), will lead to a new service worker file being created
each time you rebuild your web app.

- `'InjectManifest'` allows you to start with an existing service worker file,
and creates a copy of that file with a "precache manifest" injected into it.

The "[Which Plugin to Use?](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#which_plugin_to_use)"
guide can help you choose between the two modes.

### pwa.workboxOptions

These options are passed on through to the underlying `workbox-webpack-plugin`.

For more information on what values are supported, please see the guide for
[`GenerateSW`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#full_generatesw_config)
or for [`InjectManifest`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#full_injectmanifest_config).

### Example Configuration

```js
// Inside vue.config.js
module.exports = {
// ...other vue-cli plugin options...
pwa: {
workboxPluginMode: 'InjectManifest',
workboxOptions: {
// swSrc is required in InjectManifest mode.
swSrc: 'dev/sw.js',
// ...other Workbox options...
},
},
};
```
34 changes: 24 additions & 10 deletions packages/@vue/cli-plugin-pwa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,39 @@ module.exports = (api, options) => {
api.chainWebpack(webpackConfig => {
const name = api.service.pkg.name

const userOptions = options.pwa || {}

// the pwa plugin hooks on to html-webpack-plugin
// and injects icons, manifest links & other PWA related tags into <head>
webpackConfig
.plugin('pwa')
.use(require('./lib/HtmlPwaPlugin'), [Object.assign({
name
}, options.pwa)])
}, userOptions)])

// generate /service-worker.js in production mode
if (process.env.NODE_ENV === 'production') {
webpackConfig
.plugin('sw-precache')
.use(require('sw-precache-webpack-plugin'), [{
cacheId: name,
filename: 'service-worker.js',
staticFileGlobs: [`${options.outputDir}/**/*.{js,html,css}`],
minify: true,
stripPrefix: `${options.outputDir}/`
}])
// Default to GenerateSW mode, though InjectManifest also might be used.
const workboxPluginMode = userOptions.workboxPluginMode || 'GenerateSW'
const workboxWebpackModule = require('workbox-webpack-plugin')
if (workboxPluginMode in workboxWebpackModule) {
const workBoxConfig = Object.assign({
cacheId: name,
exclude: [
new RegExp('\.map$'),
new RegExp('img/icons/'),
new RegExp('favicon\.ico$'),
new RegExp('manifest\.json$')
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the default exclude is [/\.map$/, /^manifest.*\.js(?:on)?$/] - any reason to exclude the icons and favicon as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The request that a browser makes for either a registered favicon or for the home screen icon bypasses the service worker, so there's no benefit from caching them.

]
}, userOptions.workboxOptions)

webpackConfig
.plugin('workbox')
.use(workboxWebpackModule[workboxPluginMode], [workBoxConfig])
} else {
throw new Error(`${workboxPluginMode} is not a supported Workbox webpack plugin mode. ` +
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure what the philosophy was around misconfiguration. If there's something that should be done here other than throwing an Error, I'm happy to change.

Copy link
Member

Choose a reason for hiding this comment

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

Since this is a built-in plugin, you can add it the user options validation schema here: https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-service/lib/options.js#L3-L35

But throwing an error is also fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ❤️ joi, but have found the default error messages to be a little hard to read, so I'll just stick with a custom Error for this specific failure.

`Valid modes are: ${Object.keys(workboxWebpackModule).join(', ')}`)
}
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"access": "public"
},
"dependencies": {
"sw-precache-webpack-plugin": "^0.11.4"
"workbox-webpack-plugin": "3.0.0-beta.0"
},
"devDependencies": {
"register-service-worker": "^1.0.0"
Expand Down