As a NextJS newbie, I want to know the correct place for a dynamic header that's common in all pages. Seems to me the right location for it is in Layout that wraps all pages.
import Header from "../Header/Header";
import Footer from "../Footer/Footer";
export default function Layout({children}) {
return (
<>
<Header/>
{ children }
</>
)
}
export default function MyApp({ Component, pageProps })
{
return (
<Layout>
<Component {...pageProps} />
</Layout>
)
}
But in this approach, we won't be able to fetch data and generate the HTML for header and footer in the server side as only pages can make server side requests. So we must fetch header and footer data in client side which has two cons:
- Page body loads first and when ready, then header data is fetched which is bad UX.
- Header won't leverage the benefit of Next's SEO.
To solve this, we could place header in each page. This solves the SEO problem but there are cons in this approach:
- We would need to fetch and populate header data on each new SSR request increasing number of requests to API server.
- Any header data in memory, for example, value in a search input would be lost.
What is the correct approach to this?