-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
feat: Use the Workbox webpack plugin in pwa template #769
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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$') | ||
] | ||
}, userOptions.workboxOptions) | ||
|
||
webpackConfig | ||
.plugin('workbox') | ||
.use(workboxWebpackModule[workboxPluginMode], [workBoxConfig]) | ||
} else { | ||
throw new Error(`${workboxPluginMode} is not a supported Workbox webpack plugin mode. ` + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ❤️ |
||
`Valid modes are: ${Object.keys(workboxWebpackModule).join(', ')}`) | ||
} | ||
} | ||
}) | ||
|
||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.