Skip to content

fix(build): match Next.js sourcemap output for "use server" modules - #3817

Closed
james-elicx wants to merge 13 commits into
mainfrom
fix/server-action-client-sourcemaps
Closed

james-elicx wants to merge 13 commits into
mainfrom
fix/server-action-client-sourcemaps

Conversation

@james-elicx

@james-elicx james-elicx commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Summary

@vitejs/plugin-rsc replaces a client-imported "use server" module with createServerReference() 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.map carried the full original server-only module in sourcesContent. 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.

  • Fix (packages/vinext/src/plugins/server-action-client-sourcemap.ts): a build-only plugin for client-consumer environments.
    • Detection: action modules are the ones plugin-rsc itself registered as server references (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.
    • What is kept: a first-running transform hook records hashes of each client module's code, and a last-running enforce: "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, because outputOptions can still enable one. In a chunk that contains an action module, a source keeps its sourcesContent only if that content belongs to one of the chunk's other modules. Everything else is nulled. A data: URL source (a path segment starting with data: up to its comma) carries its content in its name, so its decoded payload is judged on its own, independent of sourcesContent, and the source is redacted unless it is public. A data: sourceRoot is folded into the sources first, every sourcesContent entry is checked (including any past the end of sources), and index maps are scrubbed through their sections[*].map.
    • Why content, not names: matching content rather than source names holds for colliding names (including sourcemapPathTransform), loader-supplied maps that name another or a public file, and custom map locations.
    • Where it scrubs: chunk.map (reassigned, since Rolldown only syncs top-level bundle assignments), the .map asset (true/"hidden", string or byte-backed) and inline data URLs. It scrubs once before every other generateBundle hook, 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 same order run in plugin order). The .map asset is only read when Rolldown sets sourcemapFileName.
    • Coverage: working on the emitted output leaves the load and transform pipelines untouched, so it holds for any loader and for sourcemaps enabled late through 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, the is_react_server_layer || is_development checks from vercel/next.js#76157). Its actions e2e fixture enables productionBrowserSourceMaps and asserts that no .js.map under .next/static contains 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 in sources, with null content, instead of having no mappings at all.

Testing

  • E2E (tests/e2e/app-router/action-owner-production.browser.spec.ts):
    • The checked-in app-action-ownership production build now enables browser sourcemaps, like Next's actions fixture. Every existing ownership test therefore runs against a sourcemapped build.
    • A new test visits pages that import actions from Client Components, including one from a linked package.
    • It follows each served script's sourceMappingURL anonymously and asserts that no served map contains action-only content. The action modules' entries must be null.
    • Positive control: the calling client-button.tsx must keep its content.
    • Like Next, it also scans every emitted map.
  • Unit (tests/server-action-client-sourcemap.test.ts): covers .map assets (string and byte-backed), inline maps, chunk.map reassignment, colliding source names, loader-supplied maps that name a public module or another original, data: URL sources with and without sourcesContent (private redacted, public kept, including a private URL next to public sourcesContent), index maps (emitted and combined sections[*].map), trailing sourcesContent entries, a data: URL spanning sourceRoot and the source, the pre and post generateBundle passes and the plugin-list reordering behind them, a same-named asset for a chunk without an external map, source names that merely contain data:, 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.
  • Without the fix, the new e2e test fails: the served map exposes the "use server" source. With the fix it passes. Also checked by hand with:
    • export * expansion
    • BOM and CRLF prologues
    • dynamic import()
    • real node_modules packages
    • later transforms that emit their own sourcesContent
    • ?query imports of action modules
    • ?raw imports (still raw)
    • maps enabled only by an outputOptions hook
    • a user loader for an action module (still used)
    • a transform that adds the "use server" directive
    • a loader that returns its own map under a different source name
    • a sourcemapPathTransform that collides every source name
    • a later generateBundle plugin reading chunk.map, and one copying the .map asset into a sidecar, including one registered before vinext with its own pre hook
    • a loader map whose source is a data: URL
    • a public module rewritten by an enforce: "pre" transform (keeps its content)
    • a public module whose enforce: "post" transform maps it to another original (keeps that original)
    • a MagicString renderChunk rewrite with includeContent: true (public content kept)

Scope and known limitations

The scrub covers the maps Rolldown and Vite emit: chunk.map, the .map asset, and Rolldown's own inline annotation (//# sourceMappingURL=data:application/json;base64,…). That includes maps from loaders, transforms and renderChunk hooks, which Rolldown composes into them. It does not parse every encoding a third-party generateBundle plugin could re-serialize a map into: //@ or percent-encoded inline annotations, URL-referenced index-map sections, or whitespace-prefixed data: sources. No real Vite plugin produces these.

In a chunk that contains an action, an original that only a renderChunk map introduces, pointing at a file that is not one of the chunk's modules, loses its sourcesContent. This fails safe: the content is nulled, never exposed.

@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.
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 10dbfcb1ed7e10b84002c64d5bc44a060d889211 without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@pkg-pr-new

pkg-pr-new Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@vinext/cloudflare

npm i https://pkg.pr.new/@vinext/cloudflare@3817

create-vinext-app

npm i https://pkg.pr.new/create-vinext-app@3817

@vinext/types

npm i https://pkg.pr.new/@vinext/types@3817

vinext

npm i https://pkg.pr.new/vinext@3817

@cloudflare/workers-response-store

npm i https://pkg.pr.new/@cloudflare/workers-response-store@3817

commit: 6b19b34

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Performance benchmarks

Compared 6b19b34 against base 661f1b6 using alternating same-runner rounds. Next.js was unchanged and skipped.

0 improved · 1 regressed · 6 within ±1.5%

Scenario Framework Baseline Current Change
Client bundle size (gzip) vinext 144.7 KB 144.7 KB ⚫ -0.0%
Client entry size (gzip) vinext 132.5 KB 132.5 KB ⚫ -0.0%
Dev server cold start vinext 2.77 s 2.75 s ⚫ -0.4%
Dev server cold start (large Pages Router app) vinext 26.81 s 26.54 s ⚫ -1.0%
Production build time vinext 3.39 s 3.50 s 🔴 +3.3%
RSC entry closure size (gzip) vinext 133.3 KB 133.3 KB ⚫ +0.0%
Server bundle size (gzip) vinext 236.7 KB 236.7 KB ⚫ +0.0%

View detailed results and traces

🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor
Example Preview Production Original
app-router-cloudflare preview production
pages-router-cloudflare preview production
app-router-playground preview production original
realworld-api-rest preview production
nextra-docs-template preview production
benchmarks preview production
hackernews preview production original
response-store-demo preview production
workers-cache preview production
kv preview production
static-assets-cache preview production
static-assets-pages preview production
static-assets-pages-i18n preview production
static-export preview production
web preview production

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 4 findings.

Posted 4 actionable inline comments on the exact head. No files were modified.

github run

… 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.
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 5236a2b96991c130cd0df3cd17e7aa62ff59f0c6 without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 2 findings.

Posted 2 actionable inline comments on exact head 5236a2b. No files modified. Targeted unit tests pass.

github run

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.
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head e6aa524694777e695a2838eb24da68f70591eaef without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 2 findings.

Posted 2 actionable inline comments on exact head e6aa5246. No files modified.

github run

…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.
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head a67053ff7fa5a5d2ae69f64fe02aae0439f5933d without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 3 findings.

Posted 3 actionable inline comments on exact head a67053ff. Previous findings are resolved. No files modified.

github run

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.
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head f1af46a10cbc9a9ba8b5f3f47e4df4f20513a27f without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 3 findings.

Posted 3 actionable inline comments on exact head f1af46a. Targeted unit tests pass. No files modified.

github run

… 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.
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 5cbaa1b51a0d56afe961d3220d4128ce3958e342 without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 2 findings.

Posted 1 new inline finding. The existing data: source disclosure thread remains unresolved. Targeted tests pass; no files modified.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 1b41c3d without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 2 findings.

Posted 2 actionable inline comments on exact head 1b41c3d. Targeted tests pass; no files modified.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 2dfc38c without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 3 findings.

Posted 3 actionable inline comments on exact head 2dfc38c8. Targeted tests pass; no files modified.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head ec4544f without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 3 findings.

Posted 2 new inline findings. The existing late-transform provenance finding remains unresolved. Targeted tests pass; no files modified.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head b41a51a without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 2 findings.

Posted 2 actionable inline comments on exact head b41a51a. Targeted unit tests pass; no files modified.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 23ea842 without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@james-elicx james-elicx changed the title fix(build): keep "use server" source out of client sourcemaps fix(build): match Next.js sourcemap output for "use server" modules Oct 9, 2026
@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 4 findings.

Posted 4 actionable inline comments on exact head 23ea8426. Targeted unit tests pass; no files modified.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head c43553c without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Review: 1 findings.

Posted 1 actionable inline comment. Targeted unit tests pass; no files modified.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 6b19b34 without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

@james-elicx Bonk workflow timed out.

View workflow run · To retry, trigger Bonk again.

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 6b19b34 without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

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