fix: Vite plugin data URLs change when the data changes

Merged
AL
11h ago
+82-14

Summary

The fix for 4.6.1. In 4.6.0 the Vite plugin's data files keep the names PDF.js asks for, so their URLs stay the same across upgrades. SvelteKit (_app/immutable/), Nuxt (_nuxt/) and Astro (_astro/) serve their asset folders as immutable, so a returning visitor's browser can keep an older version's files after an upgrade. PDF.js can't use them: the openjpeg.wasm published in 4.4.2 needs 17 imports, and the 4.6.0 worker provides 8.

The plugin now moves viewer.mjs and pdf.mjs into a folder named after a hash of both files and all their data, and writes the data next to them as before:

assets/pdfjs-viewer-84204294/viewer-DfOqdCoM.mjs
assets/pdfjs-viewer-84204294/pdf-CSmHxz2q.mjs
assets/pdfjs-viewer-84204294/pdf.sandbox.mjs
assets/pdfjs-viewer-84204294/wasm/jbig2.wasm
…

An upgrade that changes any of these files gets a new folder, so every URL in it changes. An upgrade that changes none of them keeps the folder and the cache.

How

  • outputOptions wraps assetFileNames. It calls the app's own setting first, string or function, then puts the folder in front of the file name. A runtime file the app sends to static/viewer/ lands in static/viewer/pdfjs-viewer-…/. The two runtime files are still recognised by their bytes.
  • The hash is SHA-256 over both runtime files and every data file with its name, cut to 8 hex characters. It's computed once per build and reads the same 3.4 MB the plugin copies anyway.
  • The element is unchanged. It still looks for the data next to viewer.mjs and pdf.mjs, and those two moved with it.

Effect on 4.6.0 users

The data moves from assets/wasm/… to assets/pdfjs-viewer-<hash>/wasm/…. A cache rule written per the 4.6.0 docs to exclude assets/wasm/ no longer matches, which is fine now that caching those files is safe. The README's cache note is updated. The docs site follows once 4.6.1 is on npm, because it pins the package version.

Validation

  • pnpm test:plugin: 6 passed. New: a copy of the package with one changed byte in wasm/openjpeg.wasm builds into a different folder, and an unchanged copy builds into the same one. The existing tests now check that both runtime files share a folder inside assets/, and that an app's own assetFileNames directories are kept.

  • Each test fails when the fix is broken. Four deliberate breakages, each restored afterwards:

    BreakageTests that fail
    No folder3
    Fixed folder name1 (the changed decoder)
    Hash ignores the data1 (the changed decoder)
    Only viewer.mjs moves2
  • Vite versions. Vite 4.5.14, 5.0.13, 5.4.21, 6.4.6, 7.3.8 and 8.3.8 all give the same folder name, pdfjs-viewer-84204294. In each, 288 files are in the folder, and the app's JavaScript points at the two runtime files inside it.

  • SSR. A server build still gets no data and no folder.

  • pnpm test -- --run: 40 passed. npm pack --dry-run includes plugins/vite.js.

Checklist

  • The change is focused and includes tests for behavior changes.
  • Relevant tests and the build pass locally.
  • Public API, behavior, or user-guidance documentation is updated where needed. The README is updated here; the docs site follows the release.
  • Accessibility impact has been considered, including keyboard navigation and focus visibility. The viewer is untouched.
  • This pull request does not disclose a security vulnerability.

🤖 Generated with Claude Code


Summary by cubic

Fixes the Vite plugin so the viewer's data URLs change when the data changes, so hosts that cache the asset folder as immutable, as SvelteKit, Nuxt and Astro do, no longer serve returning visitors an older PDF.js version's files.

Previously the plugin copied the data files with the names PDF.js asks for, so their URLs stayed the same across upgrades, and an old openjpeg.wasm could be served to a newer worker. The plugin now wraps the app's assetFileNames and moves viewer.mjs and pdf.mjs into a folder named after a content hash, writing the data next to them as before. Upgrades that change any of the files get a new folder, so all their URLs change; upgrades that change none keep the folder and the cache.

  • For 4.6.0 users, the data moves from assets/wasm/... to assets/pdfjs-viewer-<hash>/wasm/..., so a cache rule written per the 4.6.0 docs no longer matches; that is fine now that caching those files is safe.
  • The element is unchanged; it still looks for data next to viewer.mjs and pdf.mjs.