Skip to content

Play page: no layout shift, engine sooner, / → /play prerendered - #12

Merged
AdminGodZ merged 1 commit into
mainfrom
t3/play-navigation-performance
Nov 21, 2026
Merged

AdminGodZ merged 1 commit into
mainfrom
t3/play-navigation-performance

Conversation

@AdminGodZ

Copy link
Copy Markdown
Owner

Switching from / to /play was slow and the page jumped. The page looks exactly the same as before; it just loads faster and holds still.

What was wrong

  • Layout shift: the game script built both charts and the ship dock in the browser, so the page grew by a full chart after it was first shown. Mobile CLS was 0.43, which counts as "poor" (anything over 0.25).
  • Engine loaded late: the 1 MB wasm (127 KB with brotli) only started downloading after the game script ran. It was then compiled in a separate step after the download finished.
  • No caching: Vercel served the hashed /_astro/* files with max-age=0, must-revalidate, so every page view rechecked the CSS, fonts, JS and wasm with the server.
  • Nothing fetched ahead: clicking Play started the whole load from scratch.

Changes

  • The page arrives already drawn (Chart.astro, Game.astro): both charts, the dock, the shot counters and the hint are built into the HTML, and game.ts hooks into that markup instead of creating it. A tiny inline script sets the status line before the first paint. For a returning player it shows "Your fleet is ready". For a new player it reserves a hidden line of the right height. A saved fleet appears and can be moved before the engine has loaded. Random and Start battle wait for the engine if they're pressed before it's ready.
  • The engine starts with the page: a <link rel="preload"> for the wasm, and WebAssembly.instantiateStreaming so it compiles while it downloads. If the server sends the wrong content type, it falls back to the old non-streaming way.
  • vercel.json: /_astro/* gets Cache-Control: public, max-age=31536000, immutable.
  • CSS is inlined in each page (build.inlineStylesheets: "always"), so no stylesheet request delays the first paint.
  • Speculation rules (Base.astro): Chromium prerenders a site page when you hover over a link to it or press it, so the script and engine are already running when you arrive. /play's HTML is fetched ahead as soon as a link to it is on the page, and download links are skipped. Safari and Firefox don't support this, so a small script fetches /play and the engine into the cache the first time you hover, focus or touch a Play link.
  • offline.mjs removes the engine preload and the fetch-ahead scripts from broadside.html, since they mean nothing in a file opened from disk.

The wasm build itself is unchanged. I tried opt-level 2 and s: they save 20–45 KB with brotli, but make the engine 2–3× slower per move (mean 20 ms at opt 3, 36 ms at opt 2, 61 ms at opt s), so opt-level 3 stays.

Measured

Headless Chrome, throttled to 4 Mbit/s with 100 ms latency, on a local server that sends the same headers as vercel.json with brotli. Each test loads /, hovers Play, clicks it and times until the board is playable.

before after
CLS on /play, mobile 390 px 0.43 0
CLS on /play, desktop 0.003 0
Click to playable, new visitor ~1150 ms ~420 ms
Click to playable, saved fleet ~1150 ms ~190 ms
First paint after navigating ~390 ms ~25–40 ms

These numbers leave out the prerender, because Chrome turns prerendering off while automation tools are connected (PrerenderingDisabledByDevTools). The rules are valid and Chrome does start them on hover. With a real browser the switch to /play should be instant.

The first desktop run of each batch was a one-off at ~1170 ms; later runs were ~420 ms.

Checks

  • npm run build passes, including the offline file (1.81 MB).
  • npm run check: 0 errors, 0 warnings, 0 hints.
  • cargo clippy --all-targets is clean and cargo test passes (no Rust was changed).
  • Pixel comparison against main: /play (desktop and mobile, light and dark, before and after picking up a ship) and / are identical.
  • Playthroughs in a browser, no console errors:
    • a new visitor plays a full game and starts a new one
    • a saved fleet with a slow engine: a ship moved and Start pressed before the engine loaded, then the battle begins
    • the offline broadside.html plays a full game

- The play page's markup is the game's first frame: both charts, the ship
  dock and the status line are drawn at build time (Chart.astro), so nothing
  moves when the script and engine arrive (mobile CLS 0.43 → 0). A saved fleet
  is shown before the engine has loaded; Random and Start wait for it.
- The engine is preloaded with the page and compiled while it downloads
  (instantiateStreaming) instead of fetched after the script ran.
- Hashed assets under /_astro/ are cached for a year, immutable; they were
  revalidated on every page view.
- Stylesheets are inlined: no request holds up the first paint.
- Speculation rules: Chromium prerenders a page of the site when a link to it
  is hovered (or pressed), and fetches /play's page ahead; elsewhere, the page
  and engine are fetched on the first sign of heading for /play.

Measured on a throttled connection (4 Mbit/s, 100 ms), click on Play to a
playable board: about 1150 ms before, 420 ms now (190 ms with a saved fleet),
without counting the prerender, which headless Chrome does not do. Screenshots
of /play and / are pixel-identical to before.
@vercel

vercel Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
broadside Ready Ready Preview Nov 21, 2026 1:26pm UTC

@AdminGodZ
AdminGodZ merged commit 1735b48 into main Nov 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant