--- import { getImage } from 'astro:assets'; import type { ImageMetadata } from 'astro'; import { screens, type ScreenName } from '../lib/screens'; interface Props { name: ScreenName; /** Above the fold: load immediately with high priority. */ priority?: boolean; sizes?: string; /** Override the default alt text. */ alt?: string; class?: string; } const { name, priority = false, sizes = '(min-width: 1160px) 1120px, 100vw', alt, class: className } = Astro.props; const spec = screens[name]; const files = import.meta.glob<{ default: ImageMetadata }>('/src/assets/screens/*.{png,jpg,jpeg,webp,svg}', { eager: true }); const raster = ['png', 'webp', 'jpg', 'jpeg']; /** Real captures (png/webp/jpg) beat placeholder SVGs; `-` beats a bare ``. */ function pick(theme: 'dark' | 'light'): ImageMetadata { const tries = [ ...raster.map((e) => `${name}-${theme}.${e}`), ...raster.map((e) => `${name}.${e}`), `${name}-${theme}.svg`, `${name}.svg`, ]; for (const t of tries) { const hit = files[`/src/assets/screens/${t}`]; if (hit) return hit.default; } throw new Error(`No screenshot found for "${name}" (${theme}) in src/assets/screens/`); } /** Optimised webp srcset for real captures; SVG placeholders pass through untouched. */ async function sources(img: ImageMetadata) { if (img.format === 'svg') return { src: img.src, srcset: img.src }; const widths = [720, 1200, 1600, 2400].filter((w) => w < img.width).concat(img.width); const out = await getImage({ src: img, widths, format: 'webp' }); return { src: out.src, srcset: out.srcSet.attribute }; } const darkImg = pick('dark'); const dark = await sources(darkImg); const light = await sources(pick('light')); ---
{alt