Performance: Layout components
Layout components affect the initial page load speed and rendering performance by controlling how much HTML and CSS are sent and how the browser calculates layout.
/* Astro layout with flat structure and minimal CSS */ --- import Header from '../components/Header.astro'; import Footer from '../components/Footer.astro'; --- <html> <body> <Header /> <main> <slot /> </main> <Footer /> </body> </html>
/* Astro layout with many nested components and heavy CSS */ --- import Header from '../components/Header.astro'; import Footer from '../components/Footer.astro'; import Sidebar from '../components/Sidebar.astro'; --- <html> <body> <Header /> <Sidebar /> <main> <slot /> </main> <Footer /> </body> </html>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deep nested layout components | High (many nodes) | Multiple reflows | High paint cost | [X] Bad |
| Flat layout with minimal components | Low (few nodes) | Single reflow | Low paint cost | [OK] Good |
<body> is not how layouts work.---
// Layout.astro
---
Site Header
<slot />
--- import Layout from '../layouts/Layout.astro'; --- <Layout> <main>Welcome to my site</main> </Layout>
--- import Layout from '../layouts/Layout' --- <Layout> <h1>Hello</h1> </Layout>
Pass the test and this pattern moves unknown