Loading...

Three ways to disable Image Optimization in Vercel

question nextjs vercel
Ram Patra Published on March 1, 2025
Next.js 14+

If you’re using the free tier of Vercel, like I do for some of my small side projects, you will get the 402 Payment Required error eventually when loading images. This is because you have hit the Image Optimization limit of the free plan. The solution to this is to disable Vercel’s Image Optimization, or to host the images elsewhere, or to upgrade to their Pro plan.

Here I will be talking about the three ways to disable Vercel’s image optimization to prevent the 402 Payment Required error:

1. Disable Image Optimization Globally

If you want to disable image optimization across your entire Next.js app, update your next.config.mjs:

export default {
  images: {
    loader: 'default', // Prevents Vercel's optimization
    unoptimized: true, // Disables all image optimizations globally
  },
};

Or, if you’re using next.config.ts, update as per below snippet:

import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  images: {
    unoptimized: true, // Disables Next.js image optimization
  },
};

export default nextConfig;

This tells Next.js to serve images as they are without going through Vercel’s optimization pipeline.

2. Disable Optimization per Image (Using unoptimized in <Image>)

If you only want to disable optimization for specific images, set unoptimized={true} in the <Image> component:

import Image from "next/image";

export default function Example() {
  return (
    <Image 
      src="https://your-external-image-url.com/example.jpg" 
      width={500} 
      height={300} 
      unoptimized 
    />
  );
}

This bypasses Vercel’s image processing but still allows you to use the <Image> component.

3. Use a Custom Image Loader (For External Images)

If you host images externally and want to keep the <Image> component’s benefits (like lazy loading), use a custom loader:

const customLoader = ({ src }) => src;

export default function Example() {
  return (
    <Image 
      loader={customLoader} 
      src="https://your-external-image-url.com/example.jpg" 
      width={500} 
      height={300} 
    />
  );
}

This ensures Next.js loads the image directly from the URL instead of routing it through Vercel’s optimization.

Which Method Should You Use?

Method 1 (Global Disable) → Best if all images should bypass optimization.
Method 2 (Per Image Disable) → Best if only some images need to be unoptimized.
Method 3 (Custom Loader) → Best if using external image hosting CDN.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

KeyScreen

Show keypresses on your screen.

ToDoBar

Your to-dos on your menu bar.

SimpleFill

Fill forms using your right-click menu.

IconSim

Preview your Mac app icons.

Ram Patra Published on March 1, 2025
Image placeholder

Keep reading

If this article was helpful, others might be too

question nextjs cloudflare March 19, 2025 How to run a website built with Next.js 15 on Cloudflare Pages?

If you simply connect a website’s code on GitHub with Cloudflare Pages that’s built using Next.js 15+, it may not build successfully out of the box. If this is the case with you, make sure to do the following.

question nextjs react July 11, 2024 How to set environment variable in a Nextjs App and when to use NEXT_PUBLIC prefix?

Setting environment variables in a Next.js app is straightforward. Next.js supports loading environment variables from .env files. Here’s a step-by-step guide on how to set and use environment variables in your Next.js application:

question typescript react September 28, 2024 How to add a custom element to a Next.js/Typescript project?

Let’s say I have a custom element setapp-badge that I want to use in my tsx files. If I go ahead and use it, the compiler will throw an error and Next.js will fail to build. It seems the problem might be a combination of how Next.js, TypeScript, and custom elements work together. Therefore, let’s try an approach that avoids the namespace/module issues while ensuring custom elements are recognized in a Next.js/TypeScript project.

Like my work?

Please, feel free to reach out. I would be more than happy to chat.