Skip to content

Commit

Permalink
fix: add revision to builds/latest.json precache manifest entry (#107)
Browse files Browse the repository at this point in the history
* fix: add revision to builds/latest.json entry

* chore: cleanup

* chore: digest returns an string
  • Loading branch information
userquin committed Dec 20, 2023
1 parent 36f9144 commit 260ebbc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@vite-pwa/nuxt",
"type": "module",
"version": "0.3.5",
"packageManager": "pnpm@8.11.0",
"packageManager": "pnpm@8.12.1",
"description": "Zero-config PWA for Nuxt 3",
"author": "antfu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down
42 changes: 38 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { lstat } from 'node:fs/promises'
import { createHash } from 'node:crypto'
import { createReadStream } from 'node:fs'
import type { Nuxt } from '@nuxt/schema'
import { resolve } from 'pathe'
import type { NitroConfig } from 'nitropack'
Expand Down Expand Up @@ -80,12 +83,16 @@ export function configurePWAOptions(

// allow override manifestTransforms
if (!nuxt.options.dev && !config.manifestTransforms)
config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? '/', appManifestFolder)]
config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? '/', options.outDir, appManifestFolder)]
}

function createManifestTransform(base: string, appManifestFolder?: string): import('workbox-build').ManifestTransform {
function createManifestTransform(
base: string,
publicFolder: string,
appManifestFolder?: string,
): import('workbox-build').ManifestTransform {
return async (entries) => {
entries.filter(e => e && e.url.endsWith('.html')).forEach((e) => {
entries.filter(e => e.url.endsWith('.html')).forEach((e) => {
const url = e.url.startsWith('/') ? e.url.slice(1) : e.url
if (url === 'index.html') {
e.url = base
Expand All @@ -98,12 +105,39 @@ function createManifestTransform(base: string, appManifestFolder?: string): impo
})

if (appManifestFolder) {
// this shouldn't be necessary, since we are using dontCacheBustURLsMatching
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
// we need to remove the revision from the sw prechaing manifest, UUID is enough:
// we don't use dontCacheBustURLsMatching, single regex
entries.filter(e => e && e.url.startsWith(appManifestFolder) && regExp.test(e.url)).forEach((e) => {
entries.filter(e => e.url.startsWith(appManifestFolder) && regExp.test(e.url)).forEach((e) => {
e.revision = null
})
// add revision to latest.json file: we are excluding `_nuxt/` assets from dontCacheBustURLsMatching
const latest = `${appManifestFolder}latest.json`
const latestJson = resolve(publicFolder, latest)
const data = await lstat(latestJson).catch(() => undefined)
if (data?.isFile()) {
const revision = await new Promise<string>((resolve, reject) => {
const cHash = createHash('MD5')
const stream = createReadStream(latestJson)
stream.on('error', (err) => {
reject(err)
})
stream.on('data', chunk => cHash.update(chunk))
stream.on('end', () => {
resolve(cHash.digest('hex'))
})
})

const latestEntry = entries.find(e => e.url === latest)
if (latestEntry)
latestEntry.revision = revision
else
entries.push({ url: latest, revision, size: data.size })
}
else {
entries = entries.filter(e => e.url !== latest)
}
}

return { manifest: entries, warnings: [] }
Expand Down

0 comments on commit 260ebbc

Please sign in to comment.