Headless helpers for the project's own <Media> (look stays in the project): - mediaImageProps: dimensions from the doc (no CLS), lazy by default, LCP image gets preload + fetchPriority=high + eager (Next 16 replaced 'priority'), focal point → object-position, blur placeholder + dominant colour. - MEDIA_SIZES suggestions for 'sizes' (any string still works). - videoProps: 'background' (muted loop, respects reduced motion) or 'player' (controls, preload=none, poster). - buildMediaPlaceholderHook/Field: tiny WebP blur + dominant colour at upload via the configured sharp (no new dependency). - buildImagesConfig: AVIF/WebP, R2 remotePatterns, 31-day cache. - <YouTubeEmbed> (/client): poster + button, youtube-nocookie iframe on click. - buildCsp youtube allows the i.ytimg.com poster. kod-sekcji Media template uses the helpers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
36 lines
1.5 KiB
TypeScript
36 lines
1.5 KiB
TypeScript
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 }
|
||
}
|