<link rel="prefetch" href="/style.css" as="style" /> <link rel="preload" href="/style.css" as="style" /> <link rel="preconnect" href="https://example.com" /> <link rel="dns-prefetch" href="https://example.com" /> <link rel="prerender" href="https://example.com/about.html" />
<link rel= "preload">
tells the browser to download and cache the resource (such as a script or style sheet) as soon as possible. This is useful when it takes a resource a few seconds after the page loads - and you want to speed up the process. <link rel="preload" href="/style.css" as="style" />
href
points to the resource you want to download.as
can be anything that can be downloaded in a browser:style
for style sheetsscript
for scriptsfont
for fontsfetch
for resources loaded with fetch()
or XMLHttpRequest
,as
attribute — this helps the browser to prioritize and schedule downloads. <!-- index.html --> <link rel="stylesheet" href="index.css" /> /* index.css */ @font-face { src: url('comic-sans.woff2') format('woff2'); }
comic-sans.woff2
will start loading only after loading and parsing index.css
. In order not to wait that long, you can load the font earlier with <link rel= "preload">
: <link rel="preload" href="comic-sans.woff2" as="font" />
<style> /* Inlined critical styles */ </style> <script> /* Custom JS that starts downloading non-critical styles */ loadCSS('/app/non-critical.css'); </script>
<link rel= "preload">
to start the download earlier: <style> /* Inlined critical styles */ </style> <link rel="preload" href="/app/non-critical.css" as="style" /> <script> /* Custom JS that starts downloading non-critical styles */ loadCSS('/app/non-critical.css'); </script>
<link rel= "preload">
unless you need the resource immediately after the page loads. If you need it later, for example, for the next page, then use <link rel= "prefetch">
.<link rel="preload">
. In other cases, it may ignore the preload, for example, if it is running on a slow connection.as
. For the Chrome browser, you can view the complete priority table .<link rel= "prefetch">
asks the browser to load and cache the resource (for example, a script or style sheet) in the background. The download takes place with a low priority, so does not interfere with more important resources. This is useful if you need the resource on the next page, and you want to cache it in advance. <link rel="prefetch" href="/style.css" as="style" />
href
points to the resource you want to download.as
can be anything that can be downloaded in a browser:style
for style sheetsscript
for scriptsfont
for fontsfetch
for resources loaded with fetch()
or XMLHttpRequest
,as
attribute — this helps the browser to prioritize and schedule downloads.<link rel= "prefetch">
when loading CSS and JS files to render pages with the product.<link rel= "prefetch">
when the resource is needed in a few seconds. In this case, use <link rel= "preload">
.<link rel= "prefetch">
usually executed with a minimum priority (see the full table of priorities ), that is, after loading the rest.<link rel= "preconnect">
asks the browser to join the domain in advance when you want to speed up connection establishment in the future. <link rel= "preconnect" href="https://api.my-app.com" />
href
points to the domain name for which you want to determine the IP address. You can specify with or without the prefix ( https://domain.com
) ( //domain.com
).my-app.com
and makes AJAX requests to api.my-app.com
: you do not know the specific requests in advance, because they are made dynamically from JS. It is quite appropriate to use the tag to pre-connect to the domain.my-app.com
and uses Google Fonts fonts. They are loaded in two steps: first, the CSS file is loaded from the fonts.googleapis.com
domain, then this file requests fonts from fonts.gstatic.com
. You cannot know which specific font files from fonts.gstatic.com
you will need until you upload the CSS file, so we can only make a preliminary connection in advance.216.58.215.78
) for the specified domain name ( google.com
).<link rel= "dns-prefetch">
asks the browser to perform a DNS rezolving for the domain in advance if you will soon connect to it and want to speed up the initial connection. <link rel= "dns-prefetch" href="https://api.my-app.com" />
href
indicates the domain name for which you want to set the IP address. You can specify with or without the prefix ( https://domain.com
) ( //domain.com
).my-app.com
and makes AJAX requests to api.my-app.com
: you do not know the specific requests in advance, because they are made dynamically from JS. It is quite appropriate to use the tag to pre-connect to the domain.my-app.com
, and uses Google Fonts fonts. They are loaded in two steps: first, the CSS file is loaded from the fonts.googleapis.com
domain, then this file requests fonts from fonts.gstatic.com
. You cannot know which specific font files from fonts.gstatic.com
you will need until you load the CSS file, so we can only establish a preliminary connection in advance.Note the similar characteristics on<link rel= "dns-prefetch"/>
and<link rel= "preconnect">
. Using them together for the same domain usually does not make sense:<link rel= "preconnect">
already includes<link rel= "dns-prefetch"/>
and much more. This can be justified in two cases:
- You want to support older browsers .
<link rel= "dns-prefetch" />
supported since IE10 and Safari 5 .<link rel= "preconnect">
was supported for some time in Chrome and Firefox, but was added to Safari only in 11.1 and is still not supported in IE / Edge . If you want to support these browsers, use<link rel= "dns-prefetch" />
as a<link rel= "preconnect">
for<link rel= "preconnect">
.- You want to speed up connectivity to more than 4-6 domains . The
<link rel= "preconnect">
not recommended for use with more than 4-6 domains, since establishing and maintaining a connection is an expensive operation.<link rel= "dns-prefetch" />
consumes less resources, so use it if necessary.
216.58.215.78
. In the address bar of the browser, the site name is usually entered (for example, google.com
), and the DNS servers (Domain Name System) map it to the server’s IP address ( 216.58.215.78
).<link rel= "prerender">
asks the browser to load the URL and display it on an invisible tab. When a user clicks on a link, the page should appear immediately. This is useful if you are sure that the user will visit a particular page and want to speed up its display.<link rel= "prerender">
poorly supported by major browsers. See below for details. <link rel="prerender" href="https://my-app.com/pricing" />
href
points to the URL for which you want to render rendering in the background.<link rel= "prerender">
on page A will help you quickly display page B.<link rel= "prerender">
more than one page.<link rel= "preload">
- when you need a resource in a few seconds<link rel= "prefetch">
- when you need a resource on the next page<link rel= "preconnect">
- when you know that you will need a resource soon, but you still do not know its full URL<link rel= "dns-prefetch">
- likewise, when you know that you will need a resource soon, but you still do not know its full URL (for older browsers)<link rel= "prerender">
- when you are sure that users will go to a certain page and want to speed up its displaySource: https://habr.com/ru/post/445264/
All Articles