Repository navigation
fix(use-cache): compile .mts cache modules like other source files - #4627
Conversation
… 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.
|
/bigbonk review for issues Please review exact head |
@vinext/cloudflare
create-vinext-app
@vinext/types
vinext
@cloudflare/workers-response-store
commit: |
|
Performance benchmarksCompared 0 improved · 2 regressed · 5 within ±1.5%
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)(?:\?.*)?$/; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)(?:\?.*)?$/; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Review: 2 findings. Both findings were posted inline. Targeted transform tests pass. |
|
/bigbonk review for issues Please review exact head |
|
LGTM! |
There was a problem hiding this comment.
✅ 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
What
The
"use cache"transform (vinext:server-function-directives) only matched.js/.jsx/.ts/.tsx/.mjsmodule ids. A file-level"use cache"module written as.mtswas never compiled: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.cjsand.ctsstay out, as before: the transform emits ES module syntax, and Vite doesn't strip TypeScript from.ctsbefore 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.codeConditionis/\.(tsx|ts|js|cjs|mjs|jsx)$/. The app browser layer rule uses it to apply the SWC loader that runs the server actions transform.get_server_actions_transform_rulegoes throughget_ecma_transform_rule, which matchesmatch_js_extension:.js .jsx .ts .tsx .mjs .cjs.vinext honours the directive in
.mtsmodules 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):.js.jsx.mjs.mts.cts(ESM syntax).cjs(ESM syntax)Known limitation (unchanged from main): a
.ctsmodule written with ES module syntax and a"use cache"directive is still not transformed. Turbopack rejects ES module syntax in.ctsfiles, and handling.ctswould mean lowering its TypeScript and telling ES module sources apart from CommonJS ones before this hook.Tests
app-basic'suse-cache-client-importpage gains a"use cache".mtsmodule that its Client Component calls.tests/app-router-production-build.test.ts: the client bundle includes the caller but none of the.mtsmodule'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 buildand dev cold start (first 200 for/use-cache-client-import,.vitecache cleared). 6 runs each, alternating main-first and branch-first, medians:The difference is noise. The only newly matched module in the fixture is the new
.mtsfile. No.mts/.cts/.cjsdependency file contains"use cache", and thenode_moduleshook keeps its nativecode: "use cache"filter.