Cursor rule for next/image with alt text, explicit size or fill plus sizes, eager or high-priority LCP images instead of deprecated priority, and scoped remotePatterns.
.cursor/rules/next-js-image-optimization.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rule for next/image with alt text, explicit size or fill plus sizes, eager or high-priority LCP images instead of deprecated priority, and scoped remotePatterns."
globs: "**/*"
alwaysApply: false
---
# Next.js Image Optimization
Applies to: images rendered with `next/image` and the `images` block of `next.config.ts`.
Version baseline: checked against the Next.js 16.3 `<Image>` and `images` config reference. Read `package.json` first. `priority` is deprecated from Next.js 16 (use the options below); on Next.js 15 and earlier `priority` is still the documented prop.
## Use `next/image` for content images
- Use `<Image>` from `next/image` for raster content images so the optimizer can resize, convert format and generate `srcset`. Keep plain `<img>` or CSS backgrounds only for decorative images or when a third-party loader is required, and say why in the code.
- `alt` is required. Describe the image's content or purpose; use `alt=""` only for purely decorative images.
- SVGs are served unoptimized automatically when `src` ends in `.svg`.
## Reserve space to avoid layout shift
- Static imports (`import hero from './hero.jpg'`) supply `width`, `height` and `blurDataURL` automatically.
- Remote or dynamic `src` needs explicit `width` and `height` with the intrinsic aspect ratio, or `fill` inside a parent with `position: relative` (or absolute/fixed) and a defined size.
- With `fill` or CSS-responsive images, set `sizes` to the rendered width at each breakpoint. Without `sizes` the browser assumes `100vw` and may download a much larger file.
```tsx
// app/components/product-card.tsx
import Image from 'next/image'
export function ProductCard({ name, imageUrl }: { name: string; imageUrl: string }) {
return (
<article>
<div className="relative aspect-square w-full">
<Image
src={imageUrl}
alt={name}
fill
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
className="object-cover"
/>
</div>
<h2>{name}</h2>
</article>
)
}
```
## Loading strategy
- Images are lazy-loaded by default; leave below-the-fold images alone.
- For the one image that is the Largest Contentful Paint element (usually the hero), use `loading="eager"` or `fetchPriority="high"`. Use `preload` only when the image must start loading from `<head>` before it is discovered, and never combine `preload` with `loading` or `fetchPriority`.
- Do not mark many images as eager or high priority; that removes the benefit.
```tsx
// app/page.tsx
import Image from 'next/image'
import hero from './hero.jpg'
export default function Home() {
return <Image src={hero} alt="Team reviewing a dashboard" fetchPriority="high" placeholder="blur" />
}
```
## Placeholders
- `placeholder="blur"` works automatically for static imports of jpg, png, webp or avif. For remote images supply a small `blurDataURL` (around 10px) yourself, or skip the placeholder.
## Configuration
- Allow remote hosts with `images.remotePatterns`, scoped as narrowly as possible (protocol, hostname, pathname). `images.domains` is deprecated; migrate it.
- Next.js 16 requires the `qualities` allowlist and defaults it to `[75]`. If code passes `quality={90}`, add `90` to `images.qualities`.
```ts
// next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'cdn.example.com', pathname: '/products/**' },
],
qualities: [75, 90],
},
}
export default nextConfig
```
## Verify before finishing
- `npx tsc --noEmit` and `npx next build` pass. An unconfigured remote host only errors when the image is requested, so also load each changed page in `next dev`.
- `grep -rn "priority" app components` shows no new `priority` props on Next.js 16.
- In browser dev tools, the LCP image has `fetchpriority="high"` or `loading="eager"`, other images have `loading="lazy"`, and the downloaded size matches the rendered size.
- Run Lighthouse or the browser Performance panel on the changed page and check that layout shift from images is zero.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
framework
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Migrating a product grid and hero image in a Next.js 16 app that still uses priority and images.domains.
Hero uses <Image src={hero} priority />, product cards use <img src={p.imageUrl}> from https://cdn.example.com/products/..., and next.config.ts has images.domains: ['cdn.example.com']. Cards are 1 column on mobile, 2 on tablet, 3 on desktop. Update for Next.js 16.Illustrative; not a recorded model run. Hero changes from priority to fetchPriority="high" (or loading="eager") with alt text; cards use <Image fill sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"> inside a relative aspect-ratio wrapper with alt from the product name; next.config.ts replaces domains with remotePatterns limited to https, cdn.example.com and /products/**; qualities includes every quality value used.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/next-js-image-optimization.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "Next.js Image Optimization: next/image Sizing, Loading Priority and remotePatterns project conventions"
globs: "**/*"
alwaysApply: false
---
# Next.js Image Optimization
Applies to: images rendered with `next/image` and the `images` block of `next.config.ts`.
Version baseline: checked against the Next.js 16.3 `<Image>` and `images` config reference. Read `package.json` first. `priority` is deprecated from Next.js 16 (use the options below); on Next.js 15 and earlier `priority` is still the documented prop.
## Use `next/image` for content images
- Use `<Image>` from `next/image` for raster content images so the optimizer can resize, convert format and generate `srcset`. Keep plain `<img>` or CSS backgrounds only for decorative images or when a third-party loader is required, and say why in the code.
- `alt` is required. Describe the image's content or purpose; use `alt=""` only for purely decorative images.
- SVGs are served unoptimized automatically when `src` ends in `.svg`.
## Reserve space to avoid layout shift
- Static imports (`import hero from './hero.jpg'`) supply `width`, `height` and `blurDataURL` automatically.
- Remote or dynamic `src` needs explicit `width` and `height` with the intrinsic aspect ratio, or `fill` inside a parent with `position: relative` (or absolute/fixed) and a defined size.
- With `fill` or CSS-responsive images, set `sizes` to the rendered width at each breakpoint. Without `sizes` the browser assumes `100vw` and may download a much larger file.
```tsx
// app/components/product-card.tsx
import Image from 'next/image'
export function ProductCard({ name, imageUrl }: { name: string; imageUrl: string }) {
return (
<article>
<div className="relative aspect-square w-full">
<Image
src={imageUrl}
alt={name}
fill
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
className="object-cover"
/>
</div>
<h2>{name}</h2>
</article>
)
}
```
## Loading strategy
- Images are lazy-loaded by default; leave below-the-fold images alone.
- For the one image that is the Largest Contentful Paint element (usually the hero), use `loading="eager"` or `fetchPriority="high"`. Use `preload` only when the image must start loading from `<head>` before it is discovered, and never combine `preload` with `loading` or `fetchPriority`.
- Do not mark many images as eager or high priority; that removes the benefit.
```tsx
// app/page.tsx
import Image from 'next/image'
import hero from './hero.jpg'
export default function Home() {
return <Image src={hero} alt="Team reviewing a dashboard" fetchPriority="high" placeholder="blur" />
}
```
## Placeholders
- `placeholder="blur"` works automatically for static imports of jpg, png, webp or avif. For remote images supply a small `blurDataURL` (around 10px) yourself, or skip the placeholder.
## Configuration
- Allow remote hosts with `images.remotePatterns`, scoped as narrowly as possible (protocol, hostname, pathname). `images.domains` is deprecated; migrate it.
- Next.js 16 requires the `qualities` allowlist and defaults it to `[75]`. If code passes `quality={90}`, add `90` to `images.qualities`.
```ts
// next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'cdn.example.com', pathname: '/products/**' },
],
qualities: [75, 90],
},
}
export default nextConfig
```
## Verify before finishing
- `npx tsc --noEmit` and `npx next build` pass. An unconfigured remote host only errors when the image is requested, so also load each changed page in `next dev`.
- `grep -rn "priority" app components` shows no new `priority` props on Next.js 16.
- In browser dev tools, the LCP image has `fetchpriority="high"` or `loading="eager"`, other images have `loading="lazy"`, and the downloaded size matches the rendered size.
- Run Lighthouse or the browser Performance panel on the changed page and check that layout shift from images is zero.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.