Performance: Why data fetching happens at build time
HIGH IMPACT
This affects the page load speed by preloading data before the user requests the page, reducing runtime delays.
--- // Fetches data once at build time (static by default) const res = await fetch('https://api.example.com/data'); const data = await res.json(); --- <html> <body> <div>{data.title}</div> </body> </html>
--- // Fetches data on every request in SSR mode const res = await fetch('https://api.example.com/data'); const data = await res.json(); --- <html> <body> <div>Loading...</div> </body> </html> // Note: Configure output: 'server' in astro.config.mjs for SSR
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Runtime data fetching (SSR) | Same DOM nodes | Multiple reflows possible due to loading states | Higher paint cost due to delayed content | [X] Bad |
| Build time data fetching (Static) | Same DOM nodes | Single reflow with full content | Lower paint cost with immediate content | [OK] Good |