Skip to content

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

@Julius-Java

Description

@Julius-Java

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

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