From 8bc4a3bbca296f9470f6ec7b63cef1abae810932 Mon Sep 17 00:00:00 2001 From: Jonas Galvez Date: Sat, 25 Aug 2018 01:07:31 -0300 Subject: [PATCH] feat(workbox): offline page assets (#85) --- .eslintignore | 1 + docs/modules/workbox.md | 4 +++- packages/workbox/index.js | 2 ++ packages/workbox/templates/sw.template.js | 7 ++++++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.eslintignore b/.eslintignore index 0bfc6cbb..b3e3aa97 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,3 @@ **/templates +**/node_modules **/dist diff --git a/docs/modules/workbox.md b/docs/modules/workbox.md index 21985fbf..63406ad7 100644 --- a/docs/modules/workbox.md +++ b/docs/modules/workbox.md @@ -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. diff --git a/packages/workbox/index.js b/packages/workbox/index.js index e54f5f68..e61508ff 100755 --- a/packages/workbox/index.js +++ b/packages/workbox/index.js @@ -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 @@ -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 || []), diff --git a/packages/workbox/templates/sw.template.js b/packages/workbox/templates/sw.template.js index a1626f73..e02ee78a 100644 --- a/packages/workbox/templates/sw.template.js +++ b/packages/workbox/templates/sw.template.js @@ -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()<% } %>