Definition and Usage
The rel="preload" attribute of the <link> tag indicates that the browser should preemptively fetch the resource specified by the href attribute and store it in the cache to preload it.
It is used within the <head> tag and is mainly used to preload resources needed by the current page, such as web fonts, images, and CSS files.
- resource
- It refers to various files or data that can be accessed on the web. This includes HTML documents, images, stylesheets (CSS), and JavaScript files, as well as various types of files used to build web page content or implement functionality, such as font files, videos, and audio files.
During the process of rendering a web page, resources such as fonts and images displayed on the screen may not be fully loaded, causing the page to wait until those resources finish loading. In this case, using preloaded resources can reduce page loading time and improve loading performance.
Basic Example
The following code example uses the rel="preload" attribute of the <link> tag to preload a web font displayed on the screen, allowing the preloaded web font to be applied directly to the text when the web page is rendered.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rel=preload Example Source</title>
<!-- Preload the web font file so it can be used sooner when needed -->
<link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<!-- Link the stylesheet that includes the web font styles -->
<link rel="stylesheet" href="webfont.css">
</head>
<body>
<!-- Allow the preloaded web font to be applied quickly to the text -->
<h1>rel=preload Example Source</h1>
<!-- Additional content -->
</body>
</html>
The rel="preload" attribute of the <link> tag is useful for resources that take a long time to load or need to be prepared so they can be used immediately when needed on the current page:
- Web fonts or large images that are displayed to users immediately when a web page loads
- Large video files that need to be played immediately when a web page loads (Note: Not supported by most browsers)
- Resources such as JSON, Web Workers, and imported scripts that need to be used quickly by JavaScript for the functionality of a web page
Syntax
The syntax for the rel="preload" attribute of the <link> tag is as follows.
It is used within the <head> tag and uses the href and as attributes, with optional attributes such as type, crossorigin, and media added as needed.
<link rel="preload" href="resource path" as="resource type" [additional attributes]>
Attributes
href="resource path" (Required)
Specifies the URL of the resource to preload.
as="resource type" (Required)
Specifies the type of resource to load.
You can specify various resource types, and the available values are as follows:
font |
Font files |
|---|---|
image |
Image files |
video |
Video files
Defined in the specification, but not supported by most browsers |
style |
CSS files |
script |
JavaScript files |
fetch |
Resources fetched by Fetch or XHR requests |
track |
WebVTT files
Defined in the specification, but not supported by some browsers |
as="resource type" Examples
<!-- as="font" -->
<link
rel="preload"
href="font.woff2"
as="font">
<!-- as="image" -->
<link
rel="preload"
href="sample-image.png"
as="image">
<!-- as="style" -->
<link
rel="preload"
href="sample-style.css"
as="style">
<!-- as="script" -->
<link
rel="preload"
href="sample-script.js"
as="script">
<!-- as="fetch" -->
<link
rel="preload"
href="sample-fetch.json"
as="fetch">
type="MIME type" (Optional)
Specifies the MIME type of the resource. For example, you can set type="font/woff2" for a font file.
By explicitly specifying the MIME type using the type attribute, the browser fetches the resource only if it supports the specified MIME type. This helps the browser correctly interpret and process the resource.
Explicitly specifying the MIME type can help the browser process the resource more efficiently and provide better compatibility.
However, specifying the MIME type explicitly is not required.
In most cases, browsers can automatically detect and process the resource's MIME type. Modern browsers handle this very well, so the type attribute can be omitted explicitly in many cases.
crossorigin (Optional)
Specifies the settings for CORS requests. It is used when loading resources from an external origin.
The crossorigin attribute specifies the CORS (Cross-Origin Resource Sharing) request mode used when fetching resources linked to an element from an external origin.
- external origin
- An external origin refers to an address where at least one of the current web page's protocol (e.g.,
http://orhttps://) or domain (e.g.,example.com) is different. If a port (e.g.,example.com:8080) is specified after the domain, the port is also included as a criterion for distinguishing the origin. If the port is different, the address is considered an external origin.
- CORS (Cross-Origin Resource Sharing)
- CORS is a web standard mechanism that allows web browsers to request and access external resources that might otherwise be blocked for security reasons.
When the resource is a font file, it must be fetched using anonymous CORS mode for various reasons, even if it is served from the same origin.
Unlike general resources, web fonts are loaded into a web page using the CSS @font-face rule. During this insertion process, the browser may trigger CORS requests in the background. Therefore, even when preloading a same-origin web font resource using the <link> tag with rel="preload", it is recommended to set the crossorigin attribute to anonymous to ensure the CORS mode matches the actual font request.
crossorigin="anonymous" for a same-origin font file
<link
rel="preload"
href="font.woff2"
as="font"
type="font/woff2"
crossorigin="anonymous">
media (Optional)
The <link> tag can use the media attribute to specify media conditions and fetch resources based on specific media types.
You can also use the media attribute to specify media conditions for preloaded resources. The following is an example using the media attribute:
<link
rel="preload"
as="image"
href="mobile.png"
media="(max-width: 600px)">
<link
rel="preload"
as="image"
href="desktop.png"
media="(min-width: 601px)">
In the code example above, the <link> tag includes the media attribute to preload a mobile image for users with a narrow viewport and a desktop image for users with a wider viewport.
A viewport is a web browser term that refers to the visible area of a web page displayed in the current window (or the screen when viewing the document in full-screen mode).
rel="preload" and rel="prefetch"
The rel="prefetch" attribute of the <link> tag indicates that the browser should preemptively fetch the resource specified by the href attribute and store it in the cache.
It is used to prefetch and cache pages that the user is likely to navigate to next from the current page, or resources loaded after the page has finished loading (such as scripts, images, and CSS files).
We will compare rel="preload" with rel="prefetch".
| Comparison | rel="preload" |
rel="prefetch" |
|---|---|---|
| Timing | Current | Future |
| Purpose | Improve the loading speed of resources used on the current page | Improve the loading speed of resources that may be needed later |
| Implementation | Preloads and prepares resources required for the current page | Fetches in advance and caches resources for pages likely to be visited next or resources needed after the initial page load |
Things to Keep in Mind
The <link> tag's rel="preload" should only be used for resources that are required on the current page.
Otherwise, it can place an unnecessary load on the browser. In addition, most browsers display a warning message in the browser console if a resource has been fetched but is not used within a certain amount of time. Be careful not to use it for resources that are not needed.
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<link rel="preload">
|
50 | 79 | 85 | 11.1 |
Specifications
| Specification | |
|---|---|
<link rel="preload">
|
HTML Standard #link-type-preload |
References
See Also
- HTML rel Attribute – Meaning, List, and Examples
- HTML <link> Tag – The External Resource Link Element
- HTML <link rel="prefetch"> – Proper Understanding and Usage
- HTML <link rel="dns-prefetch"> – Proper Understanding and Usage
- HTML <link rel="preconnect"> – Proper Understanding and Usage
- HTML crossorigin Attribute – Specifying CORS Request Modes