Course contentsShow
← Web Development
Lesson 1966 of 2,98236. Performance Optimization TechniquesFree lesson

Preload with 'as' Attribute

Specify resource types correctly using the 'as' attribute to ensure proper prioritization and handling.

Preload with 'as' Attribute

What you'll learn: How specifying the correct resource type with the as attribute ensures browsers prioritize and handle preloaded assets properly.

Why 'as' Matters

When you use <link rel="preload"> to load critical resources early, the browser needs to know what kind of resource it's fetching. Without this information, the browser can't apply the correct priority, set proper HTTP headers, or enforce the right Content Security Policy rules. That's where the as attribute comes in.

Think of it like package delivery: if you don't label a box as "fragile" or "perishable," it might be handled incorrectly. Similarly, if you don't tell the browser "this is a font" or "this is a script," it won't know how urgently to fetch it or how to process it once it arrives.

How 'as' Works

The as attribute tells the browser the resource type so it can:

  • Apply the appropriate fetch priority
  • Set correct Accept headers
  • Use proper CORS handling
  • Match the resource when it's actually requested later
<!-- Preload a critical font -->
<link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin>

<!-- Preload a hero image -->
<link rel="preload" href="/images/hero.jpg" as="image">

<!-- Preload critical CSS -->
<link rel="preload" href="/styles/critical.css" as="style">

<!-- Preload JavaScript module -->
<link rel="preload" href="/js/app.js" as="script">

Common 'as' Values

  • script – JavaScript files
  • style – CSS stylesheets
  • font – Web fonts (requires crossorigin)
  • image – Images
  • fetch – XHR/fetch requests
  • document – Navigation preloads

Key Takeaway: Always specify the as attribute with rel="preload" to ensure the browser prioritizes and handles your critical resources correctly—mismatched or missing types can cause performance issues instead of improvements.