Skip to content

Redirect support in Server Functions for form actions #3820

@nilsbunger

Description

@nilsbunger

Which project does this relate to?

Start

Describe the bug

When using a server function as a form action and throwing a redirect:

export const handleForm = createServerFn({
  method: "GET",
}).handler(async (ctx) => {
  throw redirect({ to: "/" })
})

// Usage
<form action={handleForm.url}>
  {/* form fields */}
</form>

after submitting the form, the user gets a page with the URL of the server action, and this content:

{"to":"/","isRedirect":true,"statusCode":307,"reloadDocument":false}

This action handler syntax comes from Tanstack Form's guide for integrating with Start.

Expected behavior

The server ideally would respond with a proper 302/307 redirect status when using throw redirect() in the server action in this case, similar to the behavior if it was wrapped with useServerFn().

Platform

  • OS: macOS
  • Browser: Chrome

Additional context

Workaround

Manually throwing a raw Response in the server function works, but loses type safety:

throw new Response("ok", { status: 302, headers: { Location: "/" } })

This workaround also can't distinguish between how the server function was called, so it causes an unnecessary server re-render if it was called from a JS client.

@schiller-manuel asked me to write this bug report based on this discord thread.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions