-
Notifications
You must be signed in to change notification settings - Fork 26.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue when redirecting from Server Action -> Route Handler -> new page #59217
Comments
@MDFARHYN This doesn’t solve the issue. The reason it’s a route is that I want to determine redirect path dynamically server-side based on session, database etc. |
This problem also occurs when redirecting from middleware. Moving the code in repro from route.ts to middleware.ts has the same effect |
Looks like a duplicate of #58281? |
@MDFARHYN Please see above. Server Actions are unusable with Middleware because this bug is still not fixed. |
I would recommend doing this in a Server Action and not having a redirect -> redirect -> page. Any reason you can't do that? |
@leerob Can you tell me how I can keep my Server Actions DRY (Don't Repeat Yourself) without using middleware? The whole point is that every single server action should go through rate limiting and the internationalization middleware should work without breaking whenever a user is redirected to the blocked page after being rate limited too many times. |
For some reading on the merits of DRY: https://overreacted.io/the-wet-codebase/ But to your point, can't it a be a function that's called in Middleware and your Server Action? |
@leerob it does NOT work if I use it in Middleware. That's the problem. If I put it in the middleware I get the error as shown at the bottom of the image I shared. I haven't tried to do the redirect directly in the server action because I don't want to have to copy and past the rate limiting code in every single server action because it doesn't really make sense. This also violates the single responsibility rule. The server action shouldn't also have to figure out the rate limiting logic. |
Having the same issue (without the route handler step), I redirect in server action to /page and expect the middleware logic to add locales to kick in to end up on /en/page |
This comment has been minimized.
This comment has been minimized.
Similar problem with using a serverAction redirect whilst having middleware rewrites (e.g when doing something similar to the Platform Starter Kit) e.g: middleware.ts
someServerAction.ts
The URL will render correctly, however the app router will ignore the rewrite, and try and fetch the route from the wrong dir. oddly enough - the middleware function is still being called prior to render. |
Link to the code that reproduces this issue
https://github.com/cloudkite/next-redirect-bug
To Reproduce
npx next dev
Current vs. Expected behavior
Expected:
When clicking submit page server action redirects to /redirect_route which in turn redirects to /hello.
Actual
URL changes to /redirect_route but displays the content from /hello
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 Binaries: Node: 20.8.1 npm: 10.1.0 Yarn: 1.22.15 pnpm: N/A Relevant Packages: next: 14.0.4-canary.37 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router
Additional context
No response
The text was updated successfully, but these errors were encountered: