You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This loads some JavaScript eagerly that is unused during first render, which is obviously inefficient.
The Solution
It turns out you can massively improve load times and lighthouse scores by moving the src embed into srcdoc (StackOverflow answer) and requiring an extra click to load the JS:
This one change (diff) causes much better performance.
slug: faster-youtube-embeds
tags: performance, embeds, youtube
cover_image: https://user-images.githubusercontent.com/6764957/173108977-ac7f5f0f-dc70-4982-bd48-9871885136ed.png
description: It turns out you can massively improve load times and lighthouse scores by moving the
src
embed intosrcdoc
and requiring an extra click to load the JS!TLDR
The Problem
When I ran a PageSpeed Insights test on https://www.swyx.io/new-mac-setup-2021 I was surprised to find really poor performance:
screenshot
This was an obvious result of me loading the YouTube embed by YouTube's recommended method:
This loads some JavaScript eagerly that is unused during first render, which is obviously inefficient.
The Solution
It turns out you can massively improve load times and lighthouse scores by moving the
src
embed intosrcdoc
(StackOverflow answer) and requiring an extra click to load the JS:This one change (diff) causes much better performance.
screenshot
The new embed code is here:
This works without JavaScript; if you use a framework it's up to you to figure out how to adapt it to your framework.
Alternative
I haven't tried this yet but probably should - https://github.com/paulirish/lite-youtube-embed I just have a wariness around web components and dont know the JS overhead.
The text was updated successfully, but these errors were encountered: