Fix image-alt in Next.js
Although Next.js next/image requires an alt prop in TypeScript, image-alt violations still slip into Next.js apps through raw <img> tags in Markdown/MDX renderers, third-party CMS HTML payloads, and dynamic props cast with alt={post.coverAlt as string} that evaluate to undefined at runtime.
Before and after
// app/products/[slug]/page.tsx (Broken: raw <img> and unchecked nullable CMS alt)
export function ProductHero({ imageUrl, altText }: { imageUrl: string; altText?: string }) {
return (
<figure className="product-hero">
<img src={imageUrl} alt={altText} width={800} height={600} />
</figure>
);
}// app/products/[slug]/page.tsx (Fixed: next/image with guaranteed non-empty fallback)
import Image from "next/image";
interface ProductHeroProps {
imageUrl: string;
altText?: string | null;
productTitle: string;
}
export function ProductHero({ imageUrl, altText, productTitle }: ProductHeroProps) {
const safeAlt = altText?.trim() || `${productTitle} product photo`;
return (
<figure className="product-hero">
<Image
src={imageUrl}
alt={safeAlt}
width={800}
height={600}
sizes="(max-width: 768px) 100vw, 800px"
priority
/>
</figure>
);
}Fixed vs. broken example
Always provide meaningful alt text for informative photos and explicitly set alt="" (empty string) for purely decorative flourishes so screen readers skip them cleanly.
Playwright + axe-core check
import { test, expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
test("Next.js product detail images pass axe-core image-alt", async ({ page }) => {
await page.goto("/products/cobalt-ceramic-mug");
const results = await new AxeBuilder({ page })
.withRules(["image-alt"])
.analyze();
expect(results.violations).toEqual([]);
});Template deduplication tip
A single unguarded raw <img> inside an MDX component or dynamic app/blog/[slug]/page.tsx hero affects every published post. A11yDrift collapses all /blog/* image-alt violations into one route-template cluster.
What NVDA and VoiceOver announce
When an informative image lacks an alt attribute, screen readers either skip it silently or read out raw CDN filenames and hashes, blocking blind shoppers and readers from understanding products, charts, and editorial photos.
NVDA (Windows)
"graphic, cdn_shop_files_IMG_8842_v1731942.webp" (or skipped silently depending on verbosity)
"Cobalt blue stoneware ceramic pour-over mug with matte handle, graphic"
VoiceOver (macOS / iOS)
"IMG_8842_v1731942.webp, image"
"Cobalt blue stoneware ceramic pour-over mug with matte handle, image"
Common questions
Why did axe-core flag my Next.js <Image> when TypeScript compiled cleanly?
How do I mark a decorative Next.js <Image> so screen readers skip it?
Does Next.js ESLint jsx-a11y catch CMS-injected <img> tags?
Test a URL for image-alt
Enter any public URL for a free WCAG 2.2 AA check—no account or credit card required.
Shipping Next.js weekly? Block new image-alt regressions in PRs with our baseline-diff GitHub Action PR gate (≤20 URLs/PR).