install
source · Clone the upstream repo
git clone https://github.com/Intense-Visions/harness-engineering
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/Intense-Visions/harness-engineering "$T" && mkdir -p ~/.claude/skills && cp -r "$T/agents/skills/codex/next-image-optimization" ~/.claude/skills/intense-visions-harness-engineering-next-image-optimization-874dbe && rm -rf "$T"
manifest:
agents/skills/codex/next-image-optimization/SKILL.mdsource content
Next.js Image Optimization
Serve optimized, correctly sized images automatically with next/image
When to Use
- Displaying images in any Next.js page or component
- Improving Core Web Vitals (LCP, CLS) by preventing layout shift and lazy loading images
- Serving modern formats (WebP, AVIF) without manual conversion
- Building responsive images that adapt to viewport and container size
- Loading above-the-fold images with priority to improve LCP score
Instructions
- Import
fromImage
instead of the HTMLnext/image
tag for all content images.<img> - Always provide
andwidth
props for images with known dimensions to prevent layout shift (CLS).height - Use
prop with a positioned parent (fill
) for images that should fill their container — omitposition: relative
andwidth
when usingheight
.fill - Add
to the first image visible in the viewport (the LCP element) to disable lazy loading and preload it.priority - Use the
prop to tell the browser how wide the image will be at each breakpoint — this enables accuratesizes
selection.srcset - Add remote domains to
inimages.remotePatterns
before using external image URLs.next.config.ts - Use
withplaceholder="blur"
for a low-quality preview while the image loads.blurDataURL - For static imports, Next.js generates
automatically —blurDataURL
and pass the imported object.import logo from './logo.png'
// next.config.ts const config = { images: { remotePatterns: [ { protocol: 'https', hostname: 'cdn.example.com', pathname: '/images/**' }, ], }, }; // app/products/[id]/page.tsx import Image from 'next/image'; import heroImage from '@/public/hero.jpg'; // static import — dimensions known export default function ProductPage() { return ( <div> {/* Static import — auto blur placeholder, auto width/height */} <Image src={heroImage} alt="Hero" priority placeholder="blur" /> {/* Remote image — fill mode for responsive container */} <div className="relative h-64 w-full"> <Image src="https://cdn.example.com/images/product.jpg" alt="Product" fill sizes="(max-width: 768px) 100vw, 50vw" className="object-cover" /> </div> </div> ); }
Details
next/image wraps the HTML <img> element with automatic size optimization, format conversion, and lazy loading. The optimization pipeline runs server-side: Next.js resizes and converts images to WebP or AVIF on first request and caches the result.
attribute importance: Without sizes
sizes, the browser assumes the image is 100vw wide and downloads a larger source than necessary. Providing accurate sizes (e.g., "(max-width: 640px) 100vw, 640px") reduces bandwidth significantly.
fill mode: Use
fill when the image dimensions are unknown or when the image should fill a CSS-controlled container. The parent must have position: relative (or absolute/fixed). Use className="object-cover" or className="object-contain" on the Image to control aspect ratio.
priority vs lazy: All images are lazy-loaded by default (loaded when they enter the viewport). The
priority prop disables lazy loading and adds a <link rel="preload"> — use it for the LCP image only. Adding priority to multiple images defeats its purpose.
Layout shift (CLS): Providing
width and height lets the browser reserve space before the image loads, preventing layout shift. The fill prop avoids this by letting CSS control the container dimensions.
Remote patterns: The
remotePatterns config (replacing the deprecated domains) supports wildcards and pathnames. Misconfigured patterns cause 400 errors on image optimization requests.
Source
https://nextjs.org/docs/app/api-reference/components/image
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.