JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsnextjsImage & Font Optimization
PrevNext
nextjs
beginner
9 min read

Image & Font Optimization

avif
cls
fonts
image
lazy-loading
lcp
nextjs
optimization
performance
webp

next/image automatically converts images to modern formats (WebP/AVIF), lazy-loads by default, prevents layout shift with required dimensions, and serves responsive sizes — next/font loads Google and local fonts with zero layout shift by hosting font files at build time.

Key Points

1Image Optimization

Automatic WebP/AVIF conversion, lazy loading by default, required dimensions prevent layout shift, responsive srcset via sizes prop.

2priority Prop

Disables lazy loading and preloads the image — use for above-the-fold hero images and LCP elements to improve Largest Contentful Paint.

3Font Optimization

next/font downloads Google Fonts at build time, self-hosts them, and uses size-adjust for zero layout shift — no external requests at runtime.

4Core Web Vitals

Image optimization improves LCP (Largest Contentful Paint) and CLS (Cumulative Layout Shift). Font optimization eliminates FOUT and CLS.

What You'll Learn

  • Use next/image with proper width, height, sizes, and priority props
  • Explain how automatic format conversion and lazy loading improve performance
  • Implement next/font for zero-layout-shift font loading
  • Know how these optimizations improve Core Web Vitals (LCP, CLS)

Deep Dive

Next.js provides built-in optimization for images and fonts — the two assets that most impact page load performance and user experience.

next/image Component

The <Image> component extends the HTML <img> tag with automatic optimization:

JSX
import Image from 'next/image';
 
<Image
  src="/hero.jpg"
  alt="Hero image"
  width={800}
  height={600}
  priority
/>

Automatic Format Conversion: Serves WebP or AVIF (smaller file sizes) to browsers that support them, falling back to the original format for older browsers. A 500KB JPEG might become a 100KB WebP — massive savings.

Lazy Loading: Images below the fold are loaded only when they enter the viewport (using Intersection Observer). Add priority for above-the-fold images (hero images, LCP elements) to disable lazy loading and preload them.

Layout Shift Prevention: width and height are required (or use fill for responsive containers). Next.js uses these to reserve space in the layout before the image loads — eliminating Cumulative Layout Shift (CLS), a Core Web Vital.

Responsive Images: The sizes prop generates responsive srcset values so the browser downloads the appropriate image size for the device:

JSX
<Image
  src="/hero.jpg"
  alt="Hero"
  fill
  sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
/>

Blur Placeholder: For local images, placeholder="blur" shows a blurred low-resolution version while the full image loads. For remote images, provide blurDataURL with a base64-encoded tiny image.

Image Loader: By default, Next.js optimizes images through its built-in image optimization server. For CDN-hosted images, configure remotePatterns in next.config.js and optionally use a custom loader for providers like Cloudinary, Imgix, or Akamai.

next/font

Font loading typically causes layout shift (FOUT — Flash of Unstyled Text) when the font file downloads and text reflows. next/font eliminates this:

HTML
import { Inter } from 'next/font/google';
 
const inter = Inter({ subsets: ['latin'] });
 
export default function Layout({ children }) {
  return <body className={inter.className}>{children}</body>;
}

How it works:

  • Build-time download: Google Fonts are downloaded at build time and self-hosted with your deployment — no external requests at runtime
  • Zero layout shift: Uses CSS size-adjust to match the fallback font's metrics to the custom font — text doesn't reflow when the font loads
  • Automatic subsetting: Only includes the character sets you specify, reducing font file size
  • Local fonts: next/font/local loads custom font files from your project with the same optimization

next/script

For third-party scripts (analytics, chat widgets, ads), <Script> controls loading strategy:

  • strategy="afterInteractive" (default) — loads after the page is interactive
  • strategy="lazyOnload" — loads during idle time (lowest priority)
  • strategy="beforeInteractive" — loads before hydration (use sparingly)
  • strategy="worker" — offloads to a web worker (experimental)

Key Interview Distinction

next/image automatically converts to modern formats, lazy-loads below-the-fold images, prevents layout shift with required dimensions, and serves responsive sizes. Use priority for LCP images. next/font downloads fonts at build time and self-hosts them — zero external requests, zero layout shift. Both solve Core Web Vital issues (LCP, CLS) automatically.

Fun Fact

Next.js image optimization was inspired by a study showing that images account for 50% of total page weight on average across the web. The automatic WebP/AVIF conversion alone can reduce image sizes by 25-50%. Google's own performance studies showed that sites using next/image had 57% better LCP scores on average compared to raw <img> tags.

Learn These First

Next.js Fundamentals

beginner

Continue Learning

Performance Optimization

advanced

Server & Client Components

intermediate

Practice What You Learned

What is next/image and why should you use it over the regular img tag?
junior
optimization
The `next/image` component automatically optimizes images with lazy loading, responsive sizing, modern formats (WebP/AVIF), and prevents Cumulative Layout Shift (CLS), improving both performance and Core Web Vitals.
Previous
Navigation & Linking
Next
Performance Optimization
PrevNext