Repository navigation
fix(build): match Next.js sourcemap output for "use server" modules - #3817
james-elicx wants to merge 13 commits into
Conversation
@vitejs/plugin-rsc replaces client-imported "use server" modules with server-reference proxies, but Rolldown still seeds each module's sourcemap chain with the loaded file, so enabled browser sourcemaps published the full server-only module in sourcesContent. Load "use server" modules in client builds with an empty sourcemap so the proxy maps to nothing and the module is dropped from the emitted map. This matches Next.js, which strips server action spans from production client output and keeps them in development and server builds.
|
/bigbonk review for issues |
@vinext/cloudflare
create-vinext-app
@vinext/types
vinext
@cloudflare/workers-response-store
commit: |
Performance benchmarksCompared 0 improved · 1 regressed · 6 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
|
|
Review: 4 findings. Posted 4 actionable inline comments on the exact head. No files were modified. |
… config gate Run the empty-map loader with the post plugins so other loaders keep the modules they own, while still covering query-suffixed ids ahead of builtin:vite-load-fallback. Detect "use server" modules from the parsed AST instead of a prologue scanner, and drop the sourcemap config check so maps enabled through outputOptions are covered too.
|
/bigbonk review for issues |
|
Review: 2 findings. Posted 2 actionable inline comments on exact head |
Replace the load-chain hook with output scrubbing: record client modules whose loaded code has a "use server" directive (the same AST check as plugin-rsc), then null their sourcesContent in every emitted map asset and inline map. This holds for any loader, leaves the load pipeline untouched, and covers sourcemaps enabled late through outputOptions.
|
/bigbonk review for issues |
|
Review: 2 findings. Posted 2 actionable inline comments on exact head |
…known sources Detect action modules from plugin-rsc's own server-reference registrations instead of re-parsing loaded code, so directives added by earlier transforms are covered. In chunks that contain an action, keep sourcemap content only for sources matching the chunk's other modules, which also covers loader maps that name a different original file.
|
/bigbonk review for issues |
|
Review: 3 findings. Posted 3 actionable inline comments on exact head |
Match sourcesContent against hashes of the chunk's non-action modules' loaded code instead of source names, so colliding names, loader maps and custom map locations cannot keep action source. Reassign the scrubbed chunk.map so later plugins and programmatic output see it too.
|
/bigbonk review for issues |
|
Review: 3 findings. Posted 3 actionable inline comments on exact head |
… map assets Record hashes of the originals a module's combined map already points to, so public modules rewritten by loaders or enforce-pre transforms keep their content. Redact dropped data: URL sources, which carry their content in the name, and decode byte-backed .map assets before parsing.
|
/bigbonk review for issues |
|
Review: 2 findings. Posted 1 new inline finding. The existing |
|
/bigbonk review for issues |
|
Review: 2 findings. Posted 2 actionable inline comments on exact head |
…d match only data: URL segments
|
/bigbonk review for issues |
|
Review: 3 findings. Posted 3 actionable inline comments on exact head |
…p public data: sources
|
/bigbonk review for issues |
|
Review: 3 findings. Posted 2 new inline findings. The existing late-transform provenance finding remains unresolved. Targeted tests pass; no files modified. |
|
/bigbonk review for issues |
|
Review: 2 findings. Posted 2 actionable inline comments on exact head |
|
/bigbonk review for issues |
|
Review: 4 findings. Posted 4 actionable inline comments on exact head |
|
/bigbonk review for issues |
|
Review: 1 findings. Posted 1 actionable inline comment. Targeted unit tests pass; no files modified. |
… tracker after them
|
/bigbonk review for issues |
|
@james-elicx Bonk workflow timed out. View workflow run · To retry, trigger Bonk again. |
|
/bigbonk review for issues |
|
LGTM! |
Summary
@vitejs/plugin-rscreplaces a client-imported"use server"module withcreateServerReference()proxies, so the action's implementation never reaches browser JavaScript. The browser sourcemap still contained it, though. With client sourcemaps enabled (environments.client.build.sourcemap, opt-in), each chunk's.js.mapcarried the full original server-only module insourcesContent. That includes anything embedded in that source, such as keys or internal logic. Sourcemaps are normal static assets, so anyone could download them.The cause is that Rolldown records each module's loaded source, from whichever loader returned it, as the original in the chunk's sourcemap. Later transforms can't replace it.
packages/vinext/src/plugins/server-action-client-sourcemap.ts): a build-only plugin for client-consumer environments.manager.serverReferences, the same API the action-owner manifest uses). Its client transform drops the claim for every module it doesn't proxy, so this follows plugin-rsc's own classification, including directives that an earlier transform adds.transformhook records hashes of each client module's code, and a last-runningenforce: "post"tracker adds every original its combined map points to after all transforms (from loaders or any transform, such as styled-jsx). The combined map is read even when no sourcemap is configured yet, becauseoutputOptionscan still enable one. In a chunk that contains an action module, a source keeps itssourcesContentonly if that content belongs to one of the chunk's other modules. Everything else is nulled. Adata:URL source (a path segment starting withdata:up to its comma) carries its content in its name, so its decoded payload is judged on its own, independent ofsourcesContent, and the source is redacted unless it is public. Adata:sourceRootis folded into the sources first, everysourcesContententry is checked (including any past the end ofsources), and index maps are scrubbed through theirsections[*].map.sourcemapPathTransform), loader-supplied maps that name another or a public file, and custom map locations.chunk.map(reassigned, since Rolldown only syncs top-level bundle assignments), the.mapasset (true/"hidden", string or byte-backed) and inline data URLs. It scrubs once before every othergenerateBundlehook, so none of them sees the action source, and once after all of them. To guarantee that order, the plugin moves itself to the front of the resolved plugin list and its tracker to the end (hooks with the sameorderrun in plugin order). The.mapasset is only read when Rolldown setssourcemapFileName.outputOptions. The proxy's mappings still name the action module, but without its content.Next.js parity
Next.js drops the original spans when it compiles server actions for the client layer in production. It keeps them in development and in server builds (
crates/next-custom-transforms/src/transforms/server_actions.rs, theis_react_server_layer || is_developmentchecks from vercel/next.js#76157). Its actions e2e fixture enablesproductionBrowserSourceMapsand asserts that no.js.mapunder.next/staticcontains action-only content (test/e2e/app-dir/actions/app-action.test.ts, "should not expose action content in sourcemaps", vercel/next.js#76129). This PR applies the same rule: production client builds only, with dev and server builds unchanged. The one difference is that the action module stays listed insources, withnullcontent, instead of having no mappings at all.Testing
tests/e2e/app-router/action-owner-production.browser.spec.ts):app-action-ownershipproduction build now enables browser sourcemaps, like Next's actions fixture. Every existing ownership test therefore runs against a sourcemapped build.sourceMappingURLanonymously and asserts that no served map contains action-only content. The action modules' entries must benull.client-button.tsxmust keep its content.tests/server-action-client-sourcemap.test.ts): covers.mapassets (string and byte-backed), inline maps,chunk.mapreassignment, colliding source names, loader-supplied maps that name a public module or another original,data:URL sources with and withoutsourcesContent(private redacted, public kept, including a private URL next to publicsourcesContent), index maps (emitted and combinedsections[*].map), trailingsourcesContententries, adata:URL spanningsourceRootand the source, the pre and postgenerateBundlepasses and the plugin-list reordering behind them, a same-named asset for a chunk without an external map, source names that merely containdata:, public modules whose combined map points to another original (with no sourcemap configured at transform time), and the cases left unchanged: chunks without server references, and server builds."use server"source. With the fix it passes. Also checked by hand with:export *expansionimport()node_modulespackagessourcesContent?queryimports of action modules?rawimports (still raw)outputOptionshook"use server"directivesourcemapPathTransformthat collides every source namegenerateBundleplugin readingchunk.map, and one copying the.mapasset into a sidecar, including one registered before vinext with its own pre hookdata:URLenforce: "pre"transform (keeps its content)enforce: "post"transform maps it to another original (keeps that original)renderChunkrewrite withincludeContent: true(public content kept)Scope and known limitations
The scrub covers the maps Rolldown and Vite emit:
chunk.map, the.mapasset, and Rolldown's own inline annotation (//# sourceMappingURL=data:application/json;base64,…). That includes maps from loaders, transforms andrenderChunkhooks, which Rolldown composes into them. It does not parse every encoding a third-partygenerateBundleplugin could re-serialize a map into://@or percent-encoded inline annotations, URL-referenced index-map sections, or whitespace-prefixeddata:sources. No real Vite plugin produces these.In a chunk that contains an action, an original that only a
renderChunkmap introduces, pointing at a file that is not one of the chunk's modules, loses itssourcesContent. This fails safe: the content is nulled, never exposed.