Capítulo 219 de 456
Configure next.config.js images.loader: 'custom' with a loaderFile to route next/image optimization through a third-party image CDN instead of the built-in Image Optimization API.
images.loader: 'custom': opts out of the built-in optimizer.images.loaderFile: path (relative to project root) to a file exporting a default function ({ src, width, quality }) => string (the resolved image URL).loader prop: alternative to global config — pass the loader function directly to a next/image instance.'use client' requirement: the loader file needs Client Components to serialize the provided function.module.exports = {
images: {
loader: 'custom',
loaderFile: './my/image/loader.js',
},
}
'use client'
export default function myImageLoader({ src, width, quality }) {
return `https://example.com/${src}?w=${width}&q=${quality || 75}`
}
// Cloudflare
export default function cloudflareLoader({ src, width, quality }) {
const params = [`width=${width}`, `quality=${quality || 75}`, 'format=auto']
return `https://example.com/cdn-cgi/image/${params.join(',')}/${src}`
}
// Supabase
export default function supabaseLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
url.searchParams.set('width', width.toString())
url.searchParams.set('quality', (quality || 75).toString())
return url.href
}
| Provedor com exemplo pronto na doc |
|---|
| Akamai, AWS CloudFront, Cloudinary, Cloudflare, Contentful, Fastly, Gumlet, ImageEngine, Imgix, PixelBin, Sanity, Sirv, Supabase, Thumbor, ImageKit.io, Nitrogen AIO |
'use client' no arquivo de loader: a função precisa ser serializável para Client Components.loaderFile aceita caminho absoluto fora do projeto: deve ser relativo à raiz do app Next.js.w/width, q/quality) — copie o exemplo do provedor específico em vez de generalizar.loader direto no componente <Image> em vez de mexer no config.