Skip to main content

Next.js Image & Font Optimization Strategy

Optimize Next.js images and fonts for strong Core Web Vitals: AVIF/WebP, responsive sizes, art direction, font subsetting, and zero CLS.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a web performance specialist focused on Next.js image and font optimization. Help me achieve perfect Core Web Vitals scores for a e-commerce store website.

Step 1: Audit all images in the public/images directory. Categorize each by usage: hero images, product thumbnails, avatars, icons, and decorative backgrounds. For each category, recommend optimal dimensions, formats (AVIF with WebP fallback), and quality settings. Calculate the current total image payload and set a target reduction of 60%.

Step 2: Configure the Next.js Image component with a custom loader for Vercel Image Optimization. Set up responsive image sizes using the sizes prop with breakpoints at 640px, 768px, 1024px, and 1280px. Create reusable image wrapper components for each category (HeroImage, Thumbnail, Avatar) with pre-configured dimensions and priority settings.

Step 3: Implement art direction for hero images using the picture element pattern with Next.js Image. Serve different image crops for mobile (portrait), tablet (square), and desktop (landscape) viewports. Use the blur placeholder strategy to eliminate layout shift during image loading.

Step 4: Optimize font loading by configuring next/font with 3 custom fonts. Enable font subsetting to include only Latin characters (reducing file size by approximately 70%). Set font-display to swap for body text and optional for decorative headings. Pre-load the primary body font and defer secondary fonts.

Step 5: Create a font fallback system using the adjustFontFallback option to generate CSS size-adjust, ascent-override, and descent-override values that match the custom font metrics. This eliminates Cumulative Layout Shift caused by font swapping. Test with Lighthouse CI to verify zero CLS from font loading.

Step 6: Implement an image optimization pipeline in the build process that automatically converts source images to AVIF and WebP, generates responsive sizes, and strips EXIF metadata. Set up a GitHub Action or build script that processes images in public/images before deployment.

What this prompt does

This prompt turns the AI into a Next.js performance specialist focused on the two assets that wreck Core Web Vitals most often: images and fonts. It walks through a six-step plan that audits the images in [images_directory], configures the Next.js Image component for [image_cdn], applies art direction for hero crops, tunes next/font for [font_count] fonts, builds a font-fallback metrics system, and adds a build-time conversion pipeline. The [site_type] variable frames the whole audit, so an e-commerce store gets thumbnail-heavy advice while a content site leans toward hero and typography work.

The structure works because it attacks layout shift and payload size in the order they actually cause damage. Categorizing images first means each one gets dimensions and formats that fit its job, and the [size_reduction] target gives the AI a concrete payload goal to reason against. The [placeholder_strategy] setting and the font size-adjust/ascent-override step both exist to drive Cumulative Layout Shift toward zero, which is usually the hardest Core Web Vitals metric to move by hand. Wrapping each image category into a typed component also means the optimization doesn't quietly regress as new pages and developers are added later.

When to use it

  • A Next.js site's Lighthouse or CrUX scores are dragging down search rankings or conversions
  • LCP is slow because hero images ship at the wrong size or format
  • You see visible layout shift while web fonts swap in on first paint
  • You want reusable HeroImage, Thumbnail, and Avatar wrappers so optimization survives growth
  • You're standardizing image handling before onboarding a larger team
  • You need a repeatable build step that converts and strips EXIF metadata automatically
  • You're migrating to [image_cdn] and want responsive sizes and art direction set up correctly

Example output

Expect a structured, step-by-step plan rather than a single file. You typically get an image-audit table grouped by category with recommended dimensions, formats, and quality settings, code for the custom loader and the wrapper components, a next/font configuration block with Latin subsetting and per-context font-display settings, the fallback-metrics CSS overrides that come from adjustFontFallback, and a sketch of a GitHub Action or build script that processes [images_directory] before deployment. Each step is explained so you can adapt it to your project rather than paste it blind, and the art-direction step shows distinct mobile, tablet, and desktop crops for hero images.

Pro tips

  • Set [images_directory] to the real path the AI should scan (the default public/images) so the audit reflects your actual assets
  • Pick [image_cdn] to match your host; the loader code changes between Vercel's built-in optimizer and a third-party CDN
  • Keep [placeholder_strategy] on blur for hero and product shots, but switch to a solid color for icons where a blur is wasted
  • Be honest about [font_count] — every extra font is more bytes and more potential CLS, so trim before you optimize
  • Treat [size_reduction] as a target to verify with [testing_tool], not a guarantee; real savings depend on your source images
  • If CLS persists after images, the font adjustFontFallback metrics step is almost always the missing fix

Frequently Asked Questions

Does this prompt actually generate the optimized images, or just the configuration?
It generates the strategy, configuration, and build-pipeline code, not the binary image files themselves. The AI recommends dimensions, formats, and a conversion script for `[images_directory]`, but you still run that pipeline to produce the AVIF and WebP outputs during your build or deploy.
Will following this guarantee a perfect Lighthouse score?
No tool can promise a perfect score because it depends on your hosting, third-party scripts, and source assets. The prompt targets the image and font causes of poor Core Web Vitals, and you verify the real result with `[testing_tool]` rather than trusting a fixed number.
How does the font fallback step reduce layout shift?
It uses `next/font`'s `adjustFontFallback` to generate `size-adjust`, `ascent-override`, and `descent-override` values that make the fallback font occupy nearly the same space as your custom font. When the real font swaps in, text barely moves, which is what keeps Cumulative Layout Shift near zero.
Can I use this with an image CDN other than Vercel?
Yes. Set `[image_cdn]` to your provider and the prompt configures a custom loader for it. The responsive `sizes` breakpoints and wrapper components stay the same; only the loader and URL-building logic differ between CDNs.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in React & Next.js Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support