feat: WebP and srcset for featured images
- JournalFeaturedImage renders <picture> from featured_images[].variants: a WebP source and the original format as srcset fallback, the original's width and height, alt; lazy in cards, eager with fetchpriority high on the post page; unchanged without variants - app/utils/srcset.ts (imageSrcsets) with node tests - acme fixtures and SSR expectations for both cases
This commit is contained in:
45
tests/srcset.test.ts
Normal file
45
tests/srcset.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// srcset strings of featured images from the API's variants (D-85, D-71).
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { imageSrcsets } from '../app/utils/srcset.ts'
|
||||
|
||||
const v = (width: number, height: number) => ({
|
||||
width,
|
||||
height,
|
||||
url: `/u/thumb_1_${width}_${height}_0_0_exact.jpg`,
|
||||
webp_url: `/u/thumb_1_${width}_${height}_0_0_exact.webp`,
|
||||
source_width: 1000,
|
||||
source_height: 600,
|
||||
})
|
||||
|
||||
test('WebP and original srcsets from the variants, narrowest first, with the original wider than every variant', () => {
|
||||
const sets = imageSrcsets([v(800, 480), v(480, 288)], '/u/cover.jpg')
|
||||
assert.deepEqual(sets, {
|
||||
webp: '/u/thumb_1_480_288_0_0_exact.webp 480w, /u/thumb_1_800_480_0_0_exact.webp 800w',
|
||||
original: '/u/thumb_1_480_288_0_0_exact.jpg 480w, /u/thumb_1_800_480_0_0_exact.jpg 800w, /u/cover.jpg 1000w',
|
||||
width: 1000,
|
||||
height: 600,
|
||||
})
|
||||
})
|
||||
|
||||
test('the original is left out when a variant is as wide, and an explicit width wins', () => {
|
||||
assert.equal(imageSrcsets([v(480, 288)], '/u/cover.jpg', 480)?.original, '/u/thumb_1_480_288_0_0_exact.jpg 480w')
|
||||
assert.equal(imageSrcsets([v(480, 288)], null)?.original, '/u/thumb_1_480_288_0_0_exact.jpg 480w')
|
||||
})
|
||||
|
||||
test('no variants give no srcset', () => {
|
||||
assert.equal(imageSrcsets([], '/u/cover.jpg'), null)
|
||||
assert.equal(imageSrcsets(undefined, '/u/cover.jpg'), null)
|
||||
assert.equal(imageSrcsets(null), null)
|
||||
assert.equal(imageSrcsets([{ width: 0, height: 1, url: 'a', webp_url: 'b' }, { width: 480 }], '/x'), null)
|
||||
})
|
||||
|
||||
test('a width appears once, and without the source size the largest variant reserves the space', () => {
|
||||
const plain = [{ width: 480, height: 300, url: '/a.jpg', webp_url: '/a.webp' }, { width: 480, height: 300, url: '/b.jpg', webp_url: '/b.webp' }]
|
||||
assert.deepEqual(imageSrcsets(plain, '/o.jpg'), { webp: '/a.webp 480w', original: '/a.jpg 480w', width: 480, height: 300 })
|
||||
})
|
||||
|
||||
test('spaces and commas in addresses cannot split a candidate', () => {
|
||||
const sets = imageSrcsets([{ width: 480, height: 300, url: '/a b,c.jpg', webp_url: '/a b,c.webp' }])
|
||||
assert.equal(sets?.webp, '/a%20b%2Cc.webp 480w')
|
||||
})
|
||||
Reference in New Issue
Block a user