fix(start-plugin-core): strip middleware chain on client builds#7131
fix(start-plugin-core): strip middleware chain on client builds#7131afonsojramos wants to merge 1 commit intoTanStack:mainfrom
Conversation
The `createServerFn` compiler correctly replaces `.handler(fn)` with
`.handler(createClientRpc('hash'))` for client builds, but leaves the
`.middleware([...])` chain intact. This keeps server-only imports
(auth middleware, DB connections, etc.) alive in the client bundle.
With esbuild (Vite 7), these dead references were tree-shaken. With
Rolldown (Vite 8), the middleware functions remain as live references,
pulling the entire server dependency tree into the client bundle. This
causes runtime crashes like `ReferenceError: Buffer is not defined`
when Node.js-only packages (e.g. `postgres`) end up in the browser.
The fix strips `.middleware()` calls on client builds, matching the
existing pattern used for `.inputValidator()`. Since middleware only
executes server-side and the client RPC stub skips it entirely, the
middleware chain is dead code on the client.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe compiler now extracts middleware from server function method chains and strips middleware calls during client-side builds while preserving them server-side. Handler and input validator behavior remains unchanged. New tests verify middleware removal in client compilations and preservation in server compilations. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Duplicate, reopening #7125. |
Bundle Size Benchmarks
Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better. |
Summary
The
createServerFncompiler correctly replaces.handler(fn)with.handler(createClientRpc('hash'))for client builds, and strips.inputValidator()— but leaves the.middleware([...])chain intact. This keeps server-only imports (auth middleware, DB connections, etc.) alive in the client bundle.With esbuild (Vite 7/rolldown-vite), these dead references were tree-shaken. With Rolldown (Vite 8), the middleware functions remain as live symbol references, pulling the entire server dependency tree into the client bundle. This causes runtime crashes like:
...when Node.js-only packages (e.g.
postgres) end up in the browser, preventinghydrateRootfrom completing.Fix
Strip
.middleware()calls on client builds usingstripMethodCall, matching the existing pattern for.inputValidator(). Since middleware only executes server-side and the client RPC stub skips it entirely, the middleware chain is dead code on the client.Before (client build output):
After (client build output):
This makes the middleware imports (
requireAuth,requirePermission) unreferenced, allowing the bundler to tree-shake the entire server dependency chain.Tests
Added two tests to
compiler.test.ts:strips .middleware() from createServerFn on client builds— verifies.middleware()and the middleware import are removed from client outputpreserves .middleware() on server builds— verifies server builds keep middleware intactContext
createServerFn().middleware([...])with Vite 8Summary by CodeRabbit
Improvements
Tests