next.js: [FIXED in 15.1.0] next.js 14 redirect() inside a server action can't switch between root layouts (was working in next.js 13)
FIXED IN 15.1.0!
Was fixed in this PR: https://github.com/vercel/next.js/pull/73063
Link to the code that reproduces this issue
https://github.com/meszaros-lajos-gyorgy/nextjs-14-redirect-bug
To Reproduce
- start the development server with
npm run dev - open http://localhost:3000/en in your browser
- Click on the “search” submit button on top of the page next to the text input (it doesn’t matter what you enter into the textfield, it always redirects to the same route -> http://localhost:3000/en/search/hello)
Current vs. Expected behavior
Expected result:
You should land on /en/search/hello as that is the hardcoded redirection inside the server action in services/SiteSearch.service.ts.
( This was the behavior in Next.js 13.4.12, but it also threw an error (see https://github.com/vercel/next.js/issues/53392) )
Actual result:
The server action sends back a redirect instruction among the response headers:
X-Action-Redirect: /en/search/hello
but the page does not go to that path
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct 9 15:34:04 UTC 2
Binaries:
Node: 21.1.0
npm: 10.2.0
Yarn: 1.22.15
pnpm: 6.11.0
Relevant Packages:
next: 14.0.2
eslint-config-next: 14.0.2
react: 18.2.0
react-dom: 18.2.0
typescript: 5.2.2
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
App Router
Additional context
When I try the same but first opening http://localhost:3000/en/search/world and then try to redirect via the server action then it works without any issues.
I’ve also wrote down all of the infos above to the readme in my repo.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 24
- Comments: 69 (3 by maintainers)
Commits related to this issue
- 🐛 fix(login): redirection to other route group the issue already discussed in this GH issue https://github.com/vercel/next.js/issues/58263 not sure whether its bug or the new major architecture o... — committed to humblFINANCE/humblFINANCE-frontend by kimsohyuncans 3 years ago
- Redirection after login is fixed - redirection is fixed using client side redirection due to a known issue in Nextjs 14 https://github.com/vercel/next.js/issues/58263 fixes Fix Redirection after login... — committed to Anas-Ah25/Cakery by marksamfd 2 years ago
I’ve spent way too many frustrating hours on this. For me, the work around is to just have a common root layout with minimum markup.
Project structure:
Example of /app/layout.tsx
Off topic, but I have to admit that I regret choosing Next 13 for a new project. This whole next generation of next.js kinda feels like a work-in-progress/proof-of-concept project rather than something that is production ready.
The error seems to persists on 14.2.5 and even on 15 RC. This is really unfortunate since we need the functionality for multiple root layouts and seamless redirections. Is this something the Next team is actively working on?
This works for me as well. For
next@14.1.0when you have group routes defined in the app it’s required to use an minimumlayout.tsxfor work.Still not working in
14.0.3Having a root layout fixed my problem. My folder looks like this: app -> layout.tsx (I needed to add this root layout) app -> (auth) -> layout.tsx app -> (home) -> layout.tsx
This whole time I thought it was me, but yes this is a pretty major issue for us.
Any update on this? This is a pretty serious bug that is wasting a lot of peoples time before arriving here and realizing it’s not a problem in your own code but a bug in the framework.
Actually, I did get this working as I wanted, bear with me, I am new to NextJs and what I am doing maybe not what is intended but for me, all of my errors have gone and the redirect works as expected along with the different layouts rendering how I want them.
In my scenario I wanted 2 layouts, the anon layout and the landing layout. The only difference between those two is that I also have a Navbar in the landing layout.
@guscsales is correct, you need the root layout and the other layouts become nested layouts (I think).
So what I did is have the layout.tsx in the root of the app, with this being the root layout, it holds all of the top level stuff. (fyi, I have ShadCn also installed) and the other layouts are in route groups (anon) and (landing)
The (anon)/layout.tsx looks like this:-
The (landing)/layout.tsx looks like this:-
In my (anon)/login/page.tsx I have my signin method redirect as below.
Apologies for the messy response, just wanted to let peeps know how I am doing this. Feel free to correct me, as I said, I am new to NextJs.
It maybe that the problem derives from the fact that you can/should only have one
<html><body>so don’t put them in anything else except the root layout.tsx.Same for me. The first 3 hours i thought its about me being fairly new to NextJS but redirecting to some destination within my origin RootLayout works without issues. Its the context switching which breaks the redirect.
This one is pretty ugly because it affects one super mainstream use case “redirect after login”, where its pretty normal that the login page is inside your “website” Routing Group and the target route is something like a dashboard in the “WebApp” Routing Group.
For the time being i will re-architect my loginpage to be a client comoonent which calls a handleSubmit() which calls my server action to auth and then get back JSON to handleSubmit where i will do a Route push. Works but ugly.
This actually does work for me for all redirects apart from
redirect('/')orredirect('').I have two root layouts in seperate groups. Where a server action from the login page does a redirect().
What a pain in the ass if a simple redirect function doesn’t even work properly.
All I want to do is this
This is literally as simple as it gets, and I am met with this error:
Console error:
Uncaught Error: Rendered more hooks than during the previous render.Dear NextJS team: this is an extremely basic functionality, yet you guys are working on such useless “fancy” features like your cute parallel route or “vercel AI”. Fix this basic stuff then go with those fancy, gimmicky BS. @leerob
The issue is still present in
14.0.4-canary.18I’m having this issue as well. Currently using the
app/layout.tsxworkaround.While this is the work around and fixes the issue. The docs clearly state that we should not be doing this.
When using route groups the next docs clearly state
“To create multiple root layouts, remove the top-level layout.js file, and add a layout.js file inside each route group.”
So they either need to fix the documentation or fix the functionality.
Ideally fix the functionality. Or at a minimum update the docs and reflect it as a breaking change from older versions.
Same behavior with
15.0.1People here right, such a basic stuff must just work or fixed urgently. But this bug lives on almost a year and it’s not even in progress of fixing, it’s unbelievable. I have started to doubt whether it was a right choice for a new project.This just saved me from days of heart ache. Bless you good sir
@danesto yep, same error here!
in my case, when I put it inside of a try-catch block it is not working, but when I remove it it is working fine
@meszaros-lajos-gyorgy I have just amended your code slightly below and the redirect works as expected:
Add layout.tsx in src/app:-
Amend src/app/[locale]/layout.tsx to:
Amend src/app/search/layout.tsx:
Amend src/app/search/[search]/page.tsx to get the params:
let us know if it all works for you.
Still broken in
v14.0.5-canary.6.The possible workaround here is that the server action sends back some json data to the client and then the client can do a redirect on client side. Kinda removes the purpose of having server side actions though. Or we can always downgrade back to v13 I guess.
You are a life saver, I haven’t tracked all the changes that have been happening. Retested it and it works in 15.1.0 stable release which came out last week!
it seems weird that the documentation explicitly says that if you’re going to use route groups to have multiple root layouts you should remove the top-level layout.js file but some people here say that adding it is actually what make the redirect works so disappointing.
I’m experiencing the exact same issue, it’s frustrating seeing features marked as stable and after adding them to the codebase notice that everything falls apart. Why any maintainer hasn’t say anything yet about this issue ?
I don’t think it’s because of a missing root layout in /app. The docs still say that multiple root layouts are allowed with removing the top-level layout file. https://nextjs.org/docs/app/building-your-application/routing/route-groups#creating-multiple-root-layouts
wow, same issue. cant use with condition
Still broken in
v14.0.5-canary.43Version
14.0.4-canary.39still broken