Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Performance
  3. Guides
  4. Lazy loading

Lazy loading

Lazy loading is a strategy to identify resources as non-blocking (non-critical) and load these only when needed. It's a way to shorten the length of thecritical rendering path, which translates into reduced page load times.

Lazy loading can occur on different moments in the application, but it typically happens on some user interactions such as scrolling and navigation.

Overview

As the web has evolved, we have come to see huge increases in the number and size of assets sent to users.Between 2011 and 2019, the median resource weight increased from~100KB to~400KB for desktop and~50KB to~350KB for mobile. While Image size has increased from~250KB to~900KB on desktop and~100KB to~850KB on mobile.

One of the methods we can use to tackle this problem is to shorten theCritical Rendering Path length by lazy loading resources that are not critical for the first render to happen.A practical example would be when you land on the home page of an e-commerce site with a link to a cart page/section, and none of the cart page's resources (such as JavaScript, CSS, and images) are downloadeduntil you navigate there.

Strategies

Lazy loading can be applied to multiple resources and through multiple strategies.

General

Code splitting

JavaScript, CSS and HTML can be split into smaller chunks. This enables sending the minimal code required to provide value upfront, improving page-load times. The rest can be loaded on demand.

  • Entry point splitting: separates code by entry point(s) in the app
  • Dynamic splitting: separates code wheredynamic import() expressions are used

JavaScript

Script type module

Any script tag withtype="module" is treated as aJavaScript module and is deferred by default.

CSS

By default, CSS is treated as arender blocking resource, so the browser won't render any processed content until theCSSOM is constructed. CSS must be thin, delivered as quickly as possible, and the usage media types and queries are advised to unblock rendering.

html
<link href="style.css" rel="stylesheet" media="all" /><link href="portrait.css" rel="stylesheet" media="(orientation:portrait)" /><link href="print.css" rel="stylesheet" media="print" />

It is possible to perform someCSS optimizations to achieve that.

Fonts

By default, font requests are delayed until the render tree is constructed, which can result in delayed text rendering.

It is possible to override the default behavior and preload web font resources using<link rel="preload">, theCSSfont-display descriptor, and theFont Loading API.

See also:Element Link.

Images and iframes

Very often, webpages contain many images that contribute to data-usage and how fast a page can load. Most of those images are off-screen (non-critical), requiring a user interaction, like scrolling, in order to view them.

Loading attribute

Theloading attribute on an<img> element, or theloading attribute on an<iframe>, can be used to instruct the browser to defer loading of images/iframes that are off-screen until the user scrolls near them.This allows non-critical resources to load only if needed, potentially speeding up initial page loads and reducing network usage.

html
<img loading="lazy" src="image.jpg" alt="..." /><iframe loading="lazy" src="video-player.html" title="..."></iframe>

Theload event fires when the eagerly-loaded content has all been loaded. At that time, it's entirely possible (or even likely) that there may be lazily-loaded images or iframes within thevisual viewport that haven't yet loaded.

You can determine if a given image has finished loading by examining the value of its Booleancomplete property.

Intersection Observer API

Intersection Observers allow the user to know when an observed element enters or exits the browser's viewport.

Event handlers

When browser compatibility is crucial, there are a few options:

Specifications

Specification
HTML
# lazy-loading-attributes

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp