Hey, I'm trying to dynamically fetch data for my NavBar, I'm facing certain design challenges that needs to be solved and unable to figure it out using the Next App Router
- Navigation is Dynamic (Data through Sanity and changes across Layouts) common across all site
- Currently I am fetching data for the navigation in Layout.tsx (The Root Layout), If I fetch in a page.tsx, then I've to repeat again and again across multiple pages. If the data is fetched in another layout page, it is hydrating the root layout first and once it changes, it leads to a hydration error
- What should be the optimal method of doing this?
Layout.tsx
import { JetBrains_Mono } from "next/font/google";
import { GeistSans } from "geist/font/sans";
import "./globals.css";
import Ribbon from "@/components/navigation/Ribbon";
import Navigation from "@/components/navigation/Navigation";
import { Toaster } from "@/components/ui/toaster";
import { client } from "../sanity/lib/client";
import FooterBar from "@/components/navigation/footer";
async function getNavigation() {
const query = `
*[_groq query]{
`;
const data = await client.fetch(query);
return data;
}
async function getFooter() {
const query = `
*[_groq query]{
`;
const data = await client.fetch(query);
return data;
}
export default async function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
const navigationData = await getNavigation();
const footerData = await getFooter();
return (
<html
lang="en"
className={`${GeistSans.variable} ${jetbrainsMono.variable}`}
>
<body>
<Ribbon />
<Navigation navigation={navigationData} />
<main>{children}</main>
<Toaster />
<FooterBar footer={footerData} />
</body>
</html>
);
}