Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Web Performance & Core Web Vitals
  1. 1.Introduction to Web Performance & Business Impact
  2. 2.Core Web Vitals & Practical Measurement Tools
  3. 3.Optimizing the Critical Rendering Path & Basic Asset Delivery
  4. 4.JavaScript Performance & Bundle Optimization
  5. 5.Image, Video, and Media Optimization Strategies
  6. 6.Network Performance: HTTP/2, Caching, and CDNs
  7. 7.Resource Hints & Advanced Loading Strategies
  8. 8.Frontend Architectures & Server-Side Rendering Performance
  9. 9.Performance Budgets, RUM, and Continuous Monitoring
  10. 10.Advanced Browser APIs & Offloading Work with Workers
Web Performance & Core Web Vitals
03

Web Fundamentals · Lesson 3 of 10 · 4 min

Optimizing the Critical Rendering Path & Basic Asset Delivery

This is how you explain why a page is slow and what you'd do first to fix it.

When a browser receives HTML, it follows a sequence of steps to turn that code into pixels. This is the Critical Rendering Path (CRP). Your job is to make this path as short as possible. Understanding these steps lets you identify bottlenecks and justify performance optimizations in an interview.

The Browser's Path to Pixels

The browser must complete these steps before the user sees anything. A delay in any step delays the first render.

  1. DOM Construction: The browser parses the HTML into a tree of objects, the Document Object Model (DOM).
  2. CSSOM Construction: The browser parses CSS into the CSS Object Model (CSSOM). This includes styles from all sources.
  3. Render Tree Construction: The browser combines the DOM and CSSOM into a Render Tree, which includes only the nodes required to render the page (e.g., display: none nodes are omitted).
  4. Layout: The browser calculates the size and position of every object in the Render Tree. This is also known as "reflow."
  5. Paint: The browser draws the pixels for each node onto layers.
  6. Composite: The browser composites the layers onto the screen.

This process is a pipeline. Later stages depend on earlier ones.

The two primary bottlenecks you control are CSS and JavaScript, which can block this pipeline.

Unblocking Render-Blocking CSS

By default, all CSS is render-blocking. The browser will not paint any content until it has downloaded and parsed all <link rel="stylesheet"> tags in the <head>.

This is intentional. It prevents a "flash of unstyled content" (FOUC), where the user sees unstyled HTML that then shifts and re-styles itself. Waiting for CSS is a trade-off for a better visual experience, but it can make your page feel slow.

The trap: Simply stating "CSS is render-blocking" is table stakes. You must explain why it's a necessary default and how to work around it for the initial render.

The solution is to split your CSS into two parts:

  1. Critical CSS: The absolute minimum CSS required to style the above-the-fold content. Inline this directly in the <head> inside a <style> tag to avoid a network request.
  2. Non-critical CSS: The rest of your styles. Load this asynchronously so it doesn’t block the initial paint.

This is the standard pattern for loading non-critical CSS without blocking the render.

<!-- In your <head> -->

<!-- 1. Inline critical CSS for the initial viewport -->
<style>
  body { font-family: sans-serif; }
  .hero-title { font-size: 2rem; color: #333; }
  /* ... other essential above-the-fold styles ... */
</style>

<!-- 2. Load the rest of the stylesheet asynchronously -->
<link rel="preload" href="non-critical.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="non-critical.css"></noscript>

The preload link tells the browser to fetch the stylesheet with a high priority but without blocking rendering. The onload attribute then applies the stylesheet once it has finished downloading.

Choosing Between async and defer for Scripts

By default, a <script> tag is parser-blocking. When the browser's HTML parser encounters a script tag, it stops parsing, fetches the script, executes it, and only then resumes parsing the HTML.

This behavior exists for legacy reasons, mainly because a script could use document.write() to alter the DOM. You almost never want this default behavior. Your only decision should be between async and defer.

The interview question: "What's the difference between async and defer?"

  • defer: Downloads the script in parallel with HTML parsing. Guarantees execution after the parser has finished, but before the DOMContentLoaded event. Scripts with defer will execute in the order they appear in the document. This is the correct choice for scripts that need the full DOM or depend on other scripts. Use it for your main application bundle.

  • async: Downloads the script in parallel with HTML parsing. Executes the script as soon as it finishes downloading, which can happen at any time and will pause the HTML parser. Execution order is not guaranteed. This is for independent, third-party scripts that don't depend on the DOM or other scripts, like an analytics tracker.

Here is how you should structure your scripts in the <head>.

<!DOCTYPE html>
<html>
<head>
  <title>Smart Script Loading</title>

  <!-- Use `async` for independent, third-party scripts. Order is not guaranteed. -->
  <script async src="https://www.google-analytics.com/analytics.js"></script>

  <!-- Use `defer` for your application scripts. They execute in order after HTML parsing. -->
  <script defer src="/assets/vendor-bundle.js"></script>
  <script defer src="/assets/app.js"></script>
</head>
<body>
  <!-- Page content -->
</body>
</html>

Placing scripts at the end of <body> was a pre-defer workaround. Today, defer in the <head> is superior because the browser can discover and start downloading the script earlier.

Giving the Browser a Head Start with preload

Sometimes the browser discovers a critical resource late. A common case is a hero image that sets your Largest Contentful Paint (LCP), a background image defined in CSS, or a font file. The browser won't know to fetch them until it has downloaded and parsed the CSS.

rel="preload" is a declarative hint that tells the browser to fetch a resource with high priority now because you know you will need it for the current page. It separates fetching from execution.

The trap: Confusing preload with prefetch. preload is for the current navigation and is a high-priority, mandatory fetch. prefetch is for a future navigation and is a low-priority hint. Answering a question about LCP optimization without mentioning preload for the LCP image is a missed opportunity.

<head>
  <!-- Preload the main font file, which is otherwise hidden inside a CSS file.
       The `crossorigin` attribute is required for fonts. -->
  <link rel="preload" href="/fonts/inter-v12.woff2" as="font" type="font/woff2" crossorigin>

  <!-- Preload the LCP hero image so it starts downloading immediately. -->
  <link rel="preload" href="/images/hero-desktop.webp" as="image" media="(min-width: 601px)">
  <link rel="preload" href="/images/hero-mobile.webp" as="image" media="(max-width: 600px)">
</head>

Using media with preload lets you load the correct resource for the user's viewport without downloading unnecessary assets.

Try it

Open DevTools on a news website or e-commerce product page. Use the Performance tab to record a page load. In the "Network" track of the flame chart, identify the main CSS and JavaScript files. See how long they block rendering on the main thread and where they sit in the request waterfall.

Next: JavaScript Performance & Bundle Optimization

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Core Web Vitals & Practical Measurement Tools

Next

JavaScript Performance & Bundle Optimization