Media performance tools: image/video props, upload placeholders, images preset, YouTube facade

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]>
This commit is contained in:
2026-10-16 03:19:38 +02:00
co-authored by Claude Opus 5.5
parent f7466cf77f
commit 55fc27a13f
13 changed files with 414 additions and 25 deletions
+55
View File
@@ -0,0 +1,55 @@
'use client'
import { useState } from 'react'
type YouTubeEmbedProps = {
/** Classes per element; nothing is styled by default (keep the aspect ratio in your CSS). */
classNames?: Partial<Record<'button' | 'frame' | 'note' | 'poster' | 'root', string>>
/** Short text under the play button, from the panel — e.g. that playing loads YouTube. */
note?: string
/** Accessible name of the play button, from the panel ("Play: <title>"). */
playLabel: string
/**
* Poster URL — your own upload (no request to Google until play) or omit to
* use YouTube's thumbnail (i.ytimg.com, no cookies).
*/
poster?: string
/** Video title, from the panel — iframe title for screen readers. */
title: string
/** YouTube video id (the part after watch?v=). */
videoId: string
}
/**
* Lightweight YouTube: a poster and a play button until the visitor clicks —
* no ~1 MB of player JavaScript and no YouTube cookies on page load (better
* LCP, no consent problem before interaction). On click it loads the
* privacy-enhanced youtube-nocookie.com player with autoplay. Unstyled.
*/
export function YouTubeEmbed({ classNames = {}, note, playLabel, poster, title, videoId }: YouTubeEmbedProps) {
const [playing, setPlaying] = useState(false)
const id = /^[\w-]{6,20}$/.test(videoId) ? videoId : ''
if (!id) {return null}
if (playing) {
return (
<iframe
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
className={classNames.frame}
// What the YouTube player needs, nothing more (no top navigation, no forms).
sandbox="allow-scripts allow-same-origin allow-presentation allow-popups allow-popups-to-escape-sandbox"
src={`https://www.youtube-nocookie.com/embed/${id}?autoplay=1`}
title={title}
/>
)
}
return (
<div className={classNames.root}>
<img alt="" className={classNames.poster} decoding="async" loading="lazy" src={poster ?? `https://i.ytimg.com/vi/${id}/hqdefault.jpg`} />
<button aria-label={playLabel} className={classNames.button} onClick={() => setPlaying(true)} type="button">
▶
</button>
{note ? <p className={classNames.note}>{note}</p> : null}
</div>
)
}
+35
View File
@@ -0,0 +1,35 @@
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 }
}
+2
View File
@@ -1,2 +1,4 @@
export { buildImagesConfig } from './buildImagesConfig.js'
export type { ImagesConfig } from './buildImagesConfig.js'
export { buildR2Storage } from './buildR2Storage.js'
export type { BuildR2StorageDeps, S3StorageFactory } from './buildR2Storage.js'