Skip to content

Commit 260ebbc

Browse files
authored
fix: add revision to builds/latest.json precache manifest entry (#107)
* fix: add revision to builds/latest.json entry * chore: cleanup * chore: digest returns an string
1 parent 36f9144 commit 260ebbc

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@vite-pwa/nuxt",
33
"type": "module",
44
"version": "0.3.5",
5-
"packageManager": "pnpm@8.11.0",
5+
"packageManager": "pnpm@8.12.1",
66
"description": "Zero-config PWA for Nuxt 3",
77
"author": "antfu <anthonyfu117@hotmail.com>",
88
"license": "MIT",

src/config.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { lstat } from 'node:fs/promises'
2+
import { createHash } from 'node:crypto'
3+
import { createReadStream } from 'node:fs'
14
import type { Nuxt } from '@nuxt/schema'
25
import { resolve } from 'pathe'
36
import type { NitroConfig } from 'nitropack'
@@ -80,12 +83,16 @@ export function configurePWAOptions(
8083

8184
// allow override manifestTransforms
8285
if (!nuxt.options.dev && !config.manifestTransforms)
83-
config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? '/', appManifestFolder)]
86+
config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? '/', options.outDir, appManifestFolder)]
8487
}
8588

86-
function createManifestTransform(base: string, appManifestFolder?: string): import('workbox-build').ManifestTransform {
89+
function createManifestTransform(
90+
base: string,
91+
publicFolder: string,
92+
appManifestFolder?: string,
93+
): import('workbox-build').ManifestTransform {
8794
return async (entries) => {
88-
entries.filter(e => e && e.url.endsWith('.html')).forEach((e) => {
95+
entries.filter(e => e.url.endsWith('.html')).forEach((e) => {
8996
const url = e.url.startsWith('/') ? e.url.slice(1) : e.url
9097
if (url === 'index.html') {
9198
e.url = base
@@ -98,12 +105,39 @@ function createManifestTransform(base: string, appManifestFolder?: string): impo
98105
})
99106

100107
if (appManifestFolder) {
108+
// this shouldn't be necessary, since we are using dontCacheBustURLsMatching
101109
const regExp = /(\/)?[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12}\.json$/i
102110
// we need to remove the revision from the sw prechaing manifest, UUID is enough:
103111
// we don't use dontCacheBustURLsMatching, single regex
104-
entries.filter(e => e && e.url.startsWith(appManifestFolder) && regExp.test(e.url)).forEach((e) => {
112+
entries.filter(e => e.url.startsWith(appManifestFolder) && regExp.test(e.url)).forEach((e) => {
105113
e.revision = null
106114
})
115+
// add revision to latest.json file: we are excluding `_nuxt/` assets from dontCacheBustURLsMatching
116+
const latest = `${appManifestFolder}latest.json`
117+
const latestJson = resolve(publicFolder, latest)
118+
const data = await lstat(latestJson).catch(() => undefined)
119+
if (data?.isFile()) {
120+
const revision = await new Promise<string>((resolve, reject) => {
121+
const cHash = createHash('MD5')
122+
const stream = createReadStream(latestJson)
123+
stream.on('error', (err) => {
124+
reject(err)
125+
})
126+
stream.on('data', chunk => cHash.update(chunk))
127+
stream.on('end', () => {
128+
resolve(cHash.digest('hex'))
129+
})
130+
})
131+
132+
const latestEntry = entries.find(e => e.url === latest)
133+
if (latestEntry)
134+
latestEntry.revision = revision
135+
else
136+
entries.push({ url: latest, revision, size: data.size })
137+
}
138+
else {
139+
entries = entries.filter(e => e.url !== latest)
140+
}
107141
}
108142

109143
return { manifest: entries, warnings: [] }

0 commit comments

Comments
 (0)