#Best practise to opt out a specific page from RootLayout in Next.js 13.2?

7 messages · Page 1 of 1 (latest)

sturdy delta
#

I am using Next.js with the new app directoy. I have a layout.tsx file (see below) and a /app/card/page.tsx. In this page.tsx, I don't want to have the NavBar() from my RootLayout.

ChatGPT is suggesting me this, but I am unsure wether it's a "good way" / best practise to do so (this is my current layout.tsx file with the useRouter() & getLayout() add-ons from ChatGPT):

`import "./global.css";
import clsx from "clsx";
import Navbar from "components/navbar";
import { Libre_Baskerville } from "next/font/google";
import { Metadata } from "next";
import BackgroundMusicOnL from "components/music";
import React from "react";

const baskerville = Libre_Baskerville({
subsets: ["latin"],
weight: "400",
variable: "--font-baskerville",
display: "swap",
});

export const metadata: Metadata = {
// Your metadata object
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
const router = useRouter();

const getLayout = () => {
// If the current page is the page to opt-out of the layout, return the children without the layout
if (router.pathname === "/path/to/page-without-layout") {
return <>{children}</>;
}

// Otherwise, return the layout with the children
return (
  <html
    lang="en"
    className={clsx(
      "cursor-black dark:cursor-white text-black bg-white dark:text-white dark:bg-[#111010]",
      baskerville.variable
    )}
  >
    <body className="antialiased max-w-4xl mb-40 flex flex-col md:flex-row mx-4 mt-8 md:mt-20 lg:mt-32 lg:mx-auto">
      <Navbar />
      <BackgroundMusicOnL />
      <main className="flex-auto min-w-0 mt-6 md:mt-0 flex flex-col px-2 md:px-0">
        {children}
      </main>
    </body>
  </html>
);

};

return getLayout();
}
`

sturdy delta
#

Thanks, I read it but I think my case is different:

I have the following layout – see pic.

The /card route is inside my RootLayout (layout.tsx) since I want it to have at domain.tld/card directly. Is there still a way to opt out of the root layout, or maybe in the root layout to use if/else statement if the path is "/card"?

balmy flint
sturdy delta
#

How do you mean that? In the Next.js Docs I did only find that if I create, for example, a second layout.tsx file inside /app/card that it's just a sub-layout. But the original one is always on top.

forest leaf
#
app/
  (section1)/
    layout.tsx
    ...everyhting else
  (section2)/
    layout.tsx (or omit)
    card/
      page.tsx