Documente Academic
Documente Profesional
Documente Cultură
“
assist the user agent in the decision process of
which origins it should connect to, and which
resources it should fetch and preprocess to
improve page performance.”
i
Single lines of HTML
that can dramatically
speed up your site
<head>
...
...
</head>
csswz.it/rh
Overview
1.dns-prefetch
2.preconnect
3.prefetch
4.preload
5.subresource
6.prerender
dns-prefetch
csswz.it/dns-prefetch
i
Resolve the IP address
for a given domain
ahead of time
i
When you know the
domain but not the URL
<link rel="dns-prefetch" href="https://youtube.com" />
154 <iframe
155 src="https://www.youtube.com/embed/5g8a9luSZVI"
156 width="560" height="315">
157 </iframe>
5g8a9luSZVI
DNS Lookup
216.58.198.110
“
“[…] common names […] can answer in closer to
80–120ms. […] an uncommon name […] can
average closer to 200–300ms.”
— csswz.it/2GuZo21
“More interestingly, for any of these queries
“
that access the internet, dropped packets, and
overworked (under provisioned) name resolvers,
regularly increases the total resolution time to
between 1 and 10 seconds.”
— csswz.it/2GuZo21
@andydavies
!
dns-prefetch is
implemented as
prefetch in IE9…
preconnect
csswz.it/preconnect
i
Resolve the IP address
and open a TCP/TLS
connection for a given
domain ahead of time
i
When you know the
domain but not the URL
<link rel="preconnect" href="https://fonts.googleapis.com" />
5g8a9luSZVI
TCP Handshake TLS Negotiation
hpbn.co
preconnect with
dns-prefetch fallback
<link rel="preconnect dns-prefetch" href="//twitter.com" />
!
Breaks in Safari
@andydavies
<link rel="preconnect" href="https://twitter.com" />
<link rel="dns-prefetch" href="https://twitter.com" />
!
Be judicious with
preconnect
Be Judicious
… executed by this.
Caching
“
be revalidated before use if there is
a Vary response header, no-cache Cache-
Control header, or if the resource is more than
five minutes old.”
— csswz.it/nostate-prefetch
i
Fetched with lowest
possible priority
i
In-flight prefetches
persist across
navigations
!
…prefetch acts like
dns-prefetch in IE9
<link rel="prefetch"
href="https://code.jquery.com/jquery-3.4.0.js" />
preload
csswz.it/preload
i
A mandatory fetch for
a file needed for the
current navigation
i
A way to surface late-
discovered resources
<link rel="preload" href="/assets/webfont.woff2"
as="font" type="font/woff2" crossorigin />
<link rel="preload" href="/assets/webfont.woff2"
as="font" type="font/woff2" crossorigin />
Late-discovered
resource?
1000ms
}
HTML
CSS
Font
1250ms
}
HTML
CSS
Background
1350ms
} VDOM
HTML
JS
CSS
Font
preload helps the
browser find them sooner
<script src="assets/app.js"></script>
} VDOM
HTML
JS
CSS
Font
The as Attribute
<link rel="preload" href="/assets/webfont.woff2"
as="font" type="font/woff2" crossorigin />
"audio"
"document"
"embed"
"fetch"
"font"
as="image"
"object"
"script"
"style"
"track"
"video"
"worker"
“
“The attribute is necessary to guarantee correct
prioritization, request matching, application of
the correct CSP3 policy, and setting of the
appropriate Accept request header.”
!
Don’t try to be sneaky
<link rel="preload" href="style.css" as="style" />
<link rel="preload" href="app.js" as="script" />
<link rel="preload" href="image.jpg" as="image" />
<link rel="preload" href="style.css" as="image" />
<link rel="preload" href="app.js" as="style" />
<link rel="preload" href="image.jpg" as="script" />
i
Priority Hints will help
<link rel="preload" href="masthead.jpg"
as="image" importance="high" />
@andydavies
subresource
i
Precursor to preload…
!
…but completely
deprecated.
<link rel="subresource" href="/assets/webfont.woff2" />
SU B R E S O U R C E
“
“…not useful, proprietary, and buggy.”
B2
A B B3
B4
B5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0" />
<title>Prerender</title>
</head>
<body>
<h1>Prerender</h1>
</body>
</html>
i
Fetched with lowest
possible priority
Tips, Tricks, and Gotchas
i
Deploy as HTTP
Headers
@andydavies
!
Edge only supports
HTTP header for
preconnect
!
Edge doesn’t support
HTTP header for
preload
i
Generate Dynamically
instant.page
!
Have an escape hatch
if ($omgWeAreBeingDDoSed == false) {
}
!
You Need a Scheme
<link rel="preconnect" href="domain.com" />
==
JS image gallery
Resource Hint Strategy
<link rel="preconnect"
href="https://www.google-analytics.com">
<link rel="dns-prefetch"
href="https://www.google-analytics.com">
<!-- Early-discover web font -->
</head>
<head>
</head>
Thank You!
@csswizardry
csswizardry@gmail.com harry.is/for-hire
speakerdeck.com/csswizardry