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:
+4
-4
@@ -79,11 +79,11 @@ describe('archive listing', () => {
|
||||
|
||||
describe('noindex protection', () => {
|
||||
test('the homepage cannot be set to noindex; an ordinary page can', async () => {
|
||||
const home = await payload.create({ collection: 'pages' as never, data: { _status: 'published', slug: 'start', title: 'Start' } as never })
|
||||
const other = await payload.create({ collection: 'pages' as never, data: { _status: 'published', slug: 'dziekujemy', title: 'Dziękujemy' } as never })
|
||||
const home = await payload.create({ collection: 'pages' as never, data: { slug: 'start', _status: 'published', title: 'Start' } as never })
|
||||
const other = await payload.create({ collection: 'pages' as never, data: { slug: 'dziekujemy', _status: 'published', title: 'Dziękujemy' } as never })
|
||||
await payload.updateGlobal({ slug: 'site-settings' as never, data: { homepage: home.id, siteName: 'Acme' } as never })
|
||||
await expect(payload.update({ collection: 'pages' as never, data: { noindex: true } as never, id: home.id })).rejects.toThrow(/Exclude from indexing/)
|
||||
const updated = (await payload.update({ collection: 'pages' as never, data: { noindex: true } as never, id: other.id })) as { noindex?: boolean }
|
||||
await expect(payload.update({ id: home.id, collection: 'pages' as never, data: { noindex: true } as never })).rejects.toThrow(/Exclude from indexing/)
|
||||
const updated = (await payload.update({ id: other.id, collection: 'pages' as never, data: { noindex: true } as never })) as { noindex?: boolean }
|
||||
expect(updated.noindex).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import sharp from 'sharp'
|
||||
import { describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import { MEDIA_SIZES, mediaImageProps, videoProps } from '../src/modules/media/mediaProps.js'
|
||||
import { buildMediaPlaceholderHook } from '../src/modules/media/placeholder.js'
|
||||
import { buildImagesConfig } from '../src/modules/storage/buildImagesConfig.js'
|
||||
|
||||
const image = { alt: 'Ekipa przy pracy', height: 800, mimeType: 'image/webp', url: '/media/a.webp', width: 1200 }
|
||||
|
||||
describe('mediaImageProps', () => {
|
||||
test('lazy by default, dimensions from the document', () => {
|
||||
expect(mediaImageProps(image, { sizes: MEDIA_SIZES.half })).toEqual({
|
||||
alt: 'Ekipa przy pracy',
|
||||
height: 800,
|
||||
loading: 'lazy',
|
||||
sizes: '(min-width: 768px) 50vw, 100vw',
|
||||
src: '/media/a.webp',
|
||||
width: 1200,
|
||||
})
|
||||
})
|
||||
|
||||
test('LCP image: preload + high priority + eager', () => {
|
||||
expect(mediaImageProps(image, { priority: true })).toMatchObject({ fetchPriority: 'high', loading: 'eager', preload: true })
|
||||
})
|
||||
|
||||
test('placeholder and focal point', () => {
|
||||
const p = mediaImageProps({ ...image, focalX: 30, focalY: 70, placeholder: { blurDataURL: 'data:x', color: '#123456' } })
|
||||
expect(p).toMatchObject({ blurDataURL: 'data:x', placeholder: 'blur', style: { backgroundColor: '#123456', objectPosition: '30% 70%' } })
|
||||
expect(mediaImageProps({ ...image, focalX: 50, focalY: 50 })?.style).toBeUndefined()
|
||||
})
|
||||
|
||||
test('video, PDF and bare ids give null; decorative alt override', () => {
|
||||
expect(mediaImageProps({ ...image, mimeType: 'video/mp4' })).toBeNull()
|
||||
expect(mediaImageProps({ mimeType: 'application/pdf', url: '/x.pdf' })).toBeNull()
|
||||
expect(mediaImageProps(42)).toBeNull()
|
||||
expect(mediaImageProps(image, { alt: '' })?.alt).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('videoProps', () => {
|
||||
const video = { mimeType: 'video/mp4', url: '/media/v.mp4' }
|
||||
test('player: controls, nothing downloaded before play', () => {
|
||||
expect(videoProps(video, { poster: { url: '/media/p.webp' } })).toEqual({
|
||||
controls: true,
|
||||
playsInline: true,
|
||||
poster: '/media/p.webp',
|
||||
preload: 'none',
|
||||
src: '/media/v.mp4',
|
||||
})
|
||||
})
|
||||
test('background: muted loop, no autoplay with reduced motion', () => {
|
||||
expect(videoProps(video, { mode: 'background' })).toMatchObject({ autoPlay: true, loop: true, muted: true })
|
||||
expect(videoProps(video, { mode: 'background', reducedMotion: true })?.autoPlay).toBe(false)
|
||||
expect(videoProps(image)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildImagesConfig', () => {
|
||||
test('AVIF/WebP and the R2 domain', () => {
|
||||
expect(buildImagesConfig({ publicUrl: 'https://media.acme.pl/' })).toEqual({
|
||||
formats: ['image/avif', 'image/webp'],
|
||||
minimumCacheTTL: 2678400,
|
||||
remotePatterns: [{ hostname: 'media.acme.pl', pathname: '/**', protocol: 'https' }],
|
||||
})
|
||||
expect(buildImagesConfig({ publicUrl: '/media' }).remotePatterns).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildMediaPlaceholderHook', () => {
|
||||
test('stores a tiny blur image and the dominant colour; skips non-raster files', async () => {
|
||||
const png = await sharp({ create: { background: { b: 40, g: 120, r: 200 }, channels: 3, height: 300, width: 400 } }).png().toBuffer()
|
||||
const data: Record<string, unknown> = {}
|
||||
const req = { file: { data: png, mimetype: 'image/png' }, payload: { config: { sharp }, logger: { warn: vi.fn() } } }
|
||||
await buildMediaPlaceholderHook()({ args: { data }, operation: 'create', req } as never)
|
||||
const ph = data.placeholder as { blurDataURL: string; color: string }
|
||||
expect(ph.blurDataURL).toMatch(/^data:image\/webp;base64,/)
|
||||
expect(ph.blurDataURL.length).toBeLessThan(600)
|
||||
expect(ph.color).toBe('#c87828')
|
||||
|
||||
const svgData: Record<string, unknown> = {}
|
||||
await buildMediaPlaceholderHook()({ args: { data: svgData }, operation: 'create', req: { ...req, file: { data: png, mimetype: 'image/svg+xml' } } } as never)
|
||||
expect(svgData.placeholder).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user