Skip to content

Commit

Permalink
feat(workbox): use nuxt tapables
Browse files Browse the repository at this point in the history
prevent glob patterns and race condition in workbox webpack plugin (#110)
  • Loading branch information
Pooya Parsa committed Aug 16, 2017
1 parent 163b977 commit 4c52955
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions modules/workbox/index.js
@@ -1,5 +1,5 @@
const path = require('path')
const WorkboxPlugin = require('workbox-webpack-plugin')
const swBuild = require('workbox-build')

const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
Expand All @@ -21,16 +21,15 @@ module.exports = function nuxtWorkbox (options) {

const swFileName = 'sw.js'

// Add webpack plugin. This plugin internally uses swBuild to generate sw file
// Use swBuild to generate sw file
// We set dest to static dir that is served as / to allow global sw scope
// https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.generateSW

this.options.build.plugins.push(new WorkboxPlugin(Object.assign({
const workboxOptions = Object.assign({
swDest: path.resolve(this.options.srcDir, 'static', swFileName),
directoryIndex: '/',
cacheId: process.env.npm_package_name + '_' + process.env.npm_package_version,
clientsClaim: true,
globPatterns: ['**\/*.{js,css}'],
globPatterns: ['**/*.{js,css}'],
globDirectory: path.resolve(this.options.buildDir, 'dist'),
modifyUrlPrefix: {
'': fixUrl(publicPath)
Expand All @@ -48,7 +47,19 @@ module.exports = function nuxtWorkbox (options) {
handler: 'cacheFirst'
}
]
}, options)))
}, options)

// Use nuxt plugins to prevent race conditions with webpack plugin
// (https://github.com/nuxt-community/modules/issues/110)
this.nuxt.plugin('build', builder => {
builder.plugin('built', () => {
if (workboxOptions.swSrc) {
return swBuild.injectManifest(workboxOptions)
} else {
return swBuild.generateSW(workboxOptions)
}
})
})

// Register runtime plugin
this.addPlugin({
Expand Down
2 changes: 1 addition & 1 deletion modules/workbox/package.json
Expand Up @@ -9,6 +9,6 @@
"access": "public"
},
"dependencies": {
"workbox-webpack-plugin": "^1.1.0"
"workbox-build": "^1.3.0"
}
}

0 comments on commit 4c52955

Please sign in to comment.