Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: precached files are not updated #386

Merged
merged 6 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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