Repository: next.js
Issue: Redirect from Server Action takes precedence over the Redirect from Middleware when they are sequential
Labels: bug,Middleware

### Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/dqqhy7

### To Reproduce

1. Visit the [demo](https://dqqhy7-3000.csb.app/)
2. Click the Trigger A redirect button
3. You'll be redirected. The search params you'll see have hello='xyz' (This was expected to be hello=xyz&identifier=yolo)
4. Refresh the page, you'll see the expected params

### Current vs. Expected behavior

Current Behavior:

Triggering a redirect from a server action to another route results in the invocation of the middleware, which processes the request and has the ability to redirect again depending on how the request is handled. In this case, search parameters attached to the redirect URL from the server action are being received in the middleware. I’m trying to append an additional search parameter and redirect again, and although the generated URL with the newly added search param appears correct, the page served to the client does not include the newly appended parameter.

Expected Behavior:

I expect the parameter I appended to be available to the client because I attached it and triggered the redirect in the middleware.

In my actual codebase, the server action signs the user in (using signIn from [authjs](https://authjs.dev/) with redirect: false) and then redirects them (using next/navigation) to another route. This route is supposed to generate a temporary identifier and append it to the search parameters. I use middleware to achieve this, as the middleware intercepts the request to the target route (from the server action), checks for the search parameters, and if the identifier is missing, calls my custom backend (using the auth token it just received). The middleware then appends the identifier from the backend response to the search parameters and redirects the request again. The middleware is invoked once more, but this time with the search parameter included in the URL.

The issue is that the client is still being served a page without the appended query parameter.

I’ve found a workaround by avoiding the redirect in the server action and using router.push in the client component that triggered the workflow.

Could someone help me understand why this issue is happening?

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:14:59 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8122
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 18.20.3
  npm: 10.7.0
  Yarn: 1.22.22
  pnpm: 9.12.1
Relevant Packages:
  next: 14.2.6 // There is a newer version (14.2.15) available, upgrade recommended! 
  eslint-config-next: 14.2.6
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: standalone
```


### Which area(s) are affected? (Select all that apply)

Middleware

### Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Other (Deployed)

### Additional context

Tested on v14.2.5, v14.2.6, v14.2.15. Happens locally as well deployed on gcloud with a docker image.