Projects · Final Page

Lazy Loading

Make apps blazing fast. Master Intersection Observer for lazy images, dynamic import() for code splitting, route-based lazy loading, prefetching strategies, and measuring LCP improvements.

What You Will Build

1

Lazy Images

IntersectionObserver swaps data-src to src just before the image scrolls into view

2

Dynamic import()

Load JavaScript modules on demand — not before the user needs them

3

Route Splitting

Each route gets its own bundle chunk — ship only what the current page needs

4

Prefetching

Load the next page silently during idle time so navigation feels instant

Lazy Loading Techniques

TechniqueWhen to useKey APIGotcha
Lazy images (native)Images below the foldloading="lazy"No control over threshold
Lazy images (Observer)Fine-grained controlIntersectionObserverMust unobserve after load
Dynamic import()Heavy libraries, rare featuresimport('./mod.js')Bundler must see static string for splitting
Route splittingSPA with many pagesReact.lazy / import()Need Suspense/fallback for UX
Prefetch on hoverPredictive — fast navigationrequestIdleCallback + import()Don't prefetch on slow networks
<link rel=prefetch>Entire pages/assetsHTML hintMay be ignored by browser
fetchpriority hintBoost or demote LCP imagefetchpriority="high"Only affects fetch priority, not laziness

Common Pitfalls

Lazy loading the hero image (LCP element) — causes a huge CLS shift and delayed LCP score
Always eager-load above-the-fold images. Add fetchpriority="high" to the LCP image. Only lazy-load below-the-fold images.
Not calling observer.unobserve(el) after the image loads — observer keeps firing forever
Always call obs.unobserve(entry.target) inside the IntersectionObserver callback after handling the element.
Dynamic import with a fully variable path: import(variable) — bundler cannot statically analyse it, no code splitting
Use a partial static string: import(`./pages/${name}.js`) — bundler can glob-match and split all files in that directory.
No loading skeleton while the lazy image loads — blank space jumps to image causing layout shift (CLS)
Use aspect-ratio or explicit width/height on the container to reserve space before the image loads.

Build It Live

Code Editor

<p>Scroll inside the preview: images and the "heavy" widget load only when they come into view.</p>
<div class="spacer">↓</div>
<img data-src="https://picsum.photos/id/1015/400/200" alt="River" width="400" height="200">
<div class="spacer">↓</div>
<img data-src="https://picsum.photos/id/1016/400/200" alt="Canyon" width="400" height="200">
<div class="spacer">↓</div>
<div id="widget">Widget not loaded yet</div>
<p id="log"></p>

Preview

Course Complete! 🎉

Congratulations! You have completed the full JavaScript curriculum — from variables and functions all the way to lazy loading, routing, and real-time apps. You now have the skills to build modern, performant, production-ready JavaScript applications.

81
Lessons
7
Projects
100%
Complete
∞
Possibilities

Lazy Loading — step by step

1 / 4
1

IntersectionObserver API

IntersectionObserver fires a callback when a target element enters or exits the viewport. You create an observer with a callback and options (threshold, rootMargin), then call observer.observe on each element you want to watch.

1 of 4
💡Use rootMargin to Pre-load Slightly Ahead
Set rootMargin: 200px on your IntersectionObserver so elements start loading when they are 200px below the visible area. This hides the loading latency — by the time the user scrolls to an image, it has already loaded.

🎯 Quick Check

What should you do after an element finishes lazy loading to avoid unnecessary work?

Final page reached

You made it! 🎉

← Client-Side Routing