Permalink
Browse files

feat(workbox): improve service worker

  • Loading branch information...
pi0 committed Jun 2, 2017
1 parent d030e5c commit 445a1c2ce925b7798c7d85a3a1948ee90908006b
Showing with 16 additions and 11 deletions.
  1. +14 −4 modules/workbox/index.js
  2. +2 −7 modules/workbox/plugin.js
@@ -2,18 +2,28 @@ const path = require('path')
const workboxPlugin = require('workbox-webpack-plugin');
module.exports = function nuxtWorkbox(options) {
if(this.options.dev) {
if (this.options.dev) {
return
}
const swFileName = 'sw.js'
const publicPath = this.options.build.publicPath
const routerBase = this.options.router.base
// Add webpack plugin. This plugin internally uses 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({
swDest: path.resolve(this.options.srcDir, 'static', swFileName),
// navigateFallback: routerBase, // BUG
directoryIndex: '/',
cacheId: process.env.npm_package_name,
skipWaiting: true, // sw is being registered after onNuxtReady()
clientsClaim: true,
globPatterns: ['**\/*.{js,css}'],
modifyUrlPrefix: {
'/': this.options.build.publicPath
'/': publicPath
}
}, options)))
@@ -22,8 +32,8 @@ module.exports = function nuxtWorkbox(options) {
src: path.resolve(__dirname, 'plugin.js'),
ssr: false,
options: {
swURL: this.options.router.base + swFileName,
swScope: this.options.router.base
swURL: routerBase + swFileName,
swScope: routerBase
}
})
}
@@ -1,15 +1,10 @@
window.onNuxtReady(() => {
if (!'serviceWorker' in navigator) {
log('serviceWorker is not supported')
console.warn('serviceWorker is not supported')
return
}
log('Installing')
navigator.serviceWorker.register('<%= options.swURL %>',{
navigator.serviceWorker.register('<%= options.swURL %>', {
scope: '<%= options.swScope %>'
});
})
function log(msg) {
console.log('[nuxt][workbox] ', msg) // eslint-disable-line no-console
}

0 comments on commit 445a1c2

Please sign in to comment.