Performance: Prefetching and preloading
Prefetching and preloading improve page load speed by loading resources early, reducing wait time for users.
<link rel="preload" href="styles.css" as="style" onload="this.rel='stylesheet'"> <link rel="preload" href="app.js" as="script">
<link rel="stylesheet" href="styles.css"> <script src="app.js"></script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No preload or prefetch | Minimal | Multiple reflows due to late resource arrival | High paint cost due to delayed styles/scripts | [X] Bad |
| Preload critical resources | Minimal | Single reflow after early resource fetch | Lower paint cost with faster style application | [OK] Good |
| Prefetch non-critical resources | Minimal | No reflows triggered | No paint cost impact on current page | [OK] Good |
| Preload non-critical resources | Minimal | Blocks rendering causing reflows | Increased paint cost delaying LCP | [X] Bad |
prefetching in Astro?<head>?rel="prefetch" to hint browser to load resource in background.rel="preload", but question asks for prefetch. rel="fetch" is invalid.<head> <link rel="preload" as="style" href="/styles.css" /> </head> <body> <h1>Hello</h1> </body>
as="style" tells browser to load CSS immediately for fast styling.<head> <link rel="prefetch" as="script" href="/app.js" /> </head>
as attribute; it's only for preload.Pass the test and this pattern moves unknown