router icon indicating copy to clipboard operation
router copied to clipboard

Server function with middleware causes serialization error when a redirect is thrown in the function handler

Open Julius-Java opened this issue 1 month ago • 3 comments

Which project does this relate to?

Start

Describe the bug

I get serialization errors whenever I chain a middleware to a server function and perform any redirect in that server function handler but the redirect works fine and no serialization error occurs when I remove the middleware.

export const createOrgFn = createServerFn({ method: 'POST' })
  .middleware([authMiddleware])
  .inputValidator(OrgFormSchema)
  .handler(async ({ data }) => {
    const api = await client()
    const res = await api.post('/orgs/create', data)
    if (res.data) {
      throw redirect({
        to: '/$orgSlug',
        params: { orgSlug: res.data.data.organization.slug },
      })
    }
  })

Error creating organization The value [object Response] of type "object" cannot be parsed/serialized. There are few workarounds for this problem: - Transform the value in a way that it can be serialized. - If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.

Your Example Website or App

https://codesandbox.io/p/devbox/dank-morning-xdnwrm?file=%2Fsrc%2Froutes%2Fdemo%2Fstart.server-funcs.tsx%3A96%2C35

Steps to Reproduce the Bug or Issue

  1. Create a server function
  2. Chain a request middleware to that function
  3. Throw a redirect in the handler of that server function

Expected behavior

I expect the redirect to work fine and no serialization error.

Screenshots or Videos

No response

Platform

  • Router / Start Version: [^1.134.12]
  • OS: [Linux]
  • Browser: [Chrome ]
  • Browser Version: [91.1]
  • Bundler: [vite]
  • Bundler Version: [7.0.0]

Additional context

Julius-Java avatar Nov 21 '25 09:11 Julius-Java

@Julius-Java If this issue is approved I would like to work on this!

Divyapahuja31 avatar Nov 22 '25 13:11 Divyapahuja31

We are experiencing this as well.

Just a note, after updating to 1.139.3 this still doesn't work but the error is different:

Server Fn Error!

E [Error]: Seroval Error (specific: 1)

andrew-from-toronto avatar Nov 25 '25 13:11 andrew-from-toronto

We are experiencing this as well.

Just a note, after updating to 1.139.3 this still doesn't work but the error is different:

Server Fn Error!

E [Error]: Seroval Error (specific: 1)

Have you been able to fix it?

Julius-Java avatar Nov 27 '25 18:11 Julius-Java