vercel / vercel/next.js

Server Actions don't respect the `NextResponse.redirect` from Middleware

Open
#65,394 4 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Middleware
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/server-action-and-middleware-redirect-j54l2f

To Reproduce

See the reproduction link.

TL;DR:

  1. Have a server action that gets called from a client component's button
  2. Add a middleware that returns NextResponse.redirect() when request.method === 'POST' (P.S. the condition could be authenticating the user session)
  3. Click the button to trigger the server action
  4. The page doesn't redirect as instructed in the middleware
Current vs. Expected behavior

Current: The page doesn't redirect
Expected: The page redirects as instructed in the middleware

Provide environment information
Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 8.15.4
Relevant Packages:
  next: 14.3.0-canary.40 // Latest available version is detected (14.3.0-canary.40).
  eslint-config-next: 14.2.1
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.5
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

The reason is that the server action's fetch call follows the redirect:
https://github.com/vercel/next.js/blob/1ae14bafdb197a712c0bbb9b6570f4d04bd849e3/packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts#L62-L80

There is also no consistent way to handle it (catching the server action errors) on the client side. If the redirected page responds 2xx, the server action doesn't throw an error.

A workaround would be mimicking the server actions' redirect() response in the middleware:

  if (request.headers.get('Accept') === 'text/x-component') {
    return new NextResponse(null, {
      status: 303,
      headers: {
        'X-Action-Redirect': url
      }
    });
  }

However, this workaround feels very hacky.

Related issue: #62469

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior using the linked CodeSandbox with a client-triggered server action and POST middleware returning NextResponse.redirect(). Read packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts around lines 62–80, where the action fetch follows redirects. Done means a middleware redirect reliably navigates the page during a server action without requiring the proposed workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, react, typescript
Domain
full-stack, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.