Permalink
Browse files

feat: workbox

  • Loading branch information...
pi0 committed Jun 2, 2017
1 parent b0ad221 commit 3d919c3bcde52577336162872420fa22e9a111e1
Showing with 238 additions and 0 deletions.
  1. +23 −0 modules/workbox/README.md
  2. +31 −0 modules/workbox/index.js
  3. +155 −0 modules/workbox/package-lock.json
  4. +14 −0 modules/workbox/package.json
  5. +15 −0 modules/workbox/plugin.js
@@ -0,0 +1,23 @@
# Workbox (WIP)
# Setup
- Add `@nuxtjs/workbox` dependency using yarn or npm to your project
- Add `@nuxtjs/workbox` module to `nuxt.config.js`:
```js
modules: [
{
src: '@nuxtjs/workbox',
options: {
// options
}
}
]
```
- Add generated assets to `.gitignore` file:
```
static/sw.js
static/workbox-sw.*.js
```
@@ -0,0 +1,31 @@
const path = require('path')
const workboxPlugin = require('workbox-webpack-plugin');
module.exports = function nuxtWorkbox(options) {
if(this.options.dev) {
return
}
const swFileName = 'sw.js'
// 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
this.options.build.plugins.push(new workboxPlugin(Object.assign({
swDest: path.resolve(this.options.srcDir, 'static', swFileName),
modifyUrlPrefix: {
'/': this.options.build.publicPath
}
}, options)))
// Register runtime plugin
this.addPlugin({
src: path.resolve(__dirname, 'plugin.js'),
ssr: false,
options: {
swURL: this.options.router.base + swFileName,
swScope: this.options.router.base
}
})
}
module.exports.meta = require('./package.json')

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,14 @@
{
"name": "@nuxtjs/workbox",
"version": "0.0.0",
"license": "MIT",
"main": "index.js",
"repository": "https://github.com/nuxt/modules",
"homepage": "https://github.com/nuxt/modules/tree/master/modules/workbox",
"publishConfig": {
"access": "public"
},
"dependencies": {
"workbox-webpack-plugin": "^1.0.1"
}
}
@@ -0,0 +1,15 @@
window.onNuxtReady(() => {
if (!'serviceWorker' in navigator) {
log('serviceWorker is not supported')
return
}
log('Installing')
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 3d919c3

Please sign in to comment.