Skip to content

perf(build): inline critical css and trim the entry bundle - #656

Merged
TheMeinerLP merged 2 commits into
mainfrom
perf/critical-render-path
Nov 23, 2026
Merged

TheMeinerLP merged 2 commits into
mainfrom
perf/critical-render-path

Conversation

@TheMeinerLP

Copy link
Copy Markdown
Collaborator

Summary

On a phone the home page appeared late. Two things stood in the way of the first paint: the page waited for a render-blocking stylesheet request, and the entry script was almost 480 KB, two thirds of it the analytics library, which every visitor downloaded and parsed before the page became interactive.

This change inlines the global stylesheet into the HTML, so no request blocks the first paint, and loads the analytics library after hydration, so it no longer sits in the entry bundle.

Changes

  • perf(build): inline the global stylesheet into the page — features.inlineStyles: true, with tests/performance/critical-css.spec.ts.
  • perf(build): load posthog-js after hydration — a client plugin replaces nuxt-posthog's one. Calls made before the library arrives are queued and replayed in order, so the production-host opt-out and stored consent still run before any capture. Feature flags still bootstrap from the server payload, so blog-ethanol-conversion resolves as before. Budget test for the entry chunk (reads .output, skipped without a build).

Measurements

Production build under wrangler dev, Lighthouse 12.8.2, default mobile throttling, medians. Base is origin/main (n=5), after is this branch (n=10), runs interleaved on one machine. Desktop is n=3.

Page Performance FCP LCP TBT
/en mobile 76 -> 86 3971 -> 3073 ms 4121 -> 3283 ms 127 -> 64 ms
/de mobile 71 -> 77 3822 -> 3165 ms 4728 -> 4627 ms 181 -> 61 ms
/en/blog mobile 70 -> 78 3372 -> 2544 ms 4691 -> 3834 ms 270 -> 300 ms
/en desktop 97 -> 98 991 -> 857 ms 1031 -> 919 ms 0 -> 0 ms

Entry chunk: 478,484 B -> 136,400 B raw, 139,226 B -> 42,808 B brotli. The HTML grows by the inlined stylesheet, about 17 KB brotli per page, inside the 25 KB budget. Machine load stayed below 12 except for some /en runs (up to 16), where the base's minimum was a 53 outlier.

Tried and dropped

  • Trimming modulepreload to the entry chunks (first commit of this branch, removed): measured n=10 against the same build without it, Performance dropped on /en/blog 78 -> 72 and /de/events 87 -> 80 while /en rose 86 -> 92 and /de was flat (77 -> 76). That breaks the rule of no page losing more than 2 points, so the commit and its test are gone.
  • Smaller Tailwind @source and lazy seasons CSS: each under 3 KB brotli, skipped.
  • sqlite/wasm is not on the critical path; further entry-chunk contents (i18n, FontAwesome, season decorations) are each under 10 KB brotli and were left alone.

Verification

  • pnpm test (2178 passed), pnpm quality (no rise; TypeScript errors still at the baseline 3), pnpm build.
  • node scripts/cache-check.mjs and pnpm seo:check -- --base http://localhost:8787 --no-mock against the production build: no errors.
  • Production build opened in headless Chromium on /en, /de, /en/blog and /de/events: no hydration warnings.
  • The PostHog script chunk still loads and requests config and flags after hydration; nothing is captured off the production host, as before.

The 114 KB entry stylesheet was the only CSS file and blocked the first paint until
it arrived. Nuxt's own inlineStyles option, set to true, writes it into the HTML instead;
compressed, the page grows by about 17 KB. A budget test holds the blocking CSS below 4 KB.
posthog-js made up two thirds of the entry chunk. A replacement for the
nuxt-posthog client plugin queues calls and loads the library on idle time
after mount, then replays them in order. Feature flags still bootstrap
from the server payload.
@TheMeinerLP
TheMeinerLP force-pushed the perf/critical-render-path branch from 56641f5 to 7a769cf Compare November 23, 2026 10:37
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
launchpad 7a769cf Commit Preview URL

Branch Preview URL
Nov 23 2026, 10:42 AM

@TheMeinerLP
TheMeinerLP merged commit f1b2d53 into main Nov 23, 2026
13 checks passed
@TheMeinerLP
TheMeinerLP deleted the perf/critical-render-path branch November 23, 2026 10:42
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