Next.jsEasy
What is lazy loading and how does Next.js handle it?
Lazy loading delays loading a resource until it is needed, which reduces initial download size and speeds up first render. Next.js lazy-loads images by default with next/image, splits JavaScript per route automatically, and lets you lazy-load components with next/dynamic or React.lazy, and scripts with next/script.
Images
<Image>uses nativeloading="lazy": offscreen images load when they approach the viewport.- Do not lazy-load the above-the-fold LCP image; mark it with
preloadorloading="eager"/fetchPriority="high".
JavaScript
- Automatic code splitting: each route only loads the code it uses.
- Server Components send no component JavaScript at all.
next/dynamicsplits a Client Component into its own chunk and loads it when rendered.ssr: falseskips server rendering for browser-only components.- Load on interaction: render the dynamic component only after a click (a chart in a tab, a rich text editor in a modal).
- Dynamic
import()for libraries used in event handlers, for example importing a PDF library when the user clicks "Export". - Next.js 16.4 adds
experimental.turbopackLazyDynamicImportsto compile clientimport()chunks in dev only when requested, which speeds up the dev server.
Scripts and media
<Script strategy="lazyOnload">loads third-party scripts during browser idle time.- Native
loading="lazy"on<iframe>for embeds.
Pitfalls
Lazy-loading everything adds loading states and network requests on interaction. Use it for heavy or rarely used pieces.
Code example
tsx
1// app/report/page.tsx2import Image from "next/image";3import { ChartTab } from "./chart-tab"; // "use client"45export default function Report() {6 return (7 <main>8 <Image src="/hero.jpg" alt="Q3 results" width={1200} height={500} preload /> {/* not lazy */}9 <Image src="/team.jpg" alt="Team" width={600} height={400} /> {/* lazy by default */}10 <ChartTab />11 </main>12 );13}1415// app/report/chart-tab.tsx16// "use client";17// import dynamic from "next/dynamic";18// import { useState } from "react";19// const Chart = dynamic(() => import("./heavy-chart"), { loading: () => <p>Loading chart…</p> });20// export function ChartTab() {21// const [show, setShow] = useState(false);22// return <>{<button onClick={() => setShow(true)}>Show chart</button>}{show && <Chart />}</>;23// }24// Export button: const { jsPDF } = await import("jspdf");
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.