Skip to content

Commit

Permalink
fix: precached files are not updated (#386)
Browse files Browse the repository at this point in the history
* 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 <pyapar@gmail.com>
  • Loading branch information
kedrzu and pi0 committed Nov 28, 2020
1 parent 98e36a3 commit 872dce1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/workbox/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = async function nuxtWorkbox (nuxt, pwa, moduleContainer) {
'importScripts',
'config',
'cacheNames',
'cacheOptions',
'clientsClaim',
'skipWaiting',
'cleanupOutdatedCaches',
Expand Down
12 changes: 12 additions & 0 deletions lib/workbox/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path')
const { joinUrl, getRouteParams, startCase } = require('../utils')
const { randomString } = require('./utils')
const defaults = require('./defaults')

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

// Default revision
if (!options.cacheOptions.revision) {
options.cacheOptions.revision = randomString(12)
}

// Normalize preCaching
options.preCaching = options.preCaching.map(url => ({
revision: options.cacheOptions.revision,
...(typeof url === 'string' ? { url } : url)
}))

// Normalize runtimeCaching
const pluginModules = {
BackgroundSync: 'backgroundSync.BackgroundSyncPlugin',
Expand Down
1 change: 1 addition & 0 deletions lib/workbox/templates/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function precacheAssets(workbox, options) {
}
}


function runtimeCaching(workbox, options) {
for (const entry of options.runtimeCaching) {
const urlPattern = new RegExp(entry.urlPattern)
Expand Down
15 changes: 14 additions & 1 deletion lib/workbox/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,21 @@ async function copyTemplate ({ src, dst, options }) {
await writeFile(dst, compile({ options }))
}

function randomString (length) {
const result = []
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

for (let i = 0; i < length; i++) {
const char = characters.charAt(Math.floor(Math.random() * characters.length))
result.push(char)
}

return result.join('')
}

module.exports = {
readJSFiles,
pick,
copyTemplate
copyTemplate,
randomString
}
3 changes: 2 additions & 1 deletion test/__snapshots__/pwa.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ exports[`pwa manifest.json 1`] = `
`;

exports[`pwa sw.js 1`] = `
"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\\"}
"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\\"}
importScripts(...[options.workboxURL, ...options.importScripts])
Expand Down Expand Up @@ -194,6 +194,7 @@ function precacheAssets(workbox, options) {
}
}
function runtimeCaching(workbox, options) {
for (const entry of options.runtimeCaching) {
const urlPattern = new RegExp(entry.urlPattern)
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module.exports = {
prefix: 'test',
googleAnalytics: 'test-ga'
},
cacheOptions: {
revision: 'test-rev'
},
importScripts: [
'custom-sw.js'
],
Expand Down

0 comments on commit 872dce1

Please sign in to comment.