Skip to content

Commit 289c86d

Browse files
committed
fix(start-plugin-core): strip middleware chain on client builds
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.
1 parent 5a81726 commit 289c86d

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

packages/start-plugin-core/src/start-compiler-plugin/handleCreateServerFn.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export function handleCreateServerFn(
227227

228228
for (const candidate of candidates) {
229229
const { path: candidatePath, methodChain } = candidate
230-
const { inputValidator, handler } = methodChain
230+
const { inputValidator, handler, middleware } = methodChain
231231

232232
// Check if the call is assigned to a variable
233233
if (!candidatePath.parentPath.isVariableDeclarator()) {
@@ -284,6 +284,13 @@ export function handleCreateServerFn(
284284
}
285285
}
286286

287+
// Handle middleware - remove on client to prevent server-only
288+
// dependencies (auth, DB, etc.) from leaking into the client bundle.
289+
// Middleware only executes server-side; the client RPC stub skips it.
290+
if (middleware && context.env === 'client') {
291+
stripMethodCall(middleware.callPath)
292+
}
293+
287294
const handlerFnPath = handler?.firstArgPath
288295

289296
if (!handler || !handlerFnPath?.node) {

0 commit comments

Comments
 (0)