fix(e2e): preserve RSC server asset URLs - #2291
Merged
Merged
Conversation
Resolve server assets relative to the emitted chunk with newer Vite versions. Keep browser asset URLs handled by Vite.
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
commit: |
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.
Resolve server assets relative to the emitted chunk with newer Vite versions.
Keep browser asset URLs handled by Vite.
Resolve server assets relative to the emitted chunk with newer Vite versions.
Keep browser asset URLs handled by Vite.
Background
e2e/fixtures/rsc-assetcovers asset references originating inside an RSC, intwo directions that have to resolve differently within the same
rscenvironment:
new URL('./test-server.txt', import.meta.url)+readFileSyncneeds a URLthe Node process can open on disk, relative to the emitted chunk.
./test-client.txt?no-inlinerendered as an<a href>needs a public URL thebrowser can fetch (
/assets/...).Vite doesn't handle
new URL(..., import.meta.url)for SSR, so the fixture shipsits own plugin that rewrites the call into
this.emitFile()+new URL(import.meta.ROLLUP_FILE_URL_<id>). That relied on Rollup's defaultresolveFileUrl, which producesnew URL('./x', import.meta.url).href.vitejs/vite#22888 ("use
import.meta.ROLLDOWN_FILE_URL_*for assets in JS")added a
resolveFileUrlhook tovite:asset. It was written for Vite's ownemitters, but
resolveFileUrlis a global Rollup hook, so it also interceptsreferences emitted by user plugins. Those have no
asFileUrlmetadata attached,so they fall through to
toOutputFilePathInJS, which for a server-consumerenvironment always returns a base-joined path.
The fixture's server chunk therefore built as:
new URL()on a root-relative path with no base throws, so the RSC render failedwith
TypeError: Invalid URL,data-testid="server-file"never rendered, and[chromium-prd] rsc-asset › basictimed out. The client?no-inlinehalf wasunaffected. Failing job:
This change
The plugin implements its own
resolveFileUrl, scoped to the reference IDs itemitted, and returns
nullfor everything else so Vite continues to handlebrowser asset URLs.
enforce: 'pre'is required becauseresolveFileUrlis afirst hook and
vite:assetis registered ahead of normal user plugins, sowithout it the hook is never consulted.
Whether Vite intends to intercept third-party file references is a question for
upstream; this fixture works either way, since its hook returns first.