Image Optimization — Complete Guide
Image Optimization — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Next.js Tutorial on Toolliyo Academy.
On this page
Next.js Tutorial (LearnHub) · Lesson 25 of 100
Image Optimization
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Building apps · ~14 min read · Module 3: Data & Forms
Introduction
You know the basics now. Here we use Image Optimization in real LearnHub screens — layouts, data, and APIs. Still plain language, just a bit more depth. next/image resizes, lazy-loads, and serves modern formats (WebP). It prevents layout shift with width/height or fill. Configure remotePatterns for CDN course images. LearnHub catalog with fifty course thumbnails can crush mobile bandwidth. Optimized images improve LCP Core Web Vital and student experience.
Data and forms power course lists, enrollments, and progress. Learn these patterns slowly — test in a small page first.
When will you use this?
Reach for data fetching and Server Actions when pages need database content or form submissions.
- Enrollments, quiz scores, and course progress load from the server — often with Server Actions.
- Forms that enroll a student in a course use Server Actions instead of a separate REST call.
Real-world: Practo-style clinic portal
The Healthcare team building Practo-style clinic portal uses Image Optimization to serve sharp course thumbnails without slowing the catalog page. patients and doctors never see the TypeScript files — they just get a fast, reliable appointment booking and medical records UI.
Production-style code
import Image from 'next/image';
export function CourseThumb({ src, title }: { src: string; title: string }) {
return (
<Image
src={src}
alt={`${title} course thumbnail`}
width={320}
height={180}
className="rounded-lg object-cover"
sizes="(max-width: 768px) 100vw, 320px"
/>
);
}
What happens in production: In Practo-style clinic portal, getting Image Optimization right means patients and doctors trust the appointment booking and medical records UI every day.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
import Image from 'next/image';
export function CourseThumb({ src, title }: { src: string; title: string }) {
return (
<Image
src={src}
alt={`${title} course thumbnail`}
width={320}
height={180}
className="rounded-lg object-cover"
sizes="(max-width: 768px) 100vw, 320px"
/>
);
}
Line-by-line walkthrough
| Code | What it means |
|---|---|
import Image from 'next/image'; | Imports Image — optimized images with lazy loading and responsive sizes. |
export function CourseThumb({ src, title }: { src: string; title: string }) { | Named export — reusable function or component. |
return ( | Returns JSX — what the user sees in the browser. |
<Image | Part of the Image Optimization example — read it together with the lines before and after. |
src={src} | Part of the Image Optimization example — read it together with the lines before and after. |
alt={`${title} course thumbnail`} | Part of the Image Optimization example — read it together with the lines before and after. |
width={320} | Part of the Image Optimization example — read it together with the lines before and after. |
height={180} | Part of the Image Optimization example — read it together with the lines before and after. |
className="rounded-lg object-cover" | Part of the Image Optimization example — read it together with the lines before and after. |
sizes="(max-width: 768px) 100vw, 320px" | Part of the Image Optimization example — read it together with the lines before and after. |
/> | Part of the Image Optimization example — read it together with the lines before and after. |
); | Part of the Image Optimization example — read it together with the lines before and after. |
} | Closes a block started by { above. |
How it works (big picture)
- width and height reserve space.
- sizes tells browser which srcset width to pick.
- Remote domains must be listed in next.config images.remotePatterns.
Do this on your computer
- Replace
with Image on course card
- Add unsplash or local image paths
- Configure remotePatterns if using external URLs
- Run Lighthouse and check LCP improvement
- Read the real-world section and name which part of LearnHub uses this topic.
- Run the example locally with npm run dev and confirm the same behavior.
- Change one value in the example (route, text, or course id) and predict what will happen before you save.
Experiments — try changing this
- Change a string or route in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error overlay, then fix it.
- Use npm run dev while editing Image Optimization — the page hot-reloads on save.
Remember
next/image optimizes automatically. Always set alt, width, height. Configure remote domains in next.config.
Common questions
SVG with Image?
Often use or inline SVG for logos; Image targets raster photos.
How long should I spend on Image Optimization?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new concept; setup lessons may take one afternoon.
What if I get stuck on Image Optimization?
Re-read the line-by-line walkthrough, check the terminal and browser overlay for errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Image Optimization used in real jobs?
See the real-world section above — the same pattern appears in LMS, e-commerce, SaaS, and dashboards. Interviewers ask you to explain it using one concrete example.