◉ANTIGRAVITY LABJP
Articles/App Development
▣ App Development/2026-12-01Advanced

Moving Shiki's Inline Styles to Classes Without Breaking Cached HTML: Name Each Class After Its Color Pair

With dual themes, rehype-pretty-code writes a roughly 50-character style attribute on every colored token. Here is how I moved those to classes on my own site (hash-based class names, no attribute at all for the default color, CSS written at build time), and what I found when I re-measured production pages: up to 15% less uncompressed HTML, but only 1.4% less over the wire.

shikirehype-pretty-codeNext.js12Cloudflare Workers10CSSperformance15indie developer23Antigravity428

✦ Premium Article

In early November I was trimming the article pages on my own site. I had the raw HTML open in the browser's dev tools, and only inside the code blocks did the lines run absurdly long.

<span style="--shiki-dark:#F47067;--shiki-light:#D73A49">const</span>. Every keyword, every bracket, carried the same shape of style attribute.

My notes from that week say these attributes made up 20–30% of the article HTML, that the same string was also embedded in the page's serialized data (so it effectively counted twice), and that the heaviest article page went from 598KB to 434KB once I fixed it. The fix itself is simple: swap the style attributes for classes. But inside that "simple" there was one trap that only shows up after a deploy.

When I re-measured production pages today, a second surprise turned up as well. I'll walk through both.

The short version

  • Name classes by a hash of the color pair, not a running counter. A counter gets renumbered whenever articles are added, and the old HTML still sitting in the edge cache stops matching the new CSS.
  • Tokens that use the theme's default color get no attribute at all. CSS custom properties inherit from parent to child, so setting the default pair once on the code element is enough.
  • What shrinks is uncompressed bytes. On the pages I measured, the compressed transfer size moved by only 1.4%. Whether the change is worth doing depends on which byte count your pages are actually bound by.

What was going on

As an indie developer I run this site on Next.js, converting each MDX article to HTML before the build and serving it from Cloudflare Workers. Code coloring comes from rehype-pretty-code and shiki, with a dark and a light theme set at the same time.

With dual themes, shiki doesn't write colors directly. It emits two custom properties per token, and the page's CSS picks one based on the active theme.

/* The reader-side rules in globals.css (unchanged to this day) */
.article-content [data-rehype-pretty-code-figure] code span {
  color: var(--shiki-dark);
  font-weight: var(--shiki-dark-font-weight, normal);
  font-style: var(--shiki-dark-font-style, normal);
}
[data-theme="light"] .article-content [data-rehype-pretty-code-figure] code span {
  color: var(--shiki-light);
  font-weight: var(--shiki-light-font-weight, normal);
  font-style: var(--shiki-light-font-style, normal);
}

The mechanism is clean. The volume was the problem. style="--shiki-dark:#ADBAC7;--shiki-light:#24292E" is about 50 characters on its own, repeated once per colored token. On code-heavy articles the color declarations outgrew the prose.

Meanwhile, the number of distinct color combinations is tiny. The production CSS currently defines 25 pairs, and any one page uses 8 to 11 of them. I was shipping the same 50 characters hundreds of times.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Before moving syntax-highlight styles into classes, you will be able to tell whether your pages are limited by uncompressed bytes or by transfer size, and decide if the change is worth it
✦By deriving class names from a hash of each color pair, you can prevent the quiet bug where HTML still cached at the edge gets painted with the wrong colors after a deploy
✦You will be able to confirm in under a minute that every class in your HTML is actually defined in your CSS, before and after the swap
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

▣ App Dev2026-11-24
Two Times Inlining CSS Made FCP Slower, and How I Now Decide Per Site Whether to Inline
I inlined CSS into the HTML to clear PageSpeed Insights' render-blocking warning, and FCP got almost a second slower. The font preload had been pushed down to byte 28,672 of the head. Even after fixing the placement, the same change made a second site with 88KB of CSS slower, and I rolled it back. Here is a script that counts byte offsets inside the head, an inliner with a size cap, and the pattern I use on the larger site instead.
▣ App Dev2026-11-20
Correct on My Machine, Nine Hours Off in Production: Tracking Down Offset-less Date Strings
Why sitemap lastmod and JSON-LD datePublished drift exactly nine hours ahead only in production, how JavaScript parses date-only and date-time strings differently, and a detection script plus TZ-pinned tests you can run today.
▣ App Dev2026-10-15
I Swapped an Immutable Asset at the Same URL, and Only the Browser Kept the Old Copy
Overwrite a long-cached JS file at the same URL and neither a CDN purge nor a refresh will make the browser fetch it again. Four delivery styles — same URL, no-cache, query string, content-hash filename — compared in Chromium, plus a complete build-step script that adds the hash.