Skip to content

Commit 872dce1

Browse files
kedrzupi0
andauthored
fix: precached files are not updated (#386)
* fix: precached files are not updated * refactor: move randomString to utils * refactor: apply default revision in options.js * test: update snapshot * test: fix revision Co-authored-by: pooya parsa <[email protected]>
1 parent 98e36a3 commit 872dce1

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

lib/workbox/module.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = async function nuxtWorkbox (nuxt, pwa, moduleContainer) {
3535
'importScripts',
3636
'config',
3737
'cacheNames',
38+
'cacheOptions',
3839
'clientsClaim',
3940
'skipWaiting',
4041
'cleanupOutdatedCaches',

lib/workbox/options.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const { joinUrl, getRouteParams, startCase } = require('../utils')
3+
const { randomString } = require('./utils')
34
const defaults = require('./defaults')
45

56
function getOptions (nuxt, pwa) {
@@ -81,6 +82,17 @@ function getOptions (nuxt, pwa) {
8182
options.cacheOptions.cacheId = (process.env.npm_package_name || 'nuxt') + (nuxt.options.dev ? '-dev' : '-prod')
8283
}
8384

85+
// Default revision
86+
if (!options.cacheOptions.revision) {
87+
options.cacheOptions.revision = randomString(12)
88+
}
89+
90+
// Normalize preCaching
91+
options.preCaching = options.preCaching.map(url => ({
92+
revision: options.cacheOptions.revision,
93+
...(typeof url === 'string' ? { url } : url)
94+
}))
95+
8496
// Normalize runtimeCaching
8597
const pluginModules = {
8698
BackgroundSync: 'backgroundSync.BackgroundSyncPlugin',

lib/workbox/templates/sw.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function precacheAssets(workbox, options) {
5050
}
5151
}
5252

53+
5354
function runtimeCaching(workbox, options) {
5455
for (const entry of options.runtimeCaching) {
5556
const urlPattern = new RegExp(entry.urlPattern)

lib/workbox/utils.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,21 @@ async function copyTemplate ({ src, dst, options }) {
2929
await writeFile(dst, compile({ options }))
3030
}
3131

32+
function randomString (length) {
33+
const result = []
34+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
35+
36+
for (let i = 0; i < length; i++) {
37+
const char = characters.charAt(Math.floor(Math.random() * characters.length))
38+
result.push(char)
39+
}
40+
41+
return result.join('')
42+
}
43+
3244
module.exports = {
3345
readJSFiles,
3446
pick,
35-
copyTemplate
47+
copyTemplate,
48+
randomString
3649
}

test/__snapshots__/pwa.test.js.snap

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ exports[`pwa manifest.json 1`] = `
142142
`;
143143

144144
exports[`pwa sw.js 1`] = `
145-
"const options = {\\"workboxURL\\":\\"https://cdn.jsdelivr.net/npm/workbox-cdn/workbox/workbox-sw.js\\",\\"importScripts\\":[\\"custom-sw.js\\"],\\"config\\":{\\"debug\\":true},\\"cacheNames\\":{\\"prefix\\":\\"test\\",\\"googleAnalytics\\":\\"test-ga\\"},\\"clientsClaim\\":true,\\"skipWaiting\\":true,\\"cleanupOutdatedCaches\\":true,\\"offlineAnalytics\\":true,\\"preCaching\\":[\\"/?standalone=true\\",\\"precache.js\\"],\\"runtimeCaching\\":[{\\"urlPattern\\":\\"https://google.com/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"https://pwa.nuxtjs.org/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyOptions\\":{\\"cacheName\\":\\"nuxt-pwa\\"},\\"strategyPlugins\\":[{\\"use\\":\\"expiration.ExpirationPlugin\\",\\"config\\":[{\\"maxEntries\\":10,\\"maxAgeSeconds\\":300}]}]},{\\"urlPattern\\":\\"/_nuxt/\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"/\\",\\"handler\\":\\"NetworkFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]}],\\"offlinePage\\":null,\\"pagesURLPattern\\":\\"/\\",\\"offlineStrategy\\":\\"NetworkFirst\\"}
145+
"const options = {\\"workboxURL\\":\\"https://cdn.jsdelivr.net/npm/workbox-cdn/workbox/workbox-sw.js\\",\\"importScripts\\":[\\"custom-sw.js\\"],\\"config\\":{\\"debug\\":true},\\"cacheNames\\":{\\"prefix\\":\\"test\\",\\"googleAnalytics\\":\\"test-ga\\"},\\"cacheOptions\\":{\\"revision\\":\\"test-rev\\",\\"cacheId\\":\\"@nuxtjs/pwa-prod\\"},\\"clientsClaim\\":true,\\"skipWaiting\\":true,\\"cleanupOutdatedCaches\\":true,\\"offlineAnalytics\\":true,\\"preCaching\\":[{\\"revision\\":\\"test-rev\\",\\"url\\":\\"/?standalone=true\\"},{\\"revision\\":\\"test-rev\\",\\"url\\":\\"precache.js\\"}],\\"runtimeCaching\\":[{\\"urlPattern\\":\\"https://google.com/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"https://pwa.nuxtjs.org/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyOptions\\":{\\"cacheName\\":\\"nuxt-pwa\\"},\\"strategyPlugins\\":[{\\"use\\":\\"expiration.ExpirationPlugin\\",\\"config\\":[{\\"maxEntries\\":10,\\"maxAgeSeconds\\":300}]}]},{\\"urlPattern\\":\\"/_nuxt/\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"/\\",\\"handler\\":\\"NetworkFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]}],\\"offlinePage\\":null,\\"pagesURLPattern\\":\\"/\\",\\"offlineStrategy\\":\\"NetworkFirst\\"}
146146
147147
importScripts(...[options.workboxURL, ...options.importScripts])
148148
@@ -194,6 +194,7 @@ function precacheAssets(workbox, options) {
194194
}
195195
}
196196
197+
197198
function runtimeCaching(workbox, options) {
198199
for (const entry of options.runtimeCaching) {
199200
const urlPattern = new RegExp(entry.urlPattern)

test/fixture/nuxt.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ module.exports = {
3636
prefix: 'test',
3737
googleAnalytics: 'test-ga'
3838
},
39+
cacheOptions: {
40+
revision: 'test-rev'
41+
},
3942
importScripts: [
4043
'custom-sw.js'
4144
],

0 commit comments

Comments
 (0)