Slide 1

Slide 1 text

Boosting Frontend Speed: Quick Wins for Backend Developers Paul Conroy / @conroyp 21st June, 2024

Slide 2

Slide 2 text

From Dublin, Ireland Started playing with the web 30+ years ago (Notepad, Frontpage & Geocities!) CTO at Square1 conroyp.com / @conroyp Paul Conroy ๐Ÿ‘ด ๐ŸŒ ๐Ÿ‡ฎ๐Ÿ‡ช

Slide 3

Slide 3 text

In the beginning โ— First website in 1991 โ— Plain text, links - simple! https://info.cern.ch/hypertext/WWW/TheProject.html

Slide 4

Slide 4 text

In the beginning โ— First website in 1991 โ— Plain text, links - simple! โ— Img tag! Scrolling marquees! โ— Basic CSS

Slide 5

Slide 5 text

In the beginning โ— First website in 1991 โ— Plain text, links - simple! โ— Img tag! Scrolling marquees! โ— Basic CSS

Slide 6

Slide 6 text

In the beginning โ— First website in 1991 โ— Plain text, links - simple! โ— Img tag! Scrolling marquees! โ— Basic CSS -> More advanced CSS โ— Interactivity (Java applets, Flash, Javascript)

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Evolving ecosystem

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

โ€œThe site is slowโ€ - Your boss

Slide 21

Slide 21 text

Improved metrics โ— Stand-alone metrics โ— Objectively quantify performance โ— Core Web Vitals - how does the site โ€œfeelโ€? โ— Google search algorithm โ— CLS, LCP, INP

Slide 22

Slide 22 text

Improved metrics โ— CLS - how much do things jump around on a page? โ— LCP - how long does it take to draw the biggest thing on the page? โ— INP - how long between clicking and seeing something happen? (menu open etc)

Slide 23

Slide 23 text

โ— Uses same data Google uses in rankings โ— Based on real-world Chrome user data โ— More than page speed! Sitemaps, crawl errors, inbound links โ— Data has a lag of ~30 days https://search.google.com/search-console/ Search Console

Slide 24

Slide 24 text

https://pagespeed.web.dev/ โ— Aggregate score 1 - 100 Page Speed Insights

Slide 25

Slide 25 text

https://pagespeed.web.dev/ โ— Aggregate score 1 - 100 โ— Separate score for individual CWV metrics โ— Detailed information on reasons behind failing metrics Page Speed Insights

Slide 26

Slide 26 text

โ— Aggregate score 1 - 100 โ— Separate score for individual CWV metrics โ— Detailed information on reasons behind failing metrics โ— Lab and โ€œreal worldโ€ data โ— Server cache of ~30 minutes https://pagespeed.web.dev/ Page Speed Insights

Slide 27

Slide 27 text

โ— Runs locally in Chrome โ— Same as Page Speed Insights โ— Local conditions impact results (cookies, cache, etc) โ— Potential for significant variations run-to-run Lighthouse

Slide 28

Slide 28 text

โ— Runs locally in Chrome โ— Same as Page Speed Insights โ— Local conditions impact results (cookies, cache, etc) โ— Potential for significant variations run-to-run โ— Lighthouse-php Lighthouse

Slide 29

Slide 29 text

Lighthouse-PHP https://spatie.be/docs/lighthouse-php/v1/introduction

Slide 30

Slide 30 text

Lighthouse-PHP https://spatie.be/docs/lighthouse-php/v1/introduction

Slide 31

Slide 31 text

Lighthouse-PHP Consider a โ€œ?noads=1โ€ url toggle to get a โ€œrawโ€ result https://spatie.be/docs/lighthouse-php/v1/introduction ๐Ÿ’ก

Slide 32

Slide 32 text

How exactly are we meant to improve a slow website?

Slide 33

Slide 33 text

Option 1

Slide 34

Slide 34 text

Option 1

Slide 35

Slide 35 text

Option 1 โ— Send the team snowboarding for 6 months โ— Browsers and hardware will improve in that time โ— Problem solved

Slide 36

Slide 36 text

Option 1 โ— Send the team snowboarding for 6 months โ— Browsers and hardware will improve in that time โ— Problem solved โ— Might need an Option 2โ€ฆ

Slide 37

Slide 37 text

4 Horsemen of the Slowpocalypse Images Ads Videos Fonts

Slide 38

Slide 38 text

Get rid of them all!

Slide 39

Slide 39 text

Improve page speed score?

Slide 40

Slide 40 text

Improve page speed score โœ…

Slide 41

Slide 41 text

Improve page speed score โœ… ๐Ÿ˜Ž ๐Ÿน ๐Ÿ–

Slide 42

Slide 42 text

Images

Slide 43

Slide 43 text

โ— Images make up roughly 60% of average page load โ— More responsive sites โ†’ more image sizes being generated โ— Particularly tricky to manage when handling user- generated content! IMAGES JS HTML CSS, FONTS

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Image sizing โ— Stops the image from appearing too large

Slide 47

Slide 47 text

Image sizing โ— Stops the image from appearing too large โ— Still requires a large image download

Slide 48

Slide 48 text

โ— Stops the image from appearing too large โ— File size is appropriate to the image being shown Image sizing โ— Stops the image from appearing too large โ— Still requires a large image download

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Choosing the right size โ— Detecting the browser size based on User Agent / headers โ— Generate different markup based on browser size

Slide 54

Slide 54 text

Choosing the right size โ— Detecting the browser size based on User Agent / headers โ— Generate different markup based on browser size โ— Impact on URL-based caching โ— How does it handle device resizing after load?

Slide 55

Slide 55 text

Responsive images โ— srcset allows multiple sizes to be specified in markup โ— Browser can determine best one to load โ— A single URL cache works fine! โ— Resizing of the browser? Right image size loaded โ— Getting the sizes right can be fiddly (high density devices)

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Smaller images โ— Webp - image format from Google โ— 25 - 30% file size reduction, with minimal quality drop โ— Conversion support in many PHP image libraries โ— 15 years old, but widespread support only recently https://photutorial.com/image-format-comparison-statistics/

Slide 58

Slide 58 text

Can I Useโ€ฆ? https://caniuse.com/?search=webp

Slide 59

Slide 59 text

โ— Important to โ€œreserveโ€ space! โ— Tell the browser how much space weโ€™ll need โ— Different sizes on different devices? Define heights in media queries for each breakpoint

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Impact on page speed

Slide 70

Slide 70 text

Impact on page speed

Slide 71

Slide 71 text

Lazy loading โ— Only load assets when they are needed โ— Potentially save a lot of bandwidth โ— Load images just before they scroll into view

Slide 72

Slide 72 text

Lazy loading โ— Only load assets when they are needed โ— Potentially save a lot of bandwidth โ— Load images just before they scroll into view

Slide 73

Slide 73 text

THEN โ— Load polyfill library โ— When image top is near viewport, swap data-src and src โ— Scroll listener thrashed rendering thread! Polyfills

Slide 74

Slide 74 text

THEN โ— Load polyfill library โ— When image top is near viewport, swap data-src and src โ— Scroll listener thrashed rendering thread! Polyfills

Slide 75

Slide 75 text

NOW โ— loading=โ€lazyโ€, job done! ๐Ÿ”ฅ Polyfills

Slide 76

Slide 76 text

https://caniuse.com/?search=loading="lazy"

Slide 77

Slide 77 text

Lazyload everything?

Slide 78

Slide 78 text

โ— Tempting to apply loading=โ€lazyโ€ to all images โ— Actively harmful if applied to images above the fold โ— Browser is trying to optimise loading - help it out! Lazyload everything?

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Lazy loading background images โ— Background images usually large, full width, lots of detail โ— CSS property - background-image โ— Some lazy loading polyfills support background images, some donโ€™t!

Slide 83

Slide 83 text

Lazy loading background images โ— Background images usually large, full width, lots of detail โ— CSS property - background-image โ— Some lazy loading polyfills support background images, some donโ€™t! โ— IntersectionObserver replaces window.scroll

Slide 84

Slide 84 text

IntersectionObserver

Slide 85

Slide 85 text

IntersectionObserver

Slide 86

Slide 86 text

IntersectionObserver

Slide 87

Slide 87 text

IntersectionObserver

Slide 88

Slide 88 text

IntersectionObserver

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Display ads โ— A developer has no way of knowing if an ad will show or not until the ad server responds โ— Page content โ€œjumpingโ€ - very bad for CLS โ— Very annoying for users! โ— Accidental clicks, maybe good for site owners?

Slide 93

Slide 93 text

Display ads โ— A developer has no way of knowing if an ad will show or not until the ad server responds โ— Page content โ€œjumpingโ€ - very bad for CLS โ— Very annoying for users! โ— Accidental clicks, maybe good for site owners?

Slide 94

Slide 94 text

Two-click penalty โ— Google adds overlay on ads, asks user to click again โ— Unexpected behaviour! Most users wonโ€™t click again โ— Google donโ€™t tell you this is happening! โ— It doesnโ€™t show to every user โ— How do we know when itโ€™s happening?

Slide 95

Slide 95 text

Two-click penalty โ— Google adds overlay on ads, asks user to click again โ— Unexpected behaviour! Most users wonโ€™t click again โ— Google donโ€™t tell you this is happening! โ— It doesnโ€™t show to every user โ— How do we know when itโ€™s happening? ๐Ÿ“‰ ๐Ÿ“‰ ๐Ÿ’ธ

Slide 96

Slide 96 text

Avoiding the Two-click Penalty โ— Label ads clearly โ— Consistent spacing from content โ— Avoid areas near menus, buttons, etc โ— Prevention is better than cure!

Slide 97

Slide 97 text

Reducing CLS โ— Min-height on the ad container, matching expected ad unit size โ— Same technique as with images

Slide 98

Slide 98 text

โ— Min-height on the ad container, matching expected ad unit size โ— Same technique as with images โ— Ad loads in, taking the reserved space, no CLS or โ€œJumpingโ€ Reducing CLS

Slide 99

Slide 99 text

โ— Min-height on the ad container, matching expected ad unit size โ— Same technique as with images โ— Ad loads in, taking the reserved space, no CLS or โ€œJumpingโ€ Reducing CLS

Slide 100

Slide 100 text

Variable ad sizes? โ— Responsive ad sizes (single request in markup) โ— Google might give us more than we ask for! โ— How do we reserve the right space for each ad when we donโ€™t know the size?

Slide 101

Slide 101 text

Callbacks โ— Google Ad Manager has a slotRenderEnded callback โ— When an ad loads, check what div it was in โ— If ad is smaller than our min height, remove the min height

Slide 102

Slide 102 text

Callbacks โ— Set min height to that of most popular ad unit โ— โ€œBakes inโ€ some CLS, but should be net positive

Slide 103

Slide 103 text

Results โ— Users experiencing bad CLS fell by 64% overnight โ— Regular review of most popular slots is critical

Slide 104

Slide 104 text

4 Horsemen of the Slowpocalypse Images Ads Videos Fonts

Slide 105

Slide 105 text

User Engagement & SEO benefits! โ— Popular way to share content โ— Strong user engagement โ— Helps with SEO โ— Easy embed options โ— Hugely expensive payloads!

Slide 106

Slide 106 text

How expensive are they really though?

Slide 107

Slide 107 text

How expensive are they really though?

Slide 108

Slide 108 text

How expensive are they really though?

Slide 109

Slide 109 text

How expensive are they really though?

Slide 110

Slide 110 text

A facade, you sayโ€ฆ ๐Ÿค” How expensive are they really though?

Slide 111

Slide 111 text

Web components Lite Youtube Embed by Paul Irish This component: โ— Loads a placeholder image โ— On hover warms the connection to youtube โ— On click, swaps the image for the player code

Slide 112

Slide 112 text

Web components Lite Youtube Embed by Paul Irish This component: โ— Loads a placeholder image โ— On hover warms the connection to youtube โ— On click, swaps the image for the player code

Slide 113

Slide 113 text

Web components Lite Youtube Embed by Paul Irish This component: โ— Loads a placeholder image โ— On hover warms the connection to youtube โ— On click, swaps the image for the player code

Slide 114

Slide 114 text

Performance impact?

Slide 115

Slide 115 text

Performance impact?

Slide 116

Slide 116 text

Performance impact?

Slide 117

Slide 117 text

Performance impact?

Slide 118

Slide 118 text

Performance impact? ๐Ÿข ๐Ÿฐ โšกโšกโšก

Slide 119

Slide 119 text

SEO impact โ— Google is good at recognising embedded videos! โ— Doesnโ€™t recognise components so well โ— Use structured data alongside custom components โ— VideoObject schema https://schema.org/VideoObject

Slide 120

Slide 120 text

4 Horsemen of the Slowpocalypse Images Ads Videos Fonts

Slide 121

Slide 121 text

Web fonts โ— A key part of a dynamic, attractive web โ— Brand โ€œpersonalityโ€, can brighten up dull sites โ— Customisation comes at a cost!

Slide 122

Slide 122 text

โ— Wide browser support โ— Copied through the ages! โ— Do we still need it all?

Slide 123

Slide 123 text

https://caniuse.com/?search=woff2

Slide 124

Slide 124 text

โ— Wide browser support โ— Copied through the ages! โ— Do we still need it all?

Slide 125

Slide 125 text

โ— Wide browser support โ— Copied through the ages! โ— Do we still need it all?

Slide 126

Slide 126 text

โ— woff2 is all we need โ— Trim the markup โ— Delete extra font files

Slide 127

Slide 127 text

What is a font?

Slide 128

Slide 128 text

Font subsetting โ— Removing all characters we wonโ€™t use โ— Glyphhanger, install via npm or pip โ— Convert to woff2 while weโ€™re at it

Slide 129

Slide 129 text

Font subsetting

Slide 130

Slide 130 text

Before

Slide 131

Slide 131 text

After From 403kb to 138kb - nearly a 65% reduction in file size!

Slide 132

Slide 132 text

What happens if someone uses a character not in my font set? โ— Browser tries all the fonts defined in the font stack, in order โ— If the character is not found in any of them, fall back to the system font โ— If the system font has nothing, a placeholder box or empty space will show

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

โ— MS Outlook converts :) :| and :( into smileys via Wingdings โ— Those Wingding character codes use the slots for J / K / L โ— No Wingdings on your system? Fall back to J / K / L in the next available font

Slide 135

Slide 135 text

Google Fonts Subset via URL: Specific characters only:

Slide 136

Slide 136 text

Google Fonts Self-hosting Google Fonts - https://github.com/spatie/laravel-google-fonts Subset via URL: Specific characters only:

Slide 137

Slide 137 text

โ— Faster fonts, unblock rendering thread sooner โ— LCP is probably an image, but is in a queue โ— Itโ€™s critical to understand the data points you are looking at! Is this data telling me what I think it is?

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

Incidents By Driver

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

No content

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

โ— When dealing with image issues, have we other blocking resources? โ— Improving font loading can unblock threads, allowing faster image loading โ— Holistic view - top line metrics rarely influenced by solely one thing

Slide 151

Slide 151 text

4 Horsemen of the Slowpocalypse Images Ads Videos Fonts

Slide 152

Slide 152 text

Whatโ€™s next?

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

ADDY OSMANI โ— Chrome engineering lead โ— Web performance โ— New browser features https://twitter.com/addyosmani RICK VISCOMI โ— Web Perf DevRel at Chrome โ— Optimisations for CWV โ— CrUX report dataโ€จ https://twitter.com/rick_viscomi WEB.DEV โ— All aspects of web dev โ— Sections focused on perf metricsโ€จ https://web.dev/ SPATIE โ— Media Library โ— Lighthouse PHP โ— Self-host Google Fontsโ€จ https://spatie.be/

Slide 155

Slide 155 text

Takeaways? Images โ— Pre-sized correctly โ— Reserve space in the markup โ— Use native browser features, remove polyfills Fonts โ— Subsetting with Glyphanger โ— Avoid google fonts, self-host Ads โ— Reserve space (avoid CLS) โ— Avoid 2-click penalty! Labelling and positioning Videos โ— Use facades โ— SEO impact! Structured data

Slide 156

Slide 156 text

Conroyp.com โ€จ @conroyp โ€จ [email protected] Thank you! ๐ŸŒ ๐ŸŒ ๐Ÿ“ง