Skip to content

Client middleware context (sendContext) is not passed to server middleware when a server function accepts FormData. #5913

@empotts

Description

@empotts

Which project does this relate to?

Start

Describe the bug

Issue: Client middleware context (sendContext) is not passed to server middleware when a server function accepts FormData.
Expected Behavior
When a middleware defines both client and server handlers:
The client handler runs and sends context via sendContext (line 12-15 in middleware.ts)
The server handler receives that context in its context parameter (line 18 in middleware.ts)
The context should be available throughout the request chain
Actual Behavior
When a server function accepts FormData:
The client middleware runs and creates testString via sendContext
The server middleware runs, but context.testString is undefined
This causes an error: "BUG REPRODUCED: testString is missing from server middleware context!"
Root Cause
The bug appears to be in how TanStack Start serializes and transmits context when the request body is FormData. The sendContext data from the client middleware is not being properly included in the request when FormData is used, unlike when JSON or other data types are sent.

Your Example Website or App

https://github.com/empotts/tanstack-middleware-formdata-bug
This stackblitz doesnt really work for either server function but download the repo, pnpm i, pnpm dev
https://stackblitz.com/~/github.com/empotts/tanstack-middleware-formdata-bug

Steps to Reproduce the Bug or Issue

Click the button for the formdata server function and it fails.

Expected behavior

I expect the context to be passed from the client to the server middleware. This works for normal server functions but not with form data.

Screenshots or Videos

Image

[SERVER MIDDLEWARE] Received context: {
t: { t: 10, i: 0, p: { k: [Array], v: [Array], s: 1 }, o: 0 },
f: 31,
m: []
}

Server Fn Error!

Error: BUG REPRODUCED: testString is missing from server middleware context!
at context (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/src/middleware.ts:23:19)
at applyMiddleware (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-client-core@1.136.17/node_modules/@tanstack/start-client-core/src/createServerFn.ts:638:10)
at next (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-client-core@1.136.17/node_modules/@tanstack/start-client-core/src/createServerFn.ts:217:14)
at executeMiddleware (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-client-core@1.136.17/node_modules/@tanstack/start-client-core/src/createServerFn.ts:235:10)
at AsyncFunction._executeServer (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-client-core@1.136.17/node_modules/@tanstack/start-client-core/src/createServerFn.ts:147:20)
at message (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/src/server-functions.ts:18:13)
at eval (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/server-functions-handler.ts:97:24)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async eval (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/server-functions-handler.ts:68:20)
at async handleServerAction (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/server-functions-handler.ts:66:20)
at async eval (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/createStartHandler.ts:154:26)
at async eval (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/createStartHandler.ts:143:28)
at async eval (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/createStartHandler.ts:430:22)
at async next (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/createStartHandler.ts:448:16)
at async startRequestResolver (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-server-core@1.136.17/node_modules/@tanstack/start-server-core/src/createStartHandler.ts:244:19)
at async Object.fetch (/Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+react-start@1.136.18_react-dom@19.2.0_react@19.2.0__react@19.2.0_vite@7.2.2
@7024799955be5bc3fbc41ef4552184ce/node_modules/@tanstack/react-start/dist/plugin/default-entry/server.ts:16:14)
at async file:///Users/epotts/Downloads/tanstack-middleware-formdata-bug/node_modules/.pnpm/@TanStack+start-plugin-core@1.136.18
@TanStack+react-router@1.136.18_react-dom@19.2.0_r_b1f65b1edd713246bb8f1462dfe4c854/node_modules/@tanstack/start-plugin-core/dist/esm/dev-server-plugin/plugin.js:64:30

Platform

  • Router / Start Version: [e.g. 1.136.17]
  • OS: [e.g. macOS]
  • Browser: [e.g. Chrome,]

Since RC

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions