Lazy loading defers loading a resource until it's actually needed: off-screen images and iframes, code for routes or widgets the user hasn't opened, data below the fold. It shrinks the initial payload, frees bandwidth and main-thread time for what's visible, and lowers cost for users who never scroll or click. The key rule: never lazy-load what's needed for the first view, especially the LCP image.
<img src="/photos/beach.avif" alt="Beach at sunset"
width="800" height="533" loading="lazy" decoding="async">
<iframe src="https://www.youtube-nocookie.com/embed/xyz" title="Product demo"
loading="lazy" width="560" height="315"></iframe>
width/height (or CSS aspect-ratio) so the placeholder reserves space and nothing shifts (CLS).loading="lazy" to the hero/LCP image. Instead mark it fetchpriority="high".import()// load the heavy editor only when the user opens it
openButton.addEventListener('click', async () => {
const { createEditor } = await import('./editor.js');
createEditor(document.querySelector('#editor'));
});
// React: route- or component-level splitting
const Settings = lazy(() => import('./Settings'));
<Suspense fallback={<Spinner />}>
<Settings />
</Suspense>
Bundlers turn each import() into a separate chunk. Good candidates: routes, modals, charts, rich-text editors, maps, admin-only features.
const io = new IntersectionObserver((entries, obs) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
loadWidget(entry.target); // fetch data / import code / swap src
obs.unobserve(entry.target);
}
}, { rootMargin: '200px' }); // start a bit before it's visible
document.querySelectorAll('[data-lazy-widget]').forEach((el) => io.observe(el));
Use it for components, infinite scroll, or analytics that should only fire when content is seen.
content-visibility: auto skips rendering off-screen sections while still loading them.<link rel="prefetch">, router prefetch, or import() on pointerenter.Compare initial JS/bytes and LCP/INP before and after in Lighthouse and field data. A good outcome: smaller initial bundle, unchanged or better LCP, and no new CLS.
loading="lazy" for images/iframes, import() + Suspense for code, IntersectionObserver for custom cases.Spotted a mistake or a rights concern? Report an issue