Skip to content

fix: invalidate chunk hashes on renderBuiltUrl change (fix #13996) - #21433

Closed
hartz89 wants to merge 1 commit into
vitejs:mainfrom
hartz89:render-built-url-fix-hashing
Closed

fix: invalidate chunk hashes on renderBuiltUrl change (fix #13996) #21433
hartz89 wants to merge 1 commit into
vitejs:mainfrom
hartz89:render-built-url-fix-hashing

Conversation

@hartz89

@hartz89 hartz89 commented Jan 16, 2026

Copy link
Copy Markdown

Summary

Fixes #13996. When experimental.renderBuiltUrl changes, JS and CSS chunk hashes now invalidate properly.

  • JS chunks: Uses Rollup's augmentChunkHash hook to include the stringified renderBuiltUrl function in hash computation
  • CSS assets: Injects a hash of renderBuiltUrl into CSS content via finalizeCss (before the asset filename hash is computed), then strips it in generateBundle so it doesn't appear in shipped CSS

Known limitation: Hash computation uses Function.prototype.toString(), so only the function's source text is captured. If renderBuiltUrl closes over external state (e.g. process.env.CDN_URL) without referencing it in the function body, changing that value won't produce new filenames — the function body itself must change. This is noted in a code comment and matches the general contract of augmentChunkHash.

Testing

  • Unit tests in build.spec.ts verifying that different renderBuiltUrl functions produce different output hashes for both JS chunks and CSS assets
  • New playground/render-built-url integration test running two real builds with different renderBuiltUrl configs and asserting filenames differ for both JS chunks and CSS assets
  • Dogfooded in a production app using renderBuiltUrl for CDN asset serving — confirmed all 19 JS chunks and 3 CSS assets received new hashes after modifying the function

@hartz89 hartz89 changed the title fix: Include renderBuiltUrl contents in JS chunk hashes fix: Include renderBuiltUrl contents in JS + CSS chunk hashes Jan 16, 2026
@hartz89
hartz89 force-pushed the render-built-url-fix-hashing branch from 9926d74 to d7f5f21 Compare March 19, 2026 21:15
@hartz89 hartz89 changed the title fix: Include renderBuiltUrl contents in JS + CSS chunk hashes fix: include renderBuiltUrl contents in JS + CSS chunk hashes Mar 19, 2026
@hartz89 hartz89 changed the title fix: include renderBuiltUrl contents in JS + CSS chunk hashes fix: invalidate chunk hashes on renderBuiltUrl change (fix #13996) Mar 19, 2026
@hartz89
hartz89 marked this pull request as ready for review March 19, 2026 21:38
@hartz89
hartz89 force-pushed the render-built-url-fix-hashing branch from d7f5f21 to d04500f Compare March 19, 2026 21:42
@hartz89
hartz89 force-pushed the render-built-url-fix-hashing branch 2 times, most recently from 5a939b1 to b4b9d25 Compare April 20, 2026 01:29
JS chunks use the `augmentChunkHash` hook to mix the stringified
`renderBuiltUrl` function into each chunk's hash. CSS assets inject a
short hash of the function into CSS content during `finalizeCss` (stripped
before emit) to influence the content-based filename hash.

Note: hashing uses `Function.prototype.toString()`, so only the function
source text is captured. Closed-over variables (e.g. `process.env`) are
not visible — changing external state without changing the function body
will not produce new filenames.
@hartz89
hartz89 force-pushed the render-built-url-fix-hashing branch from b4b9d25 to cec94cd Compare April 20, 2026 01:38
@sapphi-red

Copy link
Copy Markdown
Member

Thanks for the PR.
I've made a PR that resolves the issue without the limitation in #22888, #22984.

@sapphi-red sapphi-red closed this Jul 31, 2026
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.

Chunk hash did not change when I only update experimental.renderBuiltUrl function.

2 participants