Upgrade to Pro — share decks privately, control downloads, hide ads and more …

More Than You Ever Wanted to Know About Resourc...

More Than You Ever Wanted to Know About Resource Hints

Resource Hints are a great way for developers to make their web pages faster by allowing us to be a little bit smarter than the browser. Although not a new specification—they’ve been around in some form or another for years!—are we truly getting the most out of them? And do we understand them thoroughly enough to use them most effectively? Heck, do we even know what a ‘Resource Hint’ is?! Well, by the end of this talk, we’ll all be experts.

Let’s take a look at all of the different Resource Hints we have available to us, real-world examples of how best to use them, and learn about some of the more obscure intricacies and gotchas that we need to be aware of if we want to really get the best out of them (and to make sure that we really are being smarter than the browser).

Harry Roberts

April 20, 2019
Tweet

More Decks by Harry Roberts

Other Decks in Programming

Transcript

  1. “ “These primitives enable the developer […] to 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.”
  2. <head> ... <link rel="preconnect" href="https://www.youtube.com/" /> <link rel="prefetch" href="app.ae72eb.js" />

    <link rel="preload" href="/assets/webfont.woff2" as="font" type="font/woff2" crossorigin /> ... </head>
  3. “ “[…] common names […] can answer in closer to

    80–120ms. […] an uncommon name […] can average closer to 200–300ms.” — csswz.it/2GuZo21
  4. “ “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
  5. Be Judicious Only warm up frequent, significant, and likely origins

    Don’t warm up fourth, fifth, sixth party origins Opening many connections can have a CPU and battery cost Chrome can only conduct six simultaneous DNS resolutions
  6. “ “The user agent SHOULD NOT apply preprocessing on the

    response and MUST NOT automatically execute or apply it against the current page context.”
  7. <!-- search-results.html --> <link rel="prefetch" href="/assets/video-player.js" /> <!-- video.html -->

    <script src="/assets/video-player.js"></script> Downloaded by this… … executed by this.
  8. Caching prefetch will not execute or otherwise process the resource

    It will drop it into HTTP cache as per its caching headers Except…
  9. “ “…those with the no-store Cache- Control header. A resource

    will 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
  10. “ “The attribute is necessary to guarantee correct prioritization, request

    matching, application of the correct CSP3 policy, and setting of the appropriate Accept request header.”
  11. “ “Preload links for CORS enabled resources, such as fonts

    or images with a crossorigin attribute, must also include a crossorigin attribute, in order for the resource to be properly used.”
  12. Problematic Prerender Huge memory footprint with rendering whole new pages

    Bandwidth usage spikes for site and user Register multiple analytics hits, ad impressions How do we handle timers, HTTP auth, interstitials, autoplay media? How do we handle animations? Do we expect the carousel to start running? Or do we write more code to wait for page visibility? What if we get MitM and are made to prerender a malicious page? How would the user know? They wouldn’t!
  13. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport"

    content="width=device-width, minimum-scale=1.0" /> <title>Prerender</title> <link rel="prerender" href="https://csswizardry.com" /> </head> <body> <h1>Prerender</h1> </body> </html>
  14. Implementing Resource Hints 1.Identify a key page 2.Audit important assets

    and origins 3.Assess likely user flows 4.Design bespoke Resource Hint strategies
  15. Resource Hint Strategy 1.HTTP header preconnect to cloudinary 2.Regular preconnect

    for other third parties 3.preload to discover web font sooner 4.prefetch for gallery.js 5.prerender or prefetch for likely next navigation
  16. <!-- Early-discover web font --> <link rel="stylesheet" href="/assets/app.css" /> <link

    rel="preload" href="/assets/webfont.woff2" as="font" type="font/woff2" crossorigin />
  17. <!-- Prerender first three search results --> <link rel="prerender" href="/products/one.html"

    /> <link rel="prerender" href="/products/two.html" /> <link rel="prerender" href="/products/three.html" />
  18. <head> <link rel="preconnect" href="https://www.google-analytics.com"> <link rel="dns-prefetch" href="https://www.google-analytics.com"> <link rel="stylesheet" href="/assets/app.css"

    /> <link rel="preload" href="/assets/webfont.woff2" as="font" type="font/woff2" crossorigin /> <link rel="prefetch" href="/assets/gallery.js" /> <link rel="prerender" href="/products/one.html" /> <link rel="prerender" href="/products/two.html" /> <link rel="prerender" href="/products/three.html" /> </head>