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
Acceptheaders - 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 filesstyle– CSS stylesheetsfont– Web fonts (requirescrossorigin)image– Imagesfetch– XHR/fetch requestsdocument– 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.