Fast Media Loading
Goal: make media load fast by sending fewer bytes, serving them from closer to the user, and loading them at the right moment.
Workflow
- Detect the stack (Next.js, Nuxt, Astro, React/Vite, WordPress, plain HTML, ...) from
package.json, config files and templates. Prefer the framework's built-in image tooling over hand-rolled markup. See references/frameworks.md. - Audit first. Run the bundled script from the project root:
It lists oversized media files, legacy formats, andbash <path-to-skill>/scripts/audit-media.sh . 300<img>/<video>tags missingwidth,height,loadingorpreload. Its output is a starting point, not a verdict: it only sees single-line tags and static files. Also read the components that render media. - Report findings by impact, highest first, each with file path, the problem and the expected gain.
- Fix what the user asked for. Don't refactor unrelated code and don't add dependencies without asking.
- Verify. Build or run the project if possible, and tell the user how to re-measure (Lighthouse, PageSpeed Insights, DevTools throttled to Slow 4G).
Checklist
1. Shrink the files
- Photos: AVIF or WebP, with a JPEG fallback in
<picture>only if needed. Icons and logos: SVG. - Replace animated GIFs with muted, looping
<video>. - Video: H.264 MP4 as baseline, optionally WebM/AV1 as extra
<source>. Long videos use adaptive streaming (HLS/DASH) or a hosted service, not one large MP4. - Details and commands: references/images.md, references/video.md.
2. Serve the right size
- Use
srcsetandsizesso phones don't download desktop-size images. - Prefer an image CDN/optimizer that resizes and converts on request.
- Always set
widthandheight(or CSSaspect-ratio) to prevent layout shift (CLS).
3. Load at the right time
loading="lazy"anddecoding="async"on below-the-fold images and iframes.- Never lazy-load the LCP/hero image. Give it
fetchpriority="high". - Video:
preload="metadata"(ornone), aposter, and no autoplay of large files. Autoplay only when muted andplaysinline. - Use a low-quality placeholder (blur, BlurHash or dominant colour).
4. Deliver from closer to the user
- Put media behind a CDN with edge locations near the actual users.
- HTTP/2 or HTTP/3, long cache headers on fingerprinted files, range requests for video.
- Details: references/caching-and-cdn.md.
5. Measure
- Track LCP, CLS and total page weight with Lighthouse and DevTools, and with real-user monitoring (
web-vitals) if available.
Priority order when time is short
- CDN in front of media.
- WebP/AVIF plus responsive sizes.
- Lazy-load everything except the hero image.
- Adaptive streaming for long video.
- Long-lived cache headers.
Output format
- Findings: short list, each with file path, problem, and expected gain (high/medium/low).
- Changes made (or proposed), with code.
- How to verify: exact commands or tools to re-measure.
Keep it concise.