Skip to content

fix(use-cache): compile .mts cache modules like other source files - #4627

Merged
james-elicx merged 2 commits into
mainfrom
fix/use-cache-mts-client-transform
Nov 9, 2026
Merged

james-elicx merged 2 commits into
mainfrom
fix/use-cache-mts-client-transform

Conversation

@james-elicx

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

Copy link
Copy Markdown
Member

What

The "use cache" transform (vinext:server-function-directives) only matched .js/.jsx/.ts/.tsx/.mjs module ids. A file-level "use cache" module written as .mts was never compiled:

  • on the server its exports weren't wrapped, so nothing was cached;
  • when a Client Component imported it, the browser bundle got the module itself (function bodies and module-scope values) instead of server references, and calls ran in the browser.

This widens both id filters (app sources and node_modules) to every ES module JavaScript and TypeScript extension: js, jsx, ts, tsx, mjs, mts. The layout/template default-export exemption uses the same list. CommonJS .cjs and .cts stay out, as before: the transform emits ES module syntax, and Vite doesn't strip TypeScript from .cts before this hook.

Next.js behaviour

Next.js 16.2.7 skips .mts (and .cts) in its server-function transform under both bundlers. The directive goes unrecognised there: the export is a plain uncached function, and a Client Component import bundles the module normally.

vinext honours the directive in .mts modules instead of ignoring it, as @vitejs/plugin-rsc's "use server" transform (no id filter) already does.

Probe (a "use cache" module imported by both a Server Component page and a Client Component, production builds):

Extension Next webpack Next Turbopack vinext (main) vinext (this PR)
.js .jsx .mjs transformed transformed transformed transformed
.mts not transformed not transformed not transformed transformed
.cts (ESM syntax) not transformed build error (module format) not transformed not transformed
.cjs (ESM syntax) build error build error build error build error

Known limitation (unchanged from main): a .cts module written with ES module syntax and a "use cache" directive is still not transformed. Turbopack rejects ES module syntax in .cts files, and handling .cts would mean lowering its TypeScript and telling ES module sources apart from CommonJS ones before this hook.

Tests

  • app-basic's use-cache-client-import page gains a "use cache" .mts module that its Client Component calls.
  • tests/app-router-production-build.test.ts: the client bundle includes the caller but none of the .mts module's body or constants. It fails on main.
  • tests/e2e/app-router/use-cache.spec.ts (dev): the call goes out as a server action request. It fails on main.
  • tests/e2e/app-router-prod/use-cache.spec.ts (prod): repeated calls return the cached result.
  • tests/use-cache-transform.test.ts: both filters match every ES module extension and skip .cjs/.cts.

Performance

tests/fixtures/app-basic, vinext build and dev cold start (first 200 for /use-cache-client-import, .vite cache cleared). 6 runs each, alternating main-first and branch-first, medians:

main this PR
build 10.76 s 10.54 s
dev cold start 7.01 s 6.62 s

The difference is noise. The only newly matched module in the fixture is the new .mts file. No .mts/.cts/.cjs dependency file contains "use cache", and the node_modules hook keeps its native code: "use cache" filter.

… files

The "use cache" transform only matched .js/.jsx/.ts/.tsx/.mjs ids, so a
file-level "use cache" module written as .mts or .cts was never compiled:
its functions were not cached on the server, and a Client Component that
imported it bundled the module itself instead of server references.

Match every JavaScript and TypeScript module extension, as
@vitejs/plugin-rsc's "use server" transform already does.
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues

Please review exact head be432306292e8c0b9ffc4721236968e17f63c9e1 without modifying or pushing the branch. Only flag issues that affect real-world usage, not contrived edge cases. Report all actionable findings within your 8-minute time limit.

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

@vinext/cloudflare

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

create-vinext-app

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

@vinext/types

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

vinext

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

@cloudflare/workers-response-store

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

commit: 4af2e8a

@github-actions

github-actions Bot commented Nov 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

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Performance benchmarks

Compared 4af2e8a against base 2dce8de using alternating same-runner rounds. Next.js was unchanged and skipped.

0 improved · 2 regressed · 5 within ±1.5%

Scenario Framework Baseline Current Change
Client bundle size (gzip) vinext 153.5 KB 153.5 KB ⚫ -0.0%
Client entry size (gzip) vinext 141.7 KB 141.7 KB ⚫ -0.0%
Dev server cold start vinext 2.16 s 2.21 s 🔴 +1.9%
Dev server cold start (large Pages Router app) vinext 17.64 s 17.55 s ⚫ -0.5%
Production build time vinext 2.74 s 2.79 s 🔴 +1.7%
RSC entry closure size (gzip) vinext 141.5 KB 141.5 KB ⚫ +0.0%
Server bundle size (gzip) vinext 250.7 KB 250.7 KB ⚫ +0.0%

View detailed results and traces

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

// (webpack `codeCondition`, Turbopack `match_js_extension`), which leaves the
// directive unrecognised: uncached on the server, and the module body bundled
// into the browser for a Client Component import.
const SOURCE_MODULE_ID_RE = /\.(?:[cm]?[jt]s|[jt]sx)(?:\?.*)?$/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now sends ordinary typed .cts source into analyzeModule, but Vite’s default OXC transform does not include .cts, and parseProgramAsync is called in JavaScript mode. A normal cache module such as export async function f(value: string) therefore fails in both dev and buildApp() with Expected ,or)but found:. Please either lower .ctsTypeScript before this hook (and ensure the transformed output is emitted as JavaScript) or keep.cts` out of the filter until that pipeline exists; the current filter-only test does not exercise the claimed support.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4af2e8a: .cts is no longer matched, so typed .cts modules build as they do on main.

// into the browser for a Client Component import.
const SOURCE_MODULE_ID_RE = /\.(?:[cm]?[jt]s|[jt]sx)(?:\?.*)?$/;
const NODE_MODULES_SOURCE_MODULE_ID_RE =
/[\\/]node_modules[\\/].*\.(?:[cm]?[jt]s|[jt]sx)(?:\?.*)?$/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including .cjs here breaks inline directives in real CommonJS modules. For example, exports.getData = async function () { "use cache"; ... } is hoisted by this plugin and receives ESM imports/exports, while the production pipeline intentionally skips project-local .cjs CommonJS-to-ESM conversion (index.ts:2673-2682). A real createBuilder().buildApp() then fails with Cannot use import statement outside a module. Please exclude CommonJS-designated files or add a CommonJS-safe transform and an end-to-end .cjs build test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4af2e8a: .cjs is no longer matched, so CommonJS modules with inline directives build as they do on main. The filters now cover only ES module extensions (js, jsx, ts, tsx, mjs, mts), and tests/use-cache-transform.test.ts asserts that both filters skip .cjs/.cts.

@ask-bonk

ask-bonk Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Review: 2 findings.

Both findings were posted inline. Targeted transform tests pass.

github run

@james-elicx james-elicx changed the title fix(use-cache): compile .mts and .cts cache modules like other source files fix(use-cache): compile .mts cache modules like other source files Nov 9, 2026
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues

Please review exact head 4af2e8a87b3ead88000f859a0b64d9e9c727e664 without modifying or pushing the branch. Only flag issues that affect real-world usage, not contrived edge cases. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@james-elicx
james-elicx marked this pull request as ready for review November 9, 2026 11:52

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found: the fix is small, its divergence from Next.js is deliberate and documented, and tests cover it in unit, build and dev/prod e2e runs.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. The change stops .mts "use cache" module bodies from being shipped to the browser, which reduces exposure.


📖 AGENTS.md: ✅ LGTM

Follows the repository guidelines: the Next.js behaviour was checked, the divergence is documented, tests were added and the Vite hook filters stay native.


📋 Code Quality: ✅ LGTM

The regexes match exactly js, jsx, ts, tsx, mjs, mts and keep the (?:\?.*)?$ query suffix. The layout/template exemption uses the same extension set. The node_modules hook keeps its native code filter.


⚡ Performance: ✅ LGTM

The filters stay native and module-scoped. The only newly matched ids are .mts files.


📝 Documentation: ✅ LGTM

The source comment states the divergence from Next.js and why CommonJS extensions are excluded.


Reviewed commit: 4af2e8a8

@james-elicx
james-elicx merged commit 1306ab4 into main Nov 9, 2026
109 checks passed
@james-elicx
james-elicx deleted the fix/use-cache-mts-client-transform branch November 9, 2026 13:30
@github-actions github-actions Bot mentioned this pull request Nov 9, 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.

1 participant