Overview
The critical rendering path is the sequence of steps the browser takes to convert HTML, CSS, and JavaScript into a rendered page. Optimizing this path is crucial for fast initial page loads and excellent user experience.Understanding the rendering path
The browser follows these steps to render a page:Parse HTML
Parse CSS
Execute JavaScript
Construct render tree
Layout
Paint
Eliminate render-blocking resources
Render-blocking resources prevent the browser from displaying content until they’re downloaded and processed.Identifying render-blocking resources
- Lighthouse
- Chrome DevTools
- WebPageTest
- Stylesheets in
<head> - Synchronous
<script>tags - Large CSS files
- Unused CSS rules
Async and defer scripts
When to use async
When to use async
async for scripts that:- Don’t depend on other scripts
- Don’t modify the DOM before it’s ready
- Can execute in any order
When to use defer
When to use defer
defer for scripts that:- Need the full DOM to be parsed
- Depend on other scripts (execution order matters)
- Can wait until HTML parsing completes
When to use neither
When to use neither
- Script must execute before page renders
- Other scripts depend on it immediately
Minimize critical CSS
Critical CSS is the minimum CSS needed to render above-the-fold content.Extracting critical CSS
Identify critical styles
Inline critical CSS
<head> to eliminate the network request.Defer non-critical CSS
Implementation approaches
- Manual extraction
- Automated with Critical
- Next.js approach
Async CSS loading pattern
Defer non-critical JavaScript
Defer JavaScript that’s not essential for initial rendering to improve page load performance.Lazy loading JavaScript
Third-party script optimization
Self-host third-party scripts
Self-host third-party scripts
- Control over caching headers
- No third-party DNS lookup
- Better privacy
- Reduced connection overhead
Use facade pattern
Use facade pattern
Delay non-essential scripts
Delay non-essential scripts
Preload, prefetch, and preconnect strategies
Resource hints tell the browser to optimize loading for specific resources.Resource hint types
Preload
Use for: Critical fonts, CSS, scripts, images
Prefetch
Use for: Next page resources, likely user actions
Preconnect
Use for: API domains, CDNs, fonts
DNS-prefetch
Use for: Multiple third-party domains
Preload examples
- Fonts
- Images
- Scripts and styles
crossorigin attribute for font preloads, even for same-origin fonts.Prefetch examples
Preconnect examples
Resource hint decision tree
Is it critical for initial render?
Will the user likely need it soon?
Does it require a third-party connection?
Complete optimization example
Measuring impact
- Before optimization
- After optimization
Critical rendering path checklist
HTML optimization
HTML optimization
- Minimize HTML size
- Enable compression (gzip/brotli)
- Use early hints (103 status code)
- Optimize server response time (TTFB < 200ms)
CSS optimization
CSS optimization
- Extract and inline critical CSS
- Async load non-critical CSS
- Remove unused CSS
- Minimize CSS file size
- Use media queries for conditional loading
JavaScript optimization
JavaScript optimization
- Defer non-critical scripts
- Use async for independent scripts
- Minimize JavaScript execution time
- Code split and lazy load
- Optimize third-party scripts
Resource loading
Resource loading
- Preload critical resources
- Preconnect to required origins
- Prefetch likely next resources
- Optimize font loading
- Lazy load below-fold images
Measurement
Measurement
- Lighthouse score > 90
- FCP < 1.8s
- LCP < 2.5s
- TTI < 3.8s
- Monitor real user metrics (RUM)