Le Do Nghiem

Le Do Nghiem

AI Engineer

About meBooksSnippetsBlog

© 2026 Le Do Nghiem. All rights reserved.

Contact |

Back to Blog

Optimizing Next.js Apps with AI-Powered Image Processing

Le Do Nghiem
Le Do NghiemAI Engineer
2025-09-07 3 min read
Share

Start with the basics

Every "optimize images with AI" tutorial skips the boring part: most image pain is sizing, format, and layout shift — not missing a neural network.

I still use AI-powered pipelines (Cloudinary, imgix, similar) when they save ops time: auto format, quality, smart crop. But I start with Next.js Image and Core Web Vitals. AI is layer two.

If you are new to App Router caching and server boundaries, What's New in Next.js 15 pairs well with this.


LCP and CLS in plain terms

LCP (Largest Contentful Paint): How fast the biggest visible thing — often a hero image — shows up.

CLS (Cumulative Layout Shift): How much the page jumps while loading. Images without width/height are a classic cause.

What I fix first:

  • Correct dimensions on every above-the-fold image
  • Modern formats (WebP/AVIF) via CDN or next/image
  • Lazy load below the fold

Red flag: Running every JPEG through an AI model while serving 4000px originals. Fix bytes and dimensions first.


Next.js Image — the baseline

import Image from "next/image";

export default function Hero() {
  return (
    <Image
      src="/hero.jpg"
      alt="Product screenshot"
      width={1200}
      height={630}
      priority
      sizes="(max-width: 768px) 100vw, 1200px"
    />
  );
}

priority — for LCP candidate images only. Not every img on the page.

sizes — tells the browser which responsive width to request. Skipping this wastes bandwidth.


When AI/CDN optimization helps

What: Services like Cloudinary fetch or transform images on the fly — auto quality, format, resize.

import Image from "next/image";

export default function OptimizedImage({ src, alt, width, height }) {
  const optimizedSrc = `https://res.cloudinary.com/your-cloud-name/image/fetch/w_${width},q_auto,f_auto/${encodeURIComponent(src)}`;

  return (
    <Image src={optimizedSrc} alt={alt} width={width} height={height} />
  );
}

Why I use it:

  • User-uploaded images with unknown sizes
  • Art direction crops without maintaining ten manual variants
  • One URL pattern for the whole site

When it is overkill:

  • A handful of static assets in /public — build-time optimization is enough
  • Strict data residency rules — third-party image proxies need legal review

AI features beyond resize

Background removal, smart crop, object detection — useful for marketplaces and CMS-heavy sites. I ask:

  1. Does this run on upload (batch) or every request (cost)?
  2. Can we cache the result?
  3. What happens when the AI service is down?

Red flag: Synchronous AI transform on the critical path with no fallback image.


Checklist before adding AI

  • Lighthouse image audit — oversized images flagged?
  • width / height set — CLS near zero?
  • Hero uses priority, rest lazy
  • CDN/cache headers documented Add remote image domains in next.config when using external URLs — otherwise prod builds fail while dev looked fine.
// next.config.js
module.exports = {
  images: {
    remotePatterns: [
      { protocol: "https", hostname: "res.cloudinary.com" },
    ],
  },
};

Real-site image mistakes

  • priority on everything — defeated the purpose; hurt mobile LCP competitors.
  • Remote patterns not configured in next.config — broken images in prod only.
  • AI quality auto on tiny icons — wasted transforms; exclude small static assets.

The practical order

Run Lighthouse on your slowest page. Fix dimensions and formats. Then add Cloudinary-or-similar if uploads or dynamic crops are real pain.

AI image tooling is a multiplier on good basics, not a substitute. Get LCP and CLS green first; let AI handle the long tail of messy user content.

On this page

  • Start with the basics
  • LCP and CLS in plain terms
  • Next.js Image — the baseline
  • When AI/CDN optimization helps
  • AI features beyond resize
  • Checklist before adding AI
  • Real-site image mistakes
  • The practical order
Share
Previous Post

Vue.js 3: Building Reactive UIs

Next Post

React Performance Optimization Guide