v(i)b(e)c(o)d(e)4u.com
Назад на форум vbcd4u.com
🔥 Нейросети2026-11-17T10:13:00.000Z▲ 0 upvotes

next/image without errors: AVIF and WebP, sizes and priority attribute for LCP

@murdasoftРедакция vbcd4u · MurdaSoft
Открыть все обсуждения →

What next/image actually does

The Image component does not compress the image during assembly. It generates <img> with attributes srcset and sizes, where each option leads to /_next/image?url=...&w=...&q=.... The optimizer on the server, upon the first request, resizes the source to the required width, recodes it into a format that the browser supports, and caches the result.

From this, three things follow that determine whether a page will be fast: what width the browser will choose (sizes attribute), in what format (formats setting) and when loading will start (priority and lazy loading).

Formats: AVIF and WebP

By default, the optimizer gives WebP. AVIF is enabled in the config:

// next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  images: {
    formats: ["image/avif", "image/webp"],
    remotePatterns: [{ protocol: "https", hostname: "cdn.example.com", pathname: "/uploads/**" }],
    minimumCacheTTL: 2678400, // 31 день, если исходники не меняются по тому же URL
  },
};
export default nextConfig;

The order is important: the browser that accepts AVIF will receive it, the others will receive WebP. AVIF, with the same visual quality, is usually smaller, but takes much longer to encode. The first request of each option will be slower, then the cache works. On sites with thousands of unique images and a small cache, this can become a problem - then it makes sense to stay on WebP.

remotePatterns limits which external images the optimizer will agree to process. A template that is too wide (any host) turns your server into a free resizer for other people's pictures.

sizes: the most common mistake

Without sizes for a fixed-width image, Next.js generates 1x and 2x variants. With fill or adaptive layout you need sizes, otherwise the browser thinks that the image occupies the entire width of the window, and on a wide monitor it will load an option of 1920–3840 pixels for a card with a width of 300.

Cards in a grid: on a phone in one column, on a tablet in two, on a desktop in three with a limit on the width of the container:

import Image from "next/image";

export function PostCard({ post }: { post: { cover: string; title: string } }) {
  return (
    <article className="card">
      <div className="relative aspect-[16/9]">
        <Image
          src={post.cover}
          alt=""
          fill
          sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 380px"
          className="object-cover"
        />
      </div>
      <h3>{post.title}</h3>
    </article>
  );
}

sizes describes the actual rendering width of the image in CSS pixels, not the width of the file. The last value without a condition is for all other cases. The browser takes into account screen density itself.

alt="" is here deliberately: the cover is decorative, the title is next to it. Meaningful images require meaningful text.

fill has two requirements: a parent with position: relative (or absolute/fixed) and a given size. Otherwise, the picture will stretch unpredictably or collapse to zero.

LCP picture and priority

Images in next/image are lazily loaded by default (loading="lazy"). For an image that is visible on the first screen and is an LCP element, this is harmful: the browser will start loading it only after the page is laid out.

import hero from "@/public/hero.jpg";

<Image
  src={hero}
  alt="Панель аналитики с графиком продаж"
  priority
  sizes="100vw"
  placeholder="blur"
  className="h-auto w-full"
/>

priority disables lazy loading, sets fetchpriority="high" and adds preloading. Give it to one or two images of the first screen, not all: if everyone is a priority, no one is a priority. In Next.js 16, instead of priority, the preload property was introduced; in 15, priority is used.

Static import (import hero from ...) gives Next.js the file sizes and embeds a blurry preview for placeholder="blur". For deleted images, you need to specify the dimensions yourself via width and height, otherwise there will be a layout shift (CLS).

How to check what works

  • Which file is actually downloaded. In the browser console: document.querySelector('img[alt^="Панель"]').currentSrc - the width w= is visible in the URL. In Network, filter Img and check the Type column: avif or webp.
  • Which LCP element. DevTools → Performance → boot entry → LCP marker will show the element. If it's a picture without priority - that's the problem.
  • Lighthouse warns "Properly size images" if the loaded version is noticeably larger than the displayed one. This is almost always the wrong sizes.

Common mistakes

  • sizes="100vw" on small cards “just in case.”
  • priority in the image below the first screen - it takes away the channel from the real LCP.
  • unoptimized globally due to a bug in one image. It's better to deal with remotePatterns.
  • Huge sources (6000 pixels, 10 MB) in public. The optimizer will do the job, but the first request of each option will be slow and will load the server.
  • Self-hosted in a container without write rights to .next/cache - optimized options are not cached and are recalculated for each request.

When next/image is not needed

It is more profitable to insert SVG icons and logos as SVG: they do not need raster optimization. If images are already processed by a CDN resizing by URL (Cloudinary, imgproxy), connect it via loader, rather than having two optimizers. And on Vercel, keep in mind that image optimization is included in the tariff limits.

Обсуждение (0)

Ещё на vbcd4u.com