New: We've launched a brand new Coding Challenges section! Check out these interactive, real-world exercises to level up your skills.Explore Challenges
FrontendPrep
performanceMedium

Web Performance: Preload, Prefetch, and Preconnect

Loading...

Understand the differences between preload, prefetch, preconnect, and dns-prefetch, and how to use them to optimize page load speeds.

Arvind M
Arvind MLinkedIn

Web Performance: Preload, Prefetch, and Preconnect

When a browser loads a web page, it parses the HTML from top to bottom. If it finds scripts, stylesheets, fonts, or images, it starts downloading them. However, by default, the browser doesn't know which assets are critical for the page to load fast versus which ones can wait.

Resource Hints (using <link rel="..."> attributes) allow you to prioritize downloads, helping browsers establish connections and fetch assets early.


1. What is preload?

The preload hint tells the browser to download a critical asset needed for the current page as soon as possible. It bypasses the browser's normal parsing order.

Best Used For:

Assets that are discovered late by the browser but are critical for rendering, such as:

  • Web fonts declared inside CSS files (which the browser only discovers after downloading and parsing the CSS).
  • Hero images above-the-fold (crucial for Largest Contentful Paint - LCP).
  • Core bundle files.

Code Example:

<head>
  <!-- Preloads a critical font asset -->
  <link 
    rel="preload" 
    href="/fonts/inter.woff2" 
    as="font" 
    type="font/woff2" 
    crossorigin="anonymous" 
  />
  
  <!-- Preloads the primary hero image -->
  <link rel="preload" href="/images/hero.webp" as="image" />
</head>

2. What is prefetch?

The prefetch hint tells the browser that an asset will be needed for a future page navigation. The browser downloads this asset in the background with low priority when it is idle.

Best Used For:

Assets needed for subsequent pages the user is likely to visit next (e.g., the dashboard script loaded when a user sits on the login page).

Code Example:

<!-- Downloads dashboard bundle in the background for future use -->
<link rel="prefetch" href="/js/dashboard.js" as="script" />

3. What is preconnect?

The preconnect hint tells the browser to establish a connection to a third-party domain immediately, before the actual request is sent. This starts the DNS lookup, TCP handshake, and secure TLS negotiation early, saving valuable milliseconds.

Best Used For:

Crucial external domains, such as Google Fonts, external CDNs, or API servers.

Code Example:

<!-- Connects to external font files domain early -->
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />

4. What is dns-prefetch?

The dns-prefetch hint performs only the DNS lookup step for a domain. It consumes fewer resources than preconnect and acts as a safe fallback.

Best Used For:

Non-critical third-party domains, tracking scripts, or as a fallback for browsers that do not support preconnect.

Code Example:

<!-- Pre-resolves domain names for third-party scripts -->
<link rel="dns-prefetch" href="https://example-tracker.com" />

Summary Comparison

Resource HintPriorityScopeTrigger Event
preloadHighCurrent PageFetches asset immediately
prefetchLowFuture PagesFetches asset when browser is idle
preconnectHighThird-party DomainsResolves DNS + TCP + TLS handshake
dns-prefetchLowThird-party DomainsResolves DNS lookup only (fallback)

Key Takeaways

  • Use preload to fetch late-discovered, critical resources (like fonts or hero images) needed for the current page immediately.
  • Use prefetch to fetch assets in the background that are likely to be used in future page navigations.
  • Use preconnect to establish early handshakes with critical external servers (like Google Fonts or CDNs).
  • Always specify the as attribute (e.g., as="font") when preloading so the browser applies the correct security and caching rules.

Finished practicing this challenge?

Mark it as completed to track your progress, or bookmark it to review later.

Loading...

Share this Resource

Help other developers level up by sharing this study guide.

⚡ Weekly newsletter

Crack Your Next Frontend Interview.

Join senior engineers who receive practical, deep-dive frontend challenges, detailed concepts, and blueprints directly in their inbox.

  • Senior level React, JS, and CSS interview blueprints
  • System Design & performance optimization deep-dives
  • 100% free, zero spam, unsubscribe with one click

Join the Study Track

We value your privacy. Unsubscribe at any time.

More Technical Questions

Expand your mastery. Deep dive into other frontend interview challenges in this category.