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

Building Fast Websites Against All Odds - Leeds JS 2018

Building Fast Websites Against All Odds - Leeds JS 2018

Event: https://www.meetup.com/LeedsJS/events/255547488/

Fast website performance is essential for a great user experience, particularly in rural areas or emerging markets where poor connectivity and low-power devices can struggle with even the most optimised sites, yet it can still seem impossible to convince clients and stakeholders of the value that performance holds.

Now, I can’t promise that I can magically transform your clients’ attitudes toward performance, but what I can do show is show you how to build a fast website without even involving them. In my work delivering high-performance, high-availability websites for some of the UK’s biggest retailers, I’ve come up with a wealth of ways in which we as front-end developers can improve performance without the need for any costly rewrites, new infrastructure, or any kind of work that requires permission (and budget!).

We may not be able to make the _fastest_ website without permission, but we can certainly make a fast one!

Ryan Townsend

October 31, 2018
Tweet

More Decks by Ryan Townsend

Other Decks in Programming

Transcript

  1. Leeds JS 2018 Building fast websites against all odds Photo

    by Emil Vilsek on Unsplash Slides – twnsnd.com/leedsjs2018
  2. “The result of rebuilding our pages for performance led to

    [...] a 15 percent increase in SEO traffic” – Pinterest Source – https://bit.ly/2ICPv4Z
  3. “[…] starting in July 2018, page speed will be a

    ranking factor for mobile searches.” – Google #SpeedUpdate Source – https://bit.ly/2Dt5Plz
  4. “For every 100ms decrease in homepage load speed, Mobify's customer

    base saw a 1.11% lift in session based conversion, amounting to an average annual revenue increase of $376,789” – Mobify Source – https://bit.ly/2s0FDHS
  5. “Similarly, for every 100ms decrease in checkout page load speed,

    Mobify's customers saw a 1.55% lift in session based conversion, amounting to an average annual revenue increase of $526,147” – Mobify Source – https://bit.ly/2s0FDHS
  6. Initial Results? • Start render: 6.4s • SpeedIndex: 12717 •

    Interactive: 4.4s • Bandwidth: 2,594 KB
  7. You can’t control your infrastructure without permission, but you can

    check features are enabled: • GZIP / Brotli • OCSP for SSL • HTTP/2 • Connection: keep-alive
  8. Correct Order 1. <meta> character encoding / viewport 2. <title>

    3. Resource hints 4. Inline <script> 5. External stylesheet 6. Inline <style> 7. External <script> 8. Anything else, e.g. other meta tags “Get Your <head> Straight” – @csswizardry
  9. Resource Hints • DNS Prefetch • Preconnect (CDN, 3rd parties)

    • Preload (CSS @imports, @font-face etc) • Prefetch (low priority preload)
  10. Start Render SpeedIndex Interactive Bandwidth (-1.8s) (+184) (-0.1s) (no change)

    Value: $6,782,202 / year Value based on Mobify case study
  11. CSS

  12. Start Render SpeedIndex Interactive Bandwidth (-0.1s) (-348) (-0.2s) (no change)

    Value: $376,789 / year Value based on Mobify case study (ideal conditions)
  13. Start Render SpeedIndex Interactive Bandwidth (-90.1s) (-∞) (-0.2s) (no change)

    Value: $339,486,889 / year Value based on Mobify case study (3rd party offline)
  14. Start Render SpeedIndex Interactive Bandwidth (-90.1s) (-∞) (-0.2s) (no change)

    Value: $38,754 / hour Value based on Mobify case study (3rd party offline)
  15. /* latin */ @font-face { font-family: 'Montserrat'; font-style: normal; font-weight:

    400; src: local('Montserrat Regular'), local(‘Montserrat-Regular'), url(/fonts/montserrat.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; font-display: swap; }
  16. Start Render SpeedIndex Interactive Bandwidth (-1.8s) (-258) (-0.1s) (no change)

    Value: $6,782,202 / year Value based on Mobify case study
  17. <picture> <!--[if IE 9]><video style="display: none;><![endif]--> <source srcset='[email protected] 1600w, ...'

    type='image/vnd.ms-photo' /> <source srcset='[email protected] 1600w, ...' type='image/jp2' /> <source srcset='[email protected] 1600w, ...' type='image/webp' /> <!--[if IE 9]></video><![endif]--> <img srcset=' [email protected] 1600w, [email protected] 1400w, [email protected] 1200w, ...' src='[email protected]' alt='Hero Image' /> </picture> our different formats our different sizes fallback to JPEG with an old-school <img> ‘src’
  18. • Progressive enhancement • Network control • Offline support •

    Progressive Web Apps (PWAs) • stale-while-revalidate / stale-while-error • Protect against 3rd party slow-downs
  19. function timeout(delay){ return new Promise(function(resolve, reject) { setTimeout(function() { resolve(new

    Response('', { status: 408, statusText: 'Request timed out.' })) }, delay) }) } self.addEventListener('fetch', function(event) { // @paulirish fix for DevTools if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') { return // if the request is not for example.com, serve requests normally } else if (!event.request.url.includes('example.com')) { return } return event.respondWith(caches.match(event.request).then(function(response) { return response || Promise.race([ timeout(2000), fetch(event.request) ]) })) }) attempt the request force a 2-second timeout
  20. • Replica of your site • Restricted HTML: limited AMP

    elements • Restricted JS: no custom JS allowed • Restricted CSS: inlined, 50kb max • No repaint: fixed element sizing • No desktop: mobile-only • Shown within Google’s branding • Stale-while-revalidate: minimum 15 seconds refresh • Extra QA effort
  21. “[…] we now feel ready to take the next step

    and work to support more instant-loading content not based on AMP technology in areas of Google Search designed for this, like the Top Stories carousel” – AMP Project @ Google Source – https://bit.ly/2JakbtC