Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Published
2 min read
Up to date

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

Improve PageSpeed Insights Score with Lazy Loading Iframes

How to save bandwidth and speed up your site by lazy-loading iframes

Lazy-loading images is pretty common these days, but did you know you can do the same thing with iframes?

By deferring offscreen iframes from being loaded until the user scrolls near them, you can significantly improve your site’s performance.

Let’s break down how to implement lazy-loading iframes, why it’s useful, and what browser support looks like.

Before and After: Adding Lazy Loading to Iframes

Here’s an iframe before adding lazy-loading:

<iframe
src="https://trevorlasn.substack.com/embed"
class="w-full"
height="320"
title="Trevor I. Lasn newsletter"
frameborder="0"
scrolling="no"
>
</iframe>

Now, let’s add the loading="lazy" attribute:

<iframe
loading="lazy"
src="https://trevorlasn.substack.com/embed"
class="w-full"
height="320"
title="Trevor I. Lasn newsletter"
frameborder="0"
scrolling="no"
>
</iframe>

That’s it! With just one attribute, the iframe will load only when the user scrolls near it.

Why Lazy-Load Iframes?

Iframes are often used to embed third-party content, like YouTube videos, social media posts, or ads. Most of this content doesn’t appear immediately in the user’s viewport. But when loaded eagerly, users still pay the cost of downloading the iframe’s data and JavaScript, even if they never scroll down to it.

For example, loading a YouTube embed upfront can pull in over 500 KB of resources that users might not even need. By lazy-loading iframes, you defer loading them until users scroll near them. This saves bandwidth and improves page performance, especially for mobile users or those on slower connections.

How Lazy Loading Works

When you add loading="lazy" to an iframe, the browser delays loading it until it’s close to the user’s viewport. This is particularly useful for content that appears below the fold.

Here’s an example:

<iframe
src="https://www.youtube.com/embed/YJGCZCaIZkQ"
loading="lazy"
width="560"
height="315"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
>
</iframe>

Without lazy-loading, this iframe would load as soon as the page starts loading, even if the user never scrolls down to it. But with the loading="lazy attribute, it only loads when the user approaches it, saving bandwidth and improving your site’s Time to Interactive (TTI).

Browser Support

Lazy-loading for iframes is supported in most modern browsers:

  • Chrome: 77+
  • Edge: 79+
  • Firefox: 121+
  • Safari: 16.4+

This wide browser support means you can confidently implement lazy-loading without worrying about breaking your site for most users.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
3 min read

CSS ::target-text for Text Highlighting

A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly

Dec 17, 2024
Read article
Webdev
3 min read

The HTML Native Search Element

The search HTML element is a container that represents the parts of the web page with search functionality

Dec 2, 2024
Read article
Webdev
4 min read

Explicit is better than implicit

Clarity is key: being explicit makes your code more readable and maintainable.

Sep 4, 2024
Read article
Webdev
3 min read

HTML Details Element: The Native Accordion You're Not Using

Discover how the HTML details element can replace your JavaScript accordions and why it might be better than your current solution

Dec 10, 2024
Read article
Webdev
7 min read

How to Land Your First Tech Job

A developer's guide to tech interviews - from someone who sits on both sides of the table

Oct 24, 2024
Read article
Webdev
3 min read

CSS content-visibility: The Web Performance Boost You Might Be Missing

The content-visibility CSS property delays rendering an element, including layout and painting, until it is needed

Dec 5, 2024
Read article
Webdev
4 min read

The What, Why, and How of Using a Skeleton Loading Screen

Skeleton loading screens enhance user experience and make your app feel faster

Nov 12, 2020
Read article
Webdev
7 min read

Tips for Reducing Cyclomatic Complexity

Cyclomatic complexity is like counting how many ways a car can go. More options make it harder to drive because you have to make more decisions, which can lead to confusion.

Sep 10, 2024
Read article
Webdev
4 min read

Self-Taught Developer's Guide to Thriving in Tech

How to turn your non-traditional background into your biggest asset

Sep 28, 2024
Read article

Become a better engineer

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Many companies have a fixed annual stipend per engineer (e.g. $2,000) for use towards learning resources. If your company offers this stipend, you can forward them your invoices directly for reimbursement. By using my affiliate links, you support my work and get a discount at the same!


This article was originally published on https://www.trevorlasn.com/blog/lazy-loading-iframes. It was written by a human and polished using grammar tools for clarity.