👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Discussion about the root layout for a website and how to properly implement it using Next.js.

20 replies
Last updated: Apr 4, 2024
How it looks like ..

https://ibb.co/VLQD9Mr
Mar 18, 2024, 2:28 PM
You probably use the same layout for studio as for your site. You probably want to move your root layout to a nested layout for your site and then have the root layout be a empty one. https://nextjs.org/docs/pages/building-your-application/routing/pages-and-layouts
Mar 18, 2024, 3:12 PM
So your root layout could be layout.tsx with something like:
export default function RootLayout({_children_}) {

return <>{_children_}</>

}
Mar 18, 2024, 3:14 PM
it only works partially, i still see my website, but when i scroll down, i see the full ui of sanity.
Mar 18, 2024, 3:50 PM
This is my root layout for my homepage

import "@/styles/globals.css";
import { Metadata } from "next";
import { siteConfig } from "@/config/site";
import { fontSans } from "@/config/fonts";
import { Providers } from "./providers";
import  Navbar  from "@/components/navbar";
import { Link } from "@nextui-org/link";
import clsx from "clsx";

export const metadata: Metadata = {
	title: {
		default: siteConfig.name,
		template: `%s - ${siteConfig.name}`,
	},
	description: siteConfig.description,
	themeColor: [
		{ media: "(prefers-color-scheme: light)", color: "white" },
		{ media: "(prefers-color-scheme: dark)", color: "black" },
	],
	icons: {
		icon: "/favicon.ico",
		shortcut: "/favicon-16x16.png",
		apple: "/apple-touch-icon.png",
	},
};

export default function RootLayout({
	children,
}: {
	children: React.ReactNode;
}) {
	return (
		<html lang="en" suppressHydrationWarning>
			<head />
			<body
				className={clsx(
					"min-h-screen bg-background font-sans antialiased",
					fontSans.variable
				)}
			>
				<Providers themeProps={{ attribute: "class", defaultTheme: "dark" }}>
					<div className="relative flex flex-col h-screen">
						<Navbar />
						<main className="container mx-auto max-w-7xl pt-16 px-6 flex-grow">
							{children}
						</main>
						<footer className="w-full flex items-center justify-center py-3">
							<Link
								isExternal
								className="flex items-center gap-1 text-current"
								href="<https://nextui-docs-v2.vercel.app?utm_source=next-app-template>"
								title="<http://nextui.org|nextui.org> homepage"
							>
								<span className="text-default-600">Powered by</span>
								<p className="text-primary">NextUI</p>
							</Link>
						</footer>
					</div>
				</Providers>
			</body>
		</html>
	);
}
Mar 18, 2024, 3:51 PM
What are you passing in as the {children} on your homepage?
Mar 18, 2024, 6:52 PM
I need this to display everything on the website. If I remove it, you can no longer see the sanity interface.The problem is still that I see the website at the top and then the sanity dashboard at the bottom
Mar 21, 2024, 3:37 PM
I think something like this would work:
Mar 21, 2024, 4:21 PM
root layout => app/_layout_.tsxyour current website
layout => app/(content)/_layout_.tsxyour studio
layout => app/studio/layout.tsx
Mar 21, 2024, 4:21 PM
Try just having this as root layout
export default function RootLayout({_children_}) {

return <>{_children_}</>

}
Mar 21, 2024, 4:22 PM
for studio use the default
Mar 21, 2024, 4:23 PM
and for website use the one you pasted above
Mar 21, 2024, 4:23 PM
My layout.tsx currently looks like this in the /app folder.
Use nextUI for explanation


import "@/styles/globals.css";
import { siteConfig } from "@/config/site";
import { fontSans } from "@/config/fonts";
import { Providers } from "./providers";
import Navbar from "@/components/navbar";
import { Link } from "@nextui-org/link";
import clsx from "clsx";
import Head from "next/head"; // Importiere den Head-Komponenten
import Footer from "@/components/Footer";

export const metadata = {
    title: {
        default: siteConfig.name,
        template: `%s - ${siteConfig.name}`,
    },
    description: siteConfig.description,
    icons: {
        icon: "/favicon.ico",
        shortcut: "/favicon-16x16.png",
        apple: "/apple-touch-icon.png",
    },
};

export default function RootLayout({
    children,
}: {
    children: React.ReactNode;
}) {
    return (
        <html lang="en" suppressHydrationWarning>
            <Head>
                <meta name="description" content={siteConfig.description} />
                {/* Hier definierst du die Theme-Farben für verschiedene Schemata */}
                <meta name="theme-color" content="white" media="(prefers-color-scheme: light)" />
                <meta name="theme-color" content="black" media="(prefers-color-scheme: dark)" />
                {/* Hier kannst du weitere Meta-Tags für Icons usw. hinzufügen */}
            </Head>
            <body
                className={clsx(
                    "min-h-screen bg-background font-sans antialiased",
                    fontSans.variable
                )}
            >
                <Providers themeProps={{ attribute: "class", defaultTheme: "dark" }}>
                    <div className="relative flex flex-col h-screen">
                        <Navbar />
                        <main className="container mx-auto max-w-7xl pt-16flex-grow">
                            {children}
                        </main>
                        <footer className="w-full flex items-center justify-center py-3">
                           <Footer />
                        </footer>
                    </div>
                </Providers>
            </body>
        </html>
    );
}
Mar 21, 2024, 4:32 PM
i used this install for my next.js website
https://nextui.org/docs/frameworks/nextjs
Mar 22, 2024, 8:48 AM
yeah. so thats why it wraps around studio. Since nested layouts gets injected in above layouts. So the studio layouts gets rendered in the
{children}
you see above
Mar 22, 2024, 8:59 AM
And how can I isolate sanity? I would actually like to have it in my project and I've never had this problem before =(
Mar 22, 2024, 9:47 AM
Like i described above
Mar 22, 2024, 9:55 AM
if you are using app router
Mar 22, 2024, 9:56 AM
I'm going crazy here. And would actually hate to work without this template.
sanity admin is under /app/admin

in /admin is the layout.tsx and in
/app/admin/[[]...index] is the page.tsx
Mar 22, 2024, 10:47 AM
You can use Route Groups in order to have multiple root layouts. That way you can have a root layout for the Sanity Studio and another root layout for your main app. This is exactly what I did

app
├─ (main)
├─ (studio)
│  ├─ studio
│     └─ [[...slug]]
│  ├─ layout.tsx
│  └─ page.tsx
Apr 4, 2024, 8:43 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?