Repository navigation
fix: Vite plugin data URLs change when the data changes - #88
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 asimmutable, so a returning visitor's browser can keep an older version's files after an upgrade. PDF.js can't use them: theopenjpeg.wasmpublished in 4.4.2 needs 17 imports, and the 4.6.0 worker provides 8.The plugin now moves
viewer.mjsandpdf.mjsinto 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 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
outputOptionswrapsassetFileNames. 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 tostatic/viewer/lands instatic/viewer/pdfjs-viewer-…/. The two runtime files are still recognised by their bytes.viewer.mjsandpdf.mjs, and those two moved with it.Effect on 4.6.0 users
The data moves from
assets/wasm/…toassets/pdfjs-viewer-<hash>/wasm/…. A cache rule written per the 4.6.0 docs to excludeassets/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 inwasm/openjpeg.wasmbuilds 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 insideassets/, and that an app's ownassetFileNamesdirectories are kept.Each test fails when the fix is broken. Four deliberate breakages, each restored afterwards:
viewer.mjsmovesVite 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-runincludesplugins/vite.js.Checklist
🤖 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.wasmcould be served to a newer worker. The plugin now wraps the app'sassetFileNamesand movesviewer.mjsandpdf.mjsinto 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.assets/wasm/...toassets/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.viewer.mjsandpdf.mjs.Written for commit 0c35fbe. Summary will update on new commits.