Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply Next.js best practices for RSC boundaries, async APIs, routing, metadata, and optimization.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
image.md
1# Image Optimization23Use `next/image` for automatic image optimization.45## Always Use next/image67```tsx8// Bad: Avoid native img9<img src="/hero.png" alt="Hero" />1011// Good: Use next/image12import Image from 'next/image'13<Image src="/hero.png" alt="Hero" width={800} height={400} />14```1516## Required Props1718Images need explicit dimensions to prevent layout shift:1920```tsx21// Local images - dimensions inferred automatically22import heroImage from './hero.png'23<Image src={heroImage} alt="Hero" />2425// Remote images - must specify width/height26<Image src="https://example.com/image.jpg" alt="Hero" width={800} height={400} />2728// Or use fill for parent-relative sizing29<div style={{ position: 'relative', width: '100%', height: 400 }}>30<Image src="/hero.png" alt="Hero" fill style={{ objectFit: 'cover' }} />31</div>32```3334## Remote Images Configuration3536Remote domains must be configured in `next.config.js`:3738```js39// next.config.js40module.exports = {41images: {42remotePatterns: [43{44protocol: 'https',45hostname: 'example.com',46pathname: '/images/**',47},48{49protocol: 'https',50hostname: '*.cdn.com', // Wildcard subdomain51},52],53},54}55```5657## Responsive Images5859Use `sizes` to tell the browser which size to download:6061```tsx62// Full-width hero63<Image64src="/hero.png"65alt="Hero"66fill67sizes="100vw"68/>6970// Responsive grid (3 columns on desktop, 1 on mobile)71<Image72src="/card.png"73alt="Card"74fill75sizes="(max-width: 768px) 100vw, 33vw"76/>7778// Fixed sidebar image79<Image80src="/avatar.png"81alt="Avatar"82width={200}83height={200}84sizes="200px"85/>86```8788## Blur Placeholder8990Prevent layout shift with placeholders:9192```tsx93// Local images - automatic blur hash94import heroImage from './hero.png'95<Image src={heroImage} alt="Hero" placeholder="blur" />9697// Remote images - provide blurDataURL98<Image99src="https://example.com/image.jpg"100alt="Hero"101width={800}102height={400}103placeholder="blur"104blurDataURL="data:image/jpeg;base64,/9j/4AAQSkZJRg..."105/>106107// Or use color placeholder108<Image109src="https://example.com/image.jpg"110alt="Hero"111width={800}112height={400}113placeholder="empty"114style={{ backgroundColor: '#e0e0e0' }}115/>116```117118## Priority Loading119120Use `priority` for above-the-fold images (LCP):121122```tsx123// Hero image - loads immediately124<Image src="/hero.png" alt="Hero" fill priority />125126// Below-fold images - lazy loaded by default (no priority needed)127<Image src="/card.png" alt="Card" width={400} height={300} />128```129130## Common Mistakes131132```tsx133// Bad: Missing sizes with fill - downloads largest image134<Image src="/hero.png" alt="Hero" fill />135136// Good: Add sizes for proper responsive behavior137<Image src="/hero.png" alt="Hero" fill sizes="100vw" />138139// Bad: Using width/height for aspect ratio only140<Image src="/hero.png" alt="Hero" width={16} height={9} />141142// Good: Use actual display dimensions or fill with sizes143<Image src="/hero.png" alt="Hero" fill sizes="100vw" style={{ objectFit: 'cover' }} />144145// Bad: Remote image without config146<Image src="https://untrusted.com/image.jpg" alt="Image" width={400} height={300} />147// Error: Invalid src prop, hostname not configured148149// Good: Add hostname to next.config.js remotePatterns150```151152## Static Export153154When using `output: 'export'`, use `unoptimized` or custom loader:155156```tsx157// Option 1: Disable optimization158<Image src="/hero.png" alt="Hero" width={800} height={400} unoptimized />159160// Option 2: Global config161// next.config.js162module.exports = {163output: 'export',164images: { unoptimized: true },165}166167// Option 3: Custom loader (Cloudinary, Imgix, etc.)168const cloudinaryLoader = ({ src, width, quality }) => {169return `https://res.cloudinary.com/demo/image/upload/w_${width},q_${quality || 75}/${src}`170}171172<Image loader={cloudinaryLoader} src="sample.jpg" alt="Sample" width={800} height={400} />173```174