type RemotePattern = { hostname: string; pathname?: string; port?: string; protocol?: 'http' | 'https' } export type ImagesConfig = { formats: ('image/avif' | 'image/webp')[] minimumCacheTTL: number remotePatterns: RemotePattern[] } /** * Defaults for `next.config.ts → images`, to merge or override: * - AVIF first, WebP fallback — ~20–50 % smaller than JPEG at the same quality; * - remotePatterns for the media domain (R2_PUBLIC_URL) so next/image may * optimize R2 files — otherwise it refuses them; * - optimized images cached for 31 days (uploads get new names when replaced). * * import { buildImagesConfig } from '@intecion/ipal-kit' * export default withPayload({ images: { ...buildImagesConfig(), /* your overrides *\/ } }) */ export function buildImagesConfig({ extraHosts = [], publicUrl = process.env.R2_PUBLIC_URL, }: { extraHosts?: string[]; publicUrl?: string } = {}): ImagesConfig { const remotePatterns: RemotePattern[] = [] if (publicUrl && /^https?:\/\//i.test(publicUrl)) { const u = new URL(publicUrl) remotePatterns.push({ hostname: u.hostname, protocol: u.protocol.replace(':', '') as 'http' | 'https', ...(u.port ? { port: u.port } : {}), pathname: `${u.pathname.replace(/\/$/, '')}/**`, }) } for (const host of extraHosts) {remotePatterns.push({ hostname: host, protocol: 'https' })} return { formats: ['image/avif', 'image/webp'], minimumCacheTTL: 60 * 60 * 24 * 31, remotePatterns } }