JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsperformance
PrevNext

Learn the concept

Rendering Strategies & Critical Rendering Path

performance
senior
rendering

What is the Critical Rendering Path and how do you optimize it?

critical-rendering-path
optimization
css
fonts
preload
Quick Answer

The Critical Rendering Path is the sequence of steps browsers take to render a page: HTML parsing, CSS parsing, render tree construction, layout, and paint. Optimize by minimizing critical resources, reducing file sizes, and removing render-blocking resources.

Detailed Explanation

Critical Rendering Path Steps:

  1. Parse HTML → DOM
  2. Parse CSS → CSSOM
  3. Combine → Render Tree
  4. Layout → Geometry calculations
  5. Paint → Draw pixels

Blocking Resources:

  • CSS blocks rendering
  • JavaScript blocks parsing
  • Fonts can cause FOIT/FOUT

Optimization Strategies:

  1. Minimize critical resources
  2. Defer non-critical CSS/JS
  3. Inline critical CSS
  4. Async/defer scripts
  5. Preload important resources
  6. Optimize font loading

Code Examples

Optimizing resource loadingHTML
<!DOCTYPE html>
<html>
<head>
  <!-- Preconnect to critical origins -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://api.example.com">
  
  <!-- Preload critical resources -->
  <link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin>
  <link rel="preload" href="/hero.jpg" as="image">
  
  <!-- Critical CSS inlined -->
  <style>
    /* Above-the-fold styles only */
    body { margin: 0; font-family: system-ui; }
    .hero { height: 100vh; background: #f0f0f0; }
    .nav { position: fixed; top: 0; width: 100%; }
  </style>
  
  <!-- Non-critical CSS loaded asynchronously -->
  <link rel="preload" href="/styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
  <noscript><link rel="stylesheet" href="/styles.css"></noscript>
  
  <!-- Scripts with appropriate loading strategy -->
  <script src="/critical.js"></script> <!-- Blocking if needed -->
  <script src="/analytics.js" async></script> <!-- Independent -->
  <script src="/app.js" defer></script> <!-- After DOM -->
</head>
<body>
  <!-- Content... -->
</body>
</html>

Real-World Applications

Use Cases

Optimizing a landing page for an online marketing campaign to achieve a high Google PageSpeed Insights score

Prioritizing the Critical Rendering Path by inlining critical CSS, deferring non-essential JavaScript, and asynchronously loading fonts to ensure the 'above-the-fold' content renders as quickly as possible.

Improving perceived performance for users on slow networks or mobile devices accessing a content-heavy news portal

Strategically deferring images and JavaScript that are not immediately needed for the initial viewport, reducing the number of critical resources and enabling faster display of main article content.

Ensuring a consistent and fast first paint for a brand's corporate website across various browsers and devices

Using `preconnect` and `preload` directives for crucial third-party resources and web fonts, and implementing `font-display: swap` to prevent Flash Of Invisible Text (FOIT) while optimizing font delivery.

Mini Projects

Critical CSS Extraction Tool

intermediate

Build a small Node.js script that uses Puppeteer or a library like `critical` to extract and inline critical CSS for a given HTML page, demonstrating CRP optimization.

Deferred JavaScript Loader

beginner

Create a simple web page with both critical and non-critical JavaScript, then implement a strategy to load non-critical scripts asynchronously or with `defer` attributes, observing the impact on initial page load.

Industry Examples

Amazon

Amazon heavily invests in optimizing its Critical Rendering Path to ensure products are visible and interactive within milliseconds, directly impacting conversion rates. They use techniques like inlined critical CSS, optimized image delivery, and deferred script loading across their vast e-commerce platform.

The Guardian

As a major news publisher, The Guardian prioritizes fast content delivery. They employ CRP optimization techniques, including responsive images, optimized font loading, and careful management of third-party scripts, to ensure articles load quickly for a global audience on diverse devices.

Resources

Web.dev - Critical Rendering Path

article

Critical CSS Tool

docs

Related Questions

What are Core Web Vitals and why do they matter?

junior
metrics

What is the difference between SSR, SSG, and CSR?

mid
rendering
Previous
How do service workers improve performance and what caching strategies exist?
Next
How do you set up performance monitoring for production applications?
PrevNext