Skip to content

Commit 2e356d0

Browse files
author
pooya parsa
committed
feat(workbox): workbox-window support
1 parent 4e9aee5 commit 2e356d0

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

Diff for: docs/modules/workbox.md

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ workbox: {
1818
}
1919
```
2020

21+
## Workbox Window
22+
23+
A global `$workbox` service is injected to the application, which can be used either by plugins (via `context.app.$workbox`) or pages (via `this.$workbox`).
24+
25+
See [workbox-window](https://developers.google.com/web/tools/workbox/modules/workbox-window) docs for more information about use cases.
26+
2127
## Options
2228

2329
<!-- General -->

Diff for: packages/workbox/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"access": "public"
99
},
1010
"dependencies": {
11-
"@nuxtjs/pwa-utils": "3.0.0-beta.10"
11+
"@nuxtjs/pwa-utils": "3.0.0-beta.10",
12+
"workbox-window": "4.0.0"
1213
}
1314
}

Diff for: packages/workbox/templates/sw.register.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
if ('serviceWorker' in navigator) {
2-
navigator.serviceWorker.register('<%= options.swURL %>', {
1+
import { Workbox } from 'workbox-window/build/workbox-window.<%= options.dev ? 'dev' : 'prod' %>.mjs'
2+
3+
export default async function(ctx, inject) {
4+
const workbox = new Workbox('<%= options.swURL %>', {
35
scope: '<%= options.swScope %>'
4-
}).then(function(registration) {
5-
window.$sw = registration
6-
}).catch(function(error) {
7-
console.error('Service worker registration failed:', error)
86
})
7+
8+
// Inject as $workbox
9+
inject('workbox', workbox)
10+
11+
if (!'serviceWorker' in navigator) {
12+
workbox._unsupported = true
13+
return // Unsupported browser!
14+
}
15+
16+
workbox.register()
917
}

Diff for: yarn.lock

+12
Original file line numberDiff line numberDiff line change
@@ -11271,6 +11271,18 @@ wordwrap@~1.0.0:
1127111271
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
1127211272
integrity sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=
1127311273

11274+
workbox-core@^4.0.0:
11275+
version "4.0.0"
11276+
resolved "https://registry.yarnpkg.com/workbox-core/-/workbox-core-4.0.0.tgz#3b4aa11c9d739361ed8916b7611abc3b6fefed8b"
11277+
integrity sha512-FRoOUuJBl7COTwvGO5FC9k0VyYGv/LkjVqgVwKk9MXQn/Xi+bvGDcqSVF7qfT+sJ6Ffcr/V+dVMpoZAE/X5e+g==
11278+
11279+
workbox-window@4.0.0:
11280+
version "4.0.0"
11281+
resolved "https://registry.yarnpkg.com/workbox-window/-/workbox-window-4.0.0.tgz#a4d6404c96cc65d4882f79331d4e812c10f912a6"
11282+
integrity sha512-gdLzP/L0vu5Gi3pdUYtVNmg0yE7OriEuYZBoLt8AYoILXh6cta9WsWHKQ0/pXfmEmZywr4Dfu0qkLLvc9VlZGQ==
11283+
dependencies:
11284+
workbox-core "^4.0.0"
11285+
1127411286
worker-farm@^1.5.2:
1127511287
version "1.6.0"
1127611288
resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.6.0.tgz#aecc405976fab5a95526180846f0dba288f3a4a0"

0 commit comments

Comments
 (0)