media-optimization
Skill1 starsChanged 3 months agoMITView on GitHub
Image, font, and video delivery engineering for Next.js 16 — next/image with preload (priority is deprecated), fill+sizes pairing, blur placeholders, a central next/font pipeline with self-hosted variable fonts, video facades, and LCP protection rules. Invoke during the build phase whenever a section renders an image, custom font, or video; and whenever the user mentions slow images, blurry images, layout shift, CLS, LCP, font flashing, "optimize images", "the hero loads slow", or Lighthouse complaints about media weight.
In this repository79 other files it gives its agents.
- ultrawebSkill
- analyticsSkill
- animejsSkill
- api-designSkill
- app-structureSkill
- assetsSkill
- authSkill
- award-canonSkill
- briefSkill
- buttonsSkill
- cardsSkill
- cartSkill
- checkpointSkill
- colorSkill
- command-paletteSkill
- component-apiSkill
- consentSkill
- content-cmsSkill
- copywritingSkill
- databaseSkill
- data-displaySkill
- data-fetchingSkill
- depthSkill
- directionSkill
- emailSkill
- faqSkill
- feature-sectionsSkill
- footerSkill
- formsSkill
- gate-accessibilitySkill
- gate-antislopSkill
- gate-codeSkill
- gate-contentSkill
- gate-performanceSkill
- gate-responsiveSkill
- gate-visualSkill
- handoffSkill
- heroSkill
- hidden-craftSkill
- i18nSkill
- iconsSkill
- identitySkill
- imagerySkill
- iterateSkill
- layout-gridSkill
- marginaliaSkill
- micro-interactionsSkill
- mockupSkill
- motion-languageSkill
- navigationSkill
- overlaysSkill
- page-transitionsSkill
- paymentsSkill
- physicsSkill
- previewSkill
- pricingSkill
- print-craftSkill
- product-detailSkill
- retrofitSkill
- routingSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 44
Every SVG that ships — logo, icon, divider, illustration — runs through SVGO once, with a config that knows whether the file will be …
- Distinctive: line 50
Run it as a build step or one deliberate pass over the asset folders — never hand-edit optimized output, and never re-run a lossy preset …
- Very specific: line 72
Never animate the LCP element from opacity: 0 — an invisible element can't be an LCP candidate, so the metric slides to whenever the reveal …
- Very specific: line 74
No LCP element inside a dynamic() import or lazy boundary.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
media-optimization — fast media, zero shift
Stage: Phase 6 — Build (engineering) - Reads: design/SYSTEM.md §imagery + §type, design/SITEMAP.md, assets from ultraweb:imagery - Writes: next/image usage in sections, lib/fonts.ts, video embeds, public/ asset layout
Standard
Media discipline is invisible when right, ruinous when wrong. The bar: LCP ≤ 2.0s on throttled Fast 4G mid hardware, CLS = 0 from images/fonts/video, every image delivered within 1.5× its rendered pixel size, fonts self-hosted with zero third-party requests, at most one preloaded image per page — exactly one when the LCP element is an image, zero when it is text. gate-performance measures this; this skill is where you earn it.
Process
- Walk
design/SITEMAP.mdand inventory every visual asset per section: rendered size at 375/768/1440, above or below the fold, content or decoration. - Name the LCP element of each page (usually the hero image or hero headline). Write it down — the protection rules below apply to it and only it.
- Wire fonts once in
lib/fonts.ts(pipeline below) before any section styling depends on them. - Implement each image per the rules below; static-import everything that lives in the repo.
- Below-fold media stays lazy (next/image default); above-fold media follows LCP protection.
- Smoke-check:
npm run build, load each page, confirm no oversized image responses and no visible shift. Hand the numbers togate-performance.
next/image — Next 16 rules
priorityis DEPRECATED → usepreload.onLoadingComplete→onLoad. Both old names are greppable relics; never write them.- Repo asset, known size → static import. Next infers
width/height(zero CLS) and generates theplaceholder="blur"data automatically:
import hero from "@/public/hero.jpg";
<Image src={hero} alt="Thrown stoneware on the wheel" preload placeholder="blur" sizes="100vw" />- Crop-to-container →
fill+sizes, ALWAYS paired.fillrequires arelativeparent with real dimensions andobject-cover(orobject-contain) on the image itself. sizesdeclares the rendered width — get it wrong and Next ships the 4K rendition to a 300px card:- Full-bleed:
sizes="100vw" - Split section:
sizes="(min-width: 768px) 50vw, 100vw" - 3-up card grid:
sizes="(min-width: 1024px) 33vw, (min-width: 640px) 50vw, 100vw"
- Full-bleed:
- Remote images (CMS/storage): allowlist the host in
next.config.ts(images.remotePatterns), pass explicitwidth/heightfrom the source, and supply a realblurDataURL— orplaceholder="empty"over a token-colored background. A remote image must never reserve zero space. - SVG logos and icons: inline as JSX so they inherit
currentColor— never through next/image. - Modern formats are the optimizer's job. Feed it a high-quality source (≥ 2× the largest rendered size); don't pre-convert or pre-compress to death.
SVGO discipline
Every SVG that ships — logo, icon, divider, illustration — runs through SVGO once, with a config that knows whether the file will be animated. The default preset is lossy in exactly the ways an animation cares about.
cleanupIds: falseon anything animated or referenced. The default minifiesidattributes, which silently breaks<use href="#…">, gradient/filter references, and every selector a CSS keyframe or a commissionedultraweb:animejstimeline targets. A static one-shot mark may keep the default.- Keep the viewBox (
removeViewBox: false) — without it the graphic can't scale, and a fixedwidth/heightreintroduces the layout shift the rest of this file exists to prevent. - No
mergePathson morph targets. Merging collapses separate<path>elements into oned, destroying both the per-path handles a multi-path draw needs and the point-count parity a morph pair depends on —ultraweb:shape-languageauthors those files, this one must not undo them. - Strip what is genuinely dead: editor metadata, comments, empty groups, and hardcoded
fillon icons meant to inheritcurrentColor. - Run it as a build step or one deliberate pass over the asset folders — never hand-edit optimized output, and never re-run a lossy preset over a file you already tuned.
next/font pipeline
One central file. A font declared anywhere else is a defect.
// lib/fonts.ts — the ONLY place fonts are declared
import { Fraunces, Instrument_Sans } from "next/font/google"; // auto self-hosted, zero Google requests
export const display = Fraunces({ subsets: ["latin"], variable: "--font-display-src", display: "swap" });
export const body = Instrument_Sans({ subsets: ["latin"], variable: "--font-body-src", display: "swap" });- Variable fonts: omit
weight— the whole axis ships in one file. Two families max (constitution); each family = one variable file, never four static weights. - Root layout:
<html className={${display.variable} ${body.variable}}>. The-srcsuffix avoids colliding with the@themenamespace:ultraweb:tokensbridges--font-display: var(--font-display-src)in@theme inline. This skill owns loading; tokens owns the bridge. - Purchased faces:
next/font/localfrom the same file —localFont({ src: "./fonts/Name-Variable.woff2", variable: "--font-display-src", display: "swap" }). - next/font auto-adjusts fallback metrics — font swap causes zero CLS. Never add hand-written
@font-faceor manualsize-adjustbeside it.
LCP protection
- The LCP image gets
preload— and ONLY it. Two preloaded images on one page means neither is protected. - Never animate the LCP element from
opacity: 0— an invisible element can't be an LCP candidate, so the metric slides to whenever the reveal finishes. Render it immediately; animate its siblings. - LCP text is server-rendered with its font variable applied from first paint — never inside a late-mounting client component.
- No LCP element inside a
dynamic()import or lazy boundary. - Hero background media is a real
<Image fill preload>— CSSbackground-imageis unoptimizable and unpreloadable.
Native video
- Ambient loops: self-hosted H.264 MP4 ≤ 4MB,
<video autoPlay muted loop playsInline preload="metadata" poster={…}>with a poster sized exactly like the video (zero CLS). Both attributes are load-bearing: withoutmutedautoplay is blocked, withoutplaysInlineiOS hijacks the clip fullscreen. - The poster is the LCP candidate, never a video frame. Ship it as a real optimized still at the video's exact dimensions and give it the page's one
preload; the clip then plays over an element that has already painted, and a slow connection degrades to a designed frame instead of a black box. prefers-reduced-motion: pause ambient video — followultraweb:motion-language's policy, not an ad-hoc check.- Meaningful video ships captions — a
<track kind="captions" srcLang default>file. WCAG 1.2.2 is a Level-A requirement, sogate-accessibilitytreats a missing track as a failure; only genuinely silent decorative loops are exempt, and those carryaria-hidden. - Third-party embeds (YouTube/Vimeo): facade pattern — render a poster + play button, inject the iframe on click. An eager YouTube iframe costs ~1MB of JS before anyone presses play. On a DACH build that facade is also the legal gate: mount it through
ultraweb:consent's click-to-load shim, since the request itself is the exposure. - Below-fold video:
preload="none".
Anti-patterns
Greppable; every hit is a defect:
priority/onLoadingCompleteon next/image — Next ≤15 relics (Next 16:preload/onLoad)layout="fill",objectFit=props — Next 12 relics<imgin TSX (sole exception: insideopengraph-image.tsxImageResponse markup)fillwithoutsizes;sizes="100vw"on anything narrower than the viewportunoptimizedas an escape hatch instead of fixing configfonts.googleapis.com/fonts.gstatic.comanywhere — next/font self-hosts, always- hand-written
@font-facealongside next/font - more than one
preloadimage per page bg-[url(/backgroundImage:for content imagery
Worked example — Loop & Thread, product photos from blob storage
Moved to references/example.md — read only when this build's case is genuinely ambiguous; the sections above are the decision material.
Composes with
Moved to references/composes.md — the handoff map; load it when orchestrating this skill against its neighbors.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

