Capítulo 289 de 456
Use next/image (Pages Router) instead of <img> to get automatic size optimization, layout-shift prevention, lazy loading and support for local or remote sources.
public/ and reference from /, or import the file directly to get automatic width/height/blurDataURL.await import(...) a file path with a static prefix to still get automatic dimensions when the filename is dynamic.src; you must manually supply width, height, and optional blurDataURL (or use fill), since Next.js can't inspect remote files at build time.next.config.js images.remotePatterns allowlist required for any remote src, to prevent malicious usage.import Image from 'next/image'
async function PostImage({ imageFilename, alt }: { imageFilename: string; alt: string }) {
const { default: image } = await import(
`../content/blog/images/${imageFilename}`
)
// image contains width, height, and blurDataURL
return <Image src={image} alt={alt} />
}
const config: NextConfig = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 's3.amazonaws.com', port: '', pathname: '/my-bucket/**', search: '' },
],
},
}
remotePatterns genérico demais: hostname/pathname amplos abrem brecha pra uso malicioso do otimizador de imagem.width/height manualmente.width+height (ou fill) e um remotePatterns explícito no config.<Image> já resolve lazy loading nativo e blur-up sem configuração extra.next/font.