Skip to content

fix: Vite plugin data URLs change when the data changes - #88

Merged
alekswebnet merged 1 commit into
masterfrom
fix/vite-plugin-cache-folder
Nov 24, 2026
Merged

alekswebnet merged 1 commit into
masterfrom
fix/vite-plugin-cache-folder

Conversation

@alekswebnet

@alekswebnet alekswebnet commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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:

    Breakage Tests that fail
    No folder 3
    Fixed folder name 1 (the changed decoder)
    Hash ignores the data 1 (the changed decoder)
    Only viewer.mjs moves 2
  • 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.

Written for commit 0c35fbe. Summary will update on new commits.

View guided diff Turn on auto-fix

The data keeps the names PDF.js asks for, so in 4.6.0 its URLs stayed
the same across upgrades. SvelteKit, Nuxt and Astro serve their asset
folders as immutable, so a returning visitor could keep an older PDF.js
version's files; the 4.4 openjpeg.wasm, for one, doesn't load in 4.6.

The plugin now wraps assetFileNames and moves viewer.mjs and pdf.mjs,
in whatever directory the app gives them, into a folder named after a
hash of both files and all their data, and writes the data next to them
as before. An upgrade that changes any of them gets a new folder; one
that changes none keeps it. The element is unchanged: it still resolves
the data next to the two files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

View guided diff | Turn on auto-fix | Re-trigger cubic

@alekswebnet
alekswebnet merged commit 4000807 into master Nov 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant