Capítulo 13 de 456
O componente <Image> (next/image) estende <img> com otimização de tamanho, estabilidade visual (evita layout shift), lazy loading nativo e redimensionamento sob demanda, inclusive para imagens remotas.
<Image>: componente de next/image; src pode ser local ou remoto.public/): referenciadas a partir da base URL /; exigem width/height explícitos quando não importadas estaticamente.src é um import estático, Next.js infere automaticamente width, height e blurDataURL, permitindo placeholder="blur".await import(...) com prefixo estático no path também obtém width/height/blurDataURL automáticos; o path deve ter prefixo estático (ex.: ../content/blog/images/) para limitar o bundling e impedir que input externo escape do diretório.src como URL string; width/height (ou fill) devem ser fornecidos manualmente porque Next.js não acessa arquivos remotos no build.remotePatterns: configuração obrigatória em next.config.js (images.remotePatterns) para permitir hosts remotos específicos, prevenindo uso malicioso.fill: prop alternativa a width/height para a imagem preencher o elemento pai.import Image from 'next/image'
export default function Page() {
return (
<Image src="/profile.png" alt="Picture of the author" width={500} height={500} />
)
}
public/ com dimensões explícitas.import Image from 'next/image'
import ProfileImage from './profile.png'
export default function Page() {
return <Image src={ProfileImage} alt="Picture of the author" />
}
width/height/blurDataURL manuais.import type { NextConfig } from 'next'
const config: NextConfig = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 's3.amazonaws.com', port: '', pathname: '/my-bucket/**', search: '' },
],
},
}
export default config
remotePatterns: <Image> recusa/bloqueia por padrão; sempre declarar o padrão mais específico possível (protocolo, host, path) para evitar uso malicioso.width/height/fill em imagem remota: gera layout shift, já que Next.js não pode inferir a proporção sem acessar o arquivo no build.width, height e blurDataURL automáticos de graça.width/height (ou fill) e remotePatterns em next.config.js são obrigatórios.remotePatterns deve ser o mais específico possível (não usar wildcard de host amplo) por segurança.