Skip to content

Commit

Permalink
feat(workbox): offline page assets (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
galvez authored and pi0 committed Aug 25, 2018
1 parent ef670b8 commit 8bc4a3b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/templates
**/node_modules
**/dist
4 changes: 3 additions & 1 deletion docs/modules/workbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ workbox: {

**importScripts** (Array) - Additional scripts to be imported in service worker script. (Relative to `/`. Can be placed in `assets/` directory)

**offlinePage** (String) - Enables routing all offline requests to the specified path. (Example: `/offline`)
**offlinePage** (String) - Enables routing all offline requests to the specified path. (Example: `/offline.html`)

**offlinePageAssets** (String) - List of offline page assets to precache (Example: `['/offline.png']`)

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

Expand Down
2 changes: 2 additions & 0 deletions packages/workbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function getOptions (moduleOptions) {
},
offline: true,
offlinePage: null,
offlinePageAssets: [],
_runtimeCaching: [
// Cache all _nuxt resources at runtime
// They are hashed by webpack so are safe to loaded by cacheFirst handler
Expand Down Expand Up @@ -116,6 +117,7 @@ function addTemplates (options) {
fileName: 'sw.template.js',
options: {
offlinePage: options.offlinePage,
offlinePageAssets: options.offlinePageAssets,
cachingExtensions: options.cachingExtensions,
routingExtensions: options.routingExtensions,
importScripts: [options.wbDst].concat(options.importScripts || []),
Expand Down
7 changes: 6 additions & 1 deletion packages/workbox/templates/sw.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ importScripts(<%= options.importScripts.map((i) => `'${i}'`).join(', ') %>)

workbox.precaching.precacheAndRoute([], <%= JSON.stringify(options.wbOptions, null, 2) %>)

<% if (options.offlinePage) { %>workbox.precaching.precacheAndRoute(['<%= options.offlinePage %>'])<% } %>
<% if (options.offlinePage) { %>
workbox.precaching.precacheAndRoute(['<%= options.offlinePage %>'])
<% if (options.offlinePageAssets.length) { %>
workbox.precaching.precacheAndRoute([<%= options.offlinePageAssets.map((i) => `'${i}'`).join(', ') %>])
<% } %>
<% } %>
<% if (options.cachingExtensions) { %><%= options.cachingExtensions %><% } %>
<% if (options.clientsClaim) { %>workbox.clientsClaim()<% } %>
<% if (options.skipWaiting) { %>workbox.skipWaiting()<% } %>
Expand Down

0 comments on commit 8bc4a3b

Please sign in to comment.