Skip to content

Commit

Permalink
feat: workboxExtensions and extension reading fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed Feb 7, 2019
1 parent 647adc0 commit 5c56484
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/modules/workbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ workbox: {

(String) Loads and inserts the contents of the specified file path into the service worker script, below autogenerated calls to `workbox.routing.*`. You may add as many extra calls as you want to this file.

### `workboxExtensions`

(String) Loads and inserts the contents of the specified file path into the service worker script before any call to `precacheAndRoute`. You may add as many extra calls as you want to this file.

### `clientsClaim`

(Boolean) Start controlling any existing clients as soon as it activates. Enabled by default.
Expand Down Expand Up @@ -102,6 +106,7 @@ workbox: {

(String) Defaults to `routerBase`.


## Examples

### Adding custom runtimeCaching items (For CDN)
Expand Down Expand Up @@ -234,3 +239,5 @@ workbox.routing.registerRoute(
```

Thanks to [@CarterLi](https://github.com/CarterLi) for the tip.

### Enable
5 changes: 3 additions & 2 deletions packages/workbox/lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ module.exports = function nuxtWorkbox (moduleOptions) {
fileName: options.swDest,
options: {
...options,
routingExtensions: readJSFiles(options.routingExtensions),
cachingExtensions: readJSFiles(options.cachingExtensions)
routingExtensions: readJSFiles.call(this, options.routingExtensions),
cachingExtensions: readJSFiles.call(this, options.cachingExtensions),
workboxExtensions: readJSFiles.call(this, options.workboxExtensions)
}
})
}
Expand Down
3 changes: 1 addition & 2 deletions packages/workbox/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ const { readFileSync, existsSync } = require('fs')

function readJSFiles (files) {
return Array.from(files)
.map(Boolean)
.map(path => {
path = this.nuxt.resolveAlias(path)
path = this.nuxt.resolver.resolvePath(path)
if (path && existsSync(path)) {
return readFileSync(path, 'utf8')
} else {
Expand Down
11 changes: 7 additions & 4 deletions packages/workbox/templates/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ workbox.skipWaiting()
workbox.googleAnalytics.initialize()
<% } %>

<% if (options.workboxExtensions) { %>
// -- Start of workboxExtensions --
<%= options.workboxExtensions %>// -- End of workboxExtensions --
<% } %>

// --------------------------------------------------
// Precaches
// --------------------------------------------------
Expand All @@ -46,8 +51,7 @@ workbox.precaching.precacheAndRoute(['<%= options.offlinePage %>'])
// --------------------------------------------------
<% if (options.cachingExtensions) { %>
// -- Start of cachingExtensions --
<%= options.cachingExtensions %>
// -- End of cachingExtensions --
<%= options.cachingExtensions %>// -- End of cachingExtensions --
<% } %>

// Register route handlers for runtimeCaching
Expand All @@ -63,6 +67,5 @@ workbox.routing.registerRoute(new RegExp('/.*'), ({event}) => {

<% if (options.routingExtensions) { %>
// -- Start of routingExtensions --
<%= options.routingExtensions %>
// -- End of routingExtensions --
<%= options.routingExtensions %>// -- End of routingExtensions --
<% } %>
12 changes: 12 additions & 0 deletions test/__snapshots__/pwa.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ workbox.skipWaiting()
// Enable offline Google Analytics tracking
workbox.googleAnalytics.initialize()
// -- Start of workboxExtensions --
// Workbox Extension
// -- End of workboxExtensions --
// --------------------------------------------------
// Precaches
// --------------------------------------------------
Expand All @@ -85,9 +89,17 @@ workbox.precaching.precacheAndRoute([], {
// Runtime Caching
// --------------------------------------------------
// -- Start of cachingExtensions --
// Caching Extension
// -- End of cachingExtensions --
// Register route handlers for runtimeCaching
workbox.routing.registerRoute(new RegExp('https://google.com/.*'), workbox.strategies.cacheFirst ({}), 'GET')
workbox.routing.registerRoute(new RegExp('/_nuxt/(?!.*(__webpack_hmr|hot-update))'), workbox.strategies.cacheFirst ({}), 'GET')
workbox.routing.registerRoute(new RegExp('^/(?!.*(__webpack_hmr|hot-update))'), workbox.strategies.networkFirst ({}), 'GET')
// -- Start of routingExtensions --
// Routing Extension
// -- End of routingExtensions --
"
`;
9 changes: 9 additions & 0 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ module.exports = {
importScripts: [
'custom-sw.js'
],
workboxExtensions: [
'~/sw/workbox'
],
cachingExtensions: [
'~/sw/caching'
],
routingExtensions: [
'~/sw/routing'
],
runtimeCaching: [
{
urlPattern: 'https://google.com/.*',
Expand Down
1 change: 1 addition & 0 deletions test/fixture/sw/caching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Caching Extension
1 change: 1 addition & 0 deletions test/fixture/sw/routing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Routing Extension
1 change: 1 addition & 0 deletions test/fixture/sw/workbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Workbox Extension

0 comments on commit 5c56484

Please sign in to comment.