The page that jumped on every reload

Every time I reloaded the prototype of this site, something jumped. The name in the sidebar grew into place. The headline snapped to a new size. On a phone the whole screen twitched.

I tracked down the causes on 13 and 14 September 2026. None of them was a big bug. Each one was small, and they stacked on top of each other. The fixes are now a standard for every site I build.

Why text jumps, even with preload and cache

The browser always starts painting before the web font arrives. With font-display: swap it shows a fallback font and swaps later. Preload and cache shrink that window to milliseconds, but one to three fallback frames almost always reach the screen. On small text the eye forgives it. On my 96 px headline you saw it every time.

The fallback has different metrics, so lines wrap differently and blocks change height. And if JavaScript measures text, it measures the fallback first and the real font second. Two jumps, one on top of the other.

Jump one: the starting size was a guess

My name in the sidebar is a fitted line: a script sizes it to fill the column. The CSS started it at 2.35rem, and the script stretched it to 48.4 px. You could watch it grow.

On desktop the column is a fixed 320 px, so the final size is known. I wrote it into the CSS:

@media (min-width:1024px) {
  /* … */
  .mark { font-size:48.4px; }
}

The CSS now lands at 319 of 320 px on the first try. The size jump was gone. The flicker wasn't.

Jump two: the fallback font

The prototype uses Pangea by Fontwerk in trial files: four weights, about 180 KB each. Trial files aren't subset; subsetting comes after the licence is bought. With swap, preload made the flash shorter but couldn't remove it.

The preloads stayed. What changed was swap, which became block:

<link rel="preload" href="fonts/pangea-regular.woff2" as="font" type="font/woff2" crossorigin>
<!-- … Medium, SemiBold, Bold … -->
@font-face { font-family:'Pangea'; font-weight:400; font-style:normal; font-display:block; src:url(fonts/pangea-regular.woff2) format('woff2'); }
/* … 500, 600, 700 … */

Keep crossorigin on font preloads, even on your own domain. Without it the browser downloads the font twice.

The other half was timing. The fitting script makes its first pass only after document.fonts.ready:

function relayout() { fitMark(); fitState(); fitNav(); measureSticky(); }
window.addEventListener('resize', relayout);
/* … */
measureSticky();
if (document.fonts && document.fonts.ready) document.fonts.ready.then(relayout);
else relayout();

The font swap disappeared completely. And the script no longer sets an inline font size on the name: the CSS value already fits.

Lighthouse flags block under "Ensure text remains visible during webfont load". With files of 30 KB or less there's no practical harm. With 180 KB files there is. More on that below.

Jump three: 100vw and the scrollbar

On mobile, the headline's starting size came from 100vw. The coefficient matched in Playwright and missed by about 2 px on a real phone. The script corrected those 2 px after fonts.ready, and the browser's scroll anchoring moved the whole screen. It looked like the photo jumped too.

100vw includes a classic scrollbar (desktop, Playwright) but not an overlay one (phones). 1cqi is 1% of the container's actual width, so one formula is exact everywhere. The 100vw line stays as a fallback for older browsers:

@media (max-width:1023px) {
  /* … */
  .feed { padding:20px 20px 64px; container-type:inline-size; }
  /* … */
  .state { font-size:min(calc((100vw - 40px) * .1414), 86px); font-size:min(14.14cqi, 86px);
    margin-bottom:calc(8.9px - .197em); }
}

Jump four: the sticky menu

On desktop the menu and the button stick below the sidebar header. Their top values are custom properties that JavaScript measures. After a reload on a scrolled page, the menu sat for a frame at the default value, then jumped.

Now the CSS defaults are the real values:

.side-stick { top:var(--head-h, 219px); background:transparent; }
.side .btn { position:sticky; top:var(--btn-top, 386px); z-index:5; }

And measureSticky() runs immediately, not after the fonts. The header height comes from font sizes and line heights, not glyphs, so there's nothing to wait for. It's the call right before fonts.ready above.

Jump five: the cache

By default Cloudflare served my static files with max-age=0, must-revalidate. Every reload on a phone revalidated fonts and images: 5–6 round trips, invisible text, a blinking photo.

My build script now writes a _headers file for Cloudflare Workers static assets:

# …
/fonts/*
  Cache-Control: public, max-age=31536000, immutable
/img/*
  Cache-Control: public, max-age=31536000, immutable

When you replace an image, give it a new file name. And save the file without a BOM, or Cloudflare doesn't parse the first line.

The photo also got a preload, fetchpriority="high" and a background colour in its tone. While it loads, you see a calm square, not a white hole.

The one I didn't fix: reload on iOS

On an iPhone the sticky menu still seemed to twitch after a reload near the bottom of the page. I measured it frame by frame, with a small diagnostic overlay behind ?diag=1. Safari paints the first frame from the top of the page, then about 50 ms later teleports the scroll to the saved position.

Zero layout shifts. My sizes were stable from the first frame. The sticky menu is just visible in both frames. A site I compared with shows the same, even stronger. I accepted it as platform behaviour.

Then PageSpeed said LCP was 4.5 seconds

The LCP element is the photo. I emulated PageSpeed's mobile conditions in real Chrome: 150 ms latency, 1.6 Mbit/s, CPU slowed down 4×. LCP was 1.8 seconds. Without the fonts, 1.1.

PageSpeed doesn't really load the page. It models a slow network and assumes that everything requested before LCP shares the connection equally. Here, 705 KB of about 800 are trial fonts that cover every alphabet in the world. In my emulation they finished at 4.3 seconds. Heavy fonts preloaded next to the LCP image hurt it more in the report than in life.

fetchpriority belongs on the preload

Lighthouse also flagged the photo under "LCP request discovery". It was preloaded with <link rel="preload" as="image">, and fetchpriority="high" sat on the <img>. But the request goes out from the preload, so the attribute on the image can't raise it. On 23 September the Chrome DevTools Protocol showed the initial priorities: photo Low, fonts High.

The fix is one attribute on the preload. It stays on the image too, with no loading="lazy":

<link rel="preload" href="img/mihai.webp" as="image" fetchpriority="high">
<!-- … -->
<img class="photo" src="img/mihai.webp" width="340" height="340" alt="Mihai Papush" tabindex="0" fetchpriority="high">

After that, everything started as High. PageSpeed LCP went from 4.5 to 3.0 seconds.

The real fix is lighter fonts

PageSpeed exaggerates, but the problem is real. On a slow network the text stays invisible for about 3.5 seconds, the block period, and then swaps with a jerk.

Optimising trial files wasn't worth it. Once the design settled, I bought the licence and subset the fonts to Latin plus Romanian diacritics: 30–33 KB per weight. A phone now loads two weights, about 62 KB; a desktop loads three, about 94 KB.

A site I studied uses the same Pangea. It doesn't set font-display, which in practice behaves like block. Its files are about 27 KB and arrive in milliseconds. No pause, no swap. That was the target.

The rules

  1. Self-hosted, subset WOFF2. One or two families, two or three weights. The whole font layer stays under roughly 80–100 KB.
  2. Preload the first-screen weights with crossorigin.
  3. font-display: block when the files are small and preloaded.
  4. Starting CSS sizes close to the final ones.
  5. cqi instead of 100vw, with 100vw as a fallback.
  6. Measure text only after document.fonts.ready.
  7. Sticky offsets whose CSS defaults equal the real values.
  8. Immutable, one-year cache for fonts and images.
  9. fetchpriority="high" on the LCP image's preload and on the <img>.
  10. Test in DevTools on "Fast 4G" and "3G": reload five times and watch the biggest text. Then again with "Disable cache".

← All notes

mihai@papush.ro