Skip to content

Commit 67f1c3d

Browse files
author
pooya parsa
committed
feat(workbox): preCaching option
1 parent ffc7b18 commit 67f1c3d

File tree

6 files changed

+45
-16
lines changed

6 files changed

+45
-16
lines changed

docs/modules/workbox.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,21 @@ It is recommanded to test workbox using `nuxt build`/`nuxt start`. You can enabl
6767

6868
<!-- Precache -->
6969

70+
### `preCaching`
71+
72+
(Array) Cache a set of files when registering service worker. Default is `[]`
73+
74+
Workbox takes a lot of the heavy lifting out of precaching by simplifying the API and ensuring assets are downloaded efficiently.
75+
7076
### `cacheOptions`
7177

7278
(Object) Default:
7379

7480
```js
7581
{
7682
cacheId: '<npm package name> || nuxt',
77-
directoryIndex: '/'
83+
directoryIndex: '/',
84+
revision: undefined
7885
}
7986
```
8087

packages/workbox/lib/defaults.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ module.exports = {
1414
workboxExtensions: [],
1515

1616
// Precache
17+
preCaching: [],
1718
cacheOptions: {
1819
cacheId: process.env.npm_package_name || 'nuxt',
19-
directoryIndex: '/'
20+
directoryIndex: '/',
21+
revision: undefined
2022
},
2123
cachingExtensions: [],
2224

packages/workbox/lib/options.js

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ function getOptions (moduleOptions) {
5656
})
5757
}
5858

59+
// Add offlineAssets to precaching
60+
if (options.offlineAssets.length) {
61+
options.preCaching.unshift(...options.offlineAssets)
62+
}
63+
64+
// Add offlinePage to precaching
65+
if (options.offlinePage) {
66+
options.preCaching.unshift(options.offlinePage)
67+
}
68+
5969
// Normalize runtimeCaching
6070
options.runtimeCaching = options.runtimeCaching.map(entry => ({
6171
...entry,

packages/workbox/templates/sw.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,9 @@ workbox.googleAnalytics.initialize()
3333
// Precaches
3434
// --------------------------------------------------
3535

36-
// Precache build artifacts
37-
workbox.precaching.precacheAndRoute([], <%= JSON.stringify(options.cacheOptions, null, 2) %>)
38-
39-
<% if (options.offlineAssets.length) { %>
40-
// Precache offlineAssets
41-
workbox.precaching.precacheAndRoute([<%= options.offlineAssets.map((i) => `'${i}'`).join(', ') %>])
42-
<% } %>
43-
44-
<% if (options.offlinePage) { %>
45-
// Precache offlinePage
46-
workbox.precaching.precacheAndRoute(['<%= options.offlinePage %>'])
36+
// Precache assets
37+
<% if (options.preCaching.length) { %>
38+
workbox.precaching.precacheAndRoute(<%= JSON.stringify(options.preCaching, null, 2) %>, <%= JSON.stringify(options.cacheOptions, null, 2) %>)
4739
<% } %>
4840

4941
<% if (options.cachingExtensions) { %>

test/__snapshots__/pwa.test.js.snap

+13-3
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ workbox.googleAnalytics.initialize()
7979
// Precaches
8080
// --------------------------------------------------
8181
82-
// Precache build artifacts
83-
workbox.precaching.precacheAndRoute([], {
82+
// Precache assets
83+
84+
workbox.precaching.precacheAndRoute([
85+
\\"/offline.html\\",
86+
\\"/offline.png\\",
87+
\\"precahce.1.js\\"
88+
], {
8489
\\"cacheId\\": \\"nuxt\\",
8590
\\"directoryIndex\\": \\"/\\"
8691
})
@@ -96,7 +101,12 @@ workbox.precaching.precacheAndRoute([], {
96101
// Register route handlers for runtimeCaching
97102
workbox.routing.registerRoute(new RegExp('https://google.com/.*'), workbox.strategies.cacheFirst ({}), 'GET')
98103
workbox.routing.registerRoute(new RegExp('/_nuxt/(?!.*(__webpack_hmr|hot-update))'), workbox.strategies.cacheFirst ({}), 'GET')
99-
workbox.routing.registerRoute(new RegExp('^/(?!.*(__webpack_hmr|hot-update))'), workbox.strategies.networkFirst ({}), 'GET')
104+
105+
// Register router handler for offlinePage
106+
workbox.routing.registerRoute(new RegExp('/.*'), ({event}) => {
107+
return workbox.strategies.networkOnly().handle({event})
108+
.catch(() => caches.match('/offline.html'))
109+
})
100110
101111
// -- Start of routingExtensions --
102112
// Routing Extension

test/fixture/nuxt.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ module.exports = {
4242
routingExtensions: [
4343
'~/sw/routing'
4444
],
45+
preCaching: [
46+
'precahce.1.js'
47+
],
48+
offline: true,
49+
offlinePage: '/offline.html',
50+
offlineAssets: [
51+
'/offline.png'
52+
],
4553
runtimeCaching: [
4654
{
4755
urlPattern: 'https://google.com/.*',

0 commit comments

Comments
 (0)