--- name: web-assets description: Use when optimizing images, fonts, and video for web — AVIF pipeline, responsive srcset, font subsetting, video codec selection, Sharp.js processing. Triggers on "optimize images", "web assets", "image pipeline", "font optimization". arguments: - name: command description: "Command: optimize, picture, fonts, video, audit, pipeline" required: false - name: target description: Directory or file path to process required: false --- # Image & Asset Optimization Pipeline Optimize images, fonts, and video for premium web performance. ## Decision Matrix | Asset | Tool | Format | Quality | |-------|------|--------|---------| | Photos | Sharp.js | AVIF primary, WebP fallback | avif:50, webp:75, jpg:80 | | Icons | SVG sprite | `` + `` | N/A | | Fonts | glyphhanger | WOFF2 only, subset | variable font preferred | | Video | FFmpeg | AV1 > H.265 > H.264 | CRF 28-32 | | AI-generated images | External generator (e.g. fal.ai) + Sharp | Process through Sharp | per above | ## Image Pipeline (Sharp.js) ```bash npm ls sharp 2>/dev/null || npm install sharp ``` Breakpoints: 400, 640, 768, 1024, 1280, 1920px. Max 2560px for Retina. ```javascript const sharp = require('sharp'); const WIDTHS = [400, 640, 768, 1024, 1280, 1920]; const FORMATS = ['avif', 'webp', 'jpg']; const QUALITY = { avif: 50, webp: 75, jpg: 80 }; async function processImage(inputPath, outputDir) { const name = path.parse(inputPath).name; fs.mkdirSync(outputDir, { recursive: true }); for (const width of WIDTHS) { for (const format of FORMATS) { await sharp(inputPath) .resize(width, null, { withoutEnlargement: true }) .toFormat(format, { quality: QUALITY[format] }) .toFile(path.join(outputDir, `${name}-${width}.${format}`)); } } } ``` ### Picture Element ```html Descriptive alt text ``` Hero/LCP image: `fetchpriority="high"`, NO `loading="lazy"`. ## Font Optimization - Variable fonts = industry standard. WOFF2 only (97%+ support) - Subset with glyphhanger: `glyphhanger --US_ASCII --subset=font.woff2 --formats=woff2` (60%+ reduction) - `font-display: swap` + preload critical: `` ## SVG Sprites ```html ``` ## Video AV1 primary (30-50% better than H.264), H.265 fallback, H.264 universal. Always set poster, width/height. ```html ``` Lazy load via IntersectionObserver (no native `loading="lazy"` for `