-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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
[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) #58263
Comments
Still not working in |
The issue is still present in |
Do we have an update on this one? I had to do a bit of a workaround to render my login page instead of the nav as redirect does not work for me: // Next
import { redirect } from "next/navigation"
// Queries
import { getUser } from "@/actions/queries/user/getUser"
// Auth
import { auth } from "@/auth"
// Components
import Nav from "@/components/nav"
// Pages
import LoginPage from "../(auth)/login/page"
export default async function AppLayout({
children,
}: {
children: React.ReactNode
}) {
const session = auth()
if (!session) {
redirect("/login")
}
const user = session !== null ? await getUser() : null
return <>{user ? <Nav user={user}>{children}</Nav> : <LoginPage />}</>
} |
Version |
probably the same issue or very similar: #59217 |
any work around? |
This whole time I thought it was me, but yes this is a pretty major issue for us. |
Still broken in 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. |
It's still not working |
I'm with the same problem. Redirect is not updating the route |
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. |
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. |
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. Example of /app/layout.tsx
|
Still broken in |
wow, same issue. cant use with condition |
still not working |
because of that i reverted from a two root layout setup to one rootLayout (which basically has no layout at all). |
This works for me as well. For |
This worked! Thank you! This is such an annoying and undocumented issue. Wasted almost 3 hours before I saw this thread. |
Tested it on the latest canary version |
Moving from NuxtJs to NextJs and got the same problem, I am starting to regret it based on this simple requirement :-( |
How hasn't this got much attention? Feels like a pretty big issue. |
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 |
@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. |
I also ended up creating a fake root element called Update: I've stopped using next.js altogether somewhere in mid-April and I'm just using vanilla react now. There are way too many unanswered questions and way too many issues which don't get addressed. In every release all I see are fixes for Turbopack. |
I do agree with comment above, that if your architecture is splitted between route groups without any root layout, then you need to have 1 root layout on your project that's used by your next app. I think it's a bug that next cannot redirect to different layout |
Still not working. I have two root layouts for the info. Next version is |
This comment was marked as resolved.
This comment was marked as resolved.
Yeah, it was cause of what the doc said that made me remove it, now i added it back and everything works fine |
Confirming this is still an issue with latest version of Next. Redirect between multiple root layouts without main root layout inside the app dir does not work. |
@danesto yep, same error here! |
Is there an open issue about this, maybe we can help/contribute?
…On Fri, 26 Jul 2024 at 22:18, Vinicius Mesel ***@***.***> wrote:
@danesto <https://github.com/danesto> yep, same error here!
—
Reply to this email directly, view it on GitHub
<#58263 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANRJIO2IWZZXGP7H5Y2QY73ZOKVHTAVCNFSM6AAAAAA7FGXDWCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENJTGQ2DSMJRGE>
.
You are receiving this because you were mentioned.Message ID: <vercel/next
.***@***.***>
|
This just saved me from days of heart ache. Bless you good sir |
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? |
What a pain in the ass if a simple redirect function doesn't even work properly. All I want to do is this export async function Redirect() {
redirect("/dashboard");
return null;
} This is literally as simple as it gets, and I am met with this error: Console error: 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 minimal root layout approach does work, but if you refresh your page you can see that the other layouts are basically falls back to the root layout, meaning that the root layout should have the html and body tags, but the other layouts shouldn't. This is not documented, and if that not a bug, it should be stated by the Next.js team. |
Same behavior with |
inside the app folder you must have a layout.tsx file with this content so redirect function inside server actions will work
|
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. |
I'm having this issue as well. Currently using the |
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. |
Still not working on |
Is it documented somewhere to have common root layout for redirect to work? wasted >4hrs just to realize there is some issue with root layout. |
- redirection is fixed using client side redirection due to a known issue in Nextjs 14 vercel/next.js#58263 fixes Fix Redirection after login #62
13 months have passed since I've opened the issue, it's clear that vercel doesn't give a damn |
I noticed #73063 in a recent changelog. If you're affected by this issue you can help everyone out by reporting if it is fixed in the latest canary release. |
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! |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
FIXED IN 15.1.0!
Was fixed in this PR: #73063
Link to the code that reproduces this issue
https://github.com/meszaros-lajos-gyorgy/nextjs-14-redirect-bug
To Reproduce
npm run dev
Current vs. Expected behavior
Expected result:
You should land on
/en/search/hello
as that is the hardcoded redirection inside the server action inservices/SiteSearch.service.ts
.( This was the behavior in Next.js
13.4.12
, but it also threw an error (see #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
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.
The text was updated successfully, but these errors were encountered: