Skip to content

Commit 27c8fa1

Browse files
galvezpi0
authored andcommittedAug 28, 2018
feat(worbox): offlineAssets (#86)
1 parent 7a8bb3b commit 27c8fa1

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed
 

‎docs/modules/workbox.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ workbox: {
2323

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

26+
**offlineAssets** (String) - List of assets to precache, in case you need extra files precached other than the ones automatically processed (`_nuxt/*` etc) or you want to ensure assets used by your `offlinePage`. (Example: `['/offline.png']`)
27+
2628
**offlinePage** (String) - Enables routing all offline requests to the specified path. (Example: `/offline.html`)
2729

28-
**offlinePageAssets** (String) - List of offline page assets to precache (Example: `['/offline.png']`)
30+
**Please note** that enabling `offlinePage` will disable `/.*` caching and will route all offline requests to the specified path. Nuxt assets are still precached as are the ones specified in `offlineAssets`.
2931

3032
**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.
3133

‎packages/workbox/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function getOptions (moduleOptions) {
9090
},
9191
offline: true,
9292
offlinePage: null,
93-
offlinePageAssets: [],
93+
offlineAssets: [],
9494
_runtimeCaching: [
9595
// Cache all _nuxt resources at runtime
9696
// They are hashed by webpack so are safe to loaded by cacheFirst handler
@@ -104,6 +104,13 @@ function getOptions (moduleOptions) {
104104

105105
const options = defaultsDeep({}, this.options.workbox, moduleOptions, defaults)
106106

107+
// Backward compatibility
108+
// https://github.com/nuxt-community/pwa-module/pull/86
109+
if (Array.isArray(options.offlinePageAssets)) {
110+
options.offlineAssets = options.offlineAssets.concat(options.offlinePageAssets)
111+
delete options.offlinePageAssets
112+
}
113+
107114
// Optionally cache other routes for offline
108115
if (options.offline && !options.offlinePage) {
109116
options._runtimeCaching.push({
@@ -134,7 +141,7 @@ function addTemplates (options) {
134141
fileName: 'sw.template.js',
135142
options: {
136143
offlinePage: options.offlinePage,
137-
offlinePageAssets: options.offlinePageAssets,
144+
offlineAssets: options.offlineAssets,
138145
cachingExtensions: options.cachingExtensions,
139146
routingExtensions: options.routingExtensions,
140147
importScripts: [options.wbDst].concat(options.importScripts || []),

‎packages/workbox/templates/sw.template.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
importScripts(<%= options.importScripts.map((i) => `'${i}'`).join(', ') %>)
22

33
workbox.precaching.precacheAndRoute([], <%= JSON.stringify(options.wbOptions, null, 2) %>)
4-
5-
<% if (options.offlinePage) { %>
6-
workbox.precaching.precacheAndRoute(['<%= options.offlinePage %>'])
7-
<% if (options.offlinePageAssets.length) { %>
8-
workbox.precaching.precacheAndRoute([<%= options.offlinePageAssets.map((i) => `'${i}'`).join(', ') %>])
9-
<% } %>
4+
<% if (options.offlineAssets.length) { %>
5+
workbox.precaching.precacheAndRoute([<%= options.offlineAssets.map((i) => `'${i}'`).join(', ') %>])
106
<% } %>
7+
<% if (options.offlinePage) { %>workbox.precaching.precacheAndRoute(['<%= options.offlinePage %>'])<% } %>
118
<% if (options.cachingExtensions) { %><%= options.cachingExtensions %><% } %>
129
<% if (options.clientsClaim) { %>workbox.clientsClaim()<% } %>
1310
<% if (options.skipWaiting) { %>workbox.skipWaiting()<% } %>

0 commit comments

Comments
 (0)
Please sign in to comment.