Next.js fix

Before and after

Broken — before
// 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>
  );
}
Fixed — after
// 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>
  );
}
Live preview

Fixed vs. broken example

Passes image-alt
Accessibility tree: role="img", accessible name="Cobalt blue stoneware ceramic pour-over mug with matte handle"

Always provide meaningful alt text for informative photos and explicitly set alt="" (empty string) for purely decorative flourishes so screen readers skip them cleanly.

Automated test

Playwright + axe-core check

tests/a11y-image-alt.spec.ts
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.

Screen readers

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)

Broken

"graphic, cdn_shop_files_IMG_8842_v1731942.webp" (or skipped silently depending on verbosity)


Fixed

"Cobalt blue stoneware ceramic pour-over mug with matte handle, graphic"

VoiceOver (macOS / iOS)

Broken

"IMG_8842_v1731942.webp, image"


Fixed

"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?
If your CMS returns null or undefined at runtime (for example via an untyped JSON fetch or dangerouslySetInnerHTML), React omits the alt attribute from the rendered DOM even if TypeScript thought it was a string.
How do I mark a decorative Next.js <Image> so screen readers skip it?
Pass an explicit empty string alt="" (never omit alt or pass undefined). Next.js renders alt="" in the DOM, which signals to NVDA, VoiceOver, and axe-core that the graphic is decorative.
Does Next.js ESLint jsx-a11y catch CMS-injected <img> tags?
Static ESLint rules only inspect JSX source files—they cannot inspect HTML strings coming from headless CMS rich-text fields. Running Playwright + axe-core in CI verifies the live rendered DOM.