Performance: Layout vs template difference
This concept affects how quickly the main page content loads and how efficiently the browser updates the UI when navigating between pages.
export default function Layout({ children }) { return ( <> <header>Site Header</header> <main>{children}</main> <footer>Site Footer</footer> </> ) } // Pages use Layout to wrap content, so header/footer render once.
export default function Page() { return ( <> <header>Site Header</header> <main>Page content</main> <footer>Site Footer</footer> </> ) }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Template with repeated header/footer | High (full page nodes recreated) | Multiple reflows per navigation | High paint cost due to full repaint | [X] Bad |
| Layout with shared header/footer | Low (shared nodes reused) | Single reflow for content update | Lower paint cost focused on content | [OK] Good |
layout component?children prop to wrap page content.{ children } and returns JSX wrapping children.export default function Layout({ children }) {
return (
<html>
<body>
<header>Site Header</header>
{children}
<footer>Site Footer</footer>
</body>
</html>
)
}export default function Page() {
return <main>Welcome!</main>
}export default function Layout() {
return (
<div>
<header>Header</header>
{children}
<footer>Footer</footer>
</div>
)
}{ children } as a parameter to render page content inside.{ children } in the function signature, so it cannot render page content.Pass the test and this pattern moves unknown