Portfolio
Prerendering a React app without a framework: what fixed LCP, CLS and a 286 KB bundle
How prerendering a Vite React SPA with renderToString, safe hydration, font fixes and per-language chunks took a portfolio from 79 on a phone to fast.
In short
Render the first screen at build time with React's renderToString, and hydrate only when the visitor matches what was prerendered; render fresh otherwise. Then fix what still blocks the first paint: font CSS that downloads every subset, an animation that Largest Contentful Paint can't see, and a bundle carrying six languages (286 KB down to 123 KB). Layout shift went from 0.041 to 0.
My portfolio, farhad-rezaei.com, is a React and TypeScript single-page app built with Vite. It's published in six languages, including Persian and Arabic, and its first screen is a small piece of theatre: a canvas dragon breathes fire across the page while my name burns in, letter by letter.
On a phone, that first screen cost more than it looked. Lighthouse scored the live home page 79 for performance on 2 November: 3.4 seconds before the largest text appeared, 240 ms of blocked main thread, and a layout that jumped when the fonts arrived. For a recruiter opening the link on a train, that meant three seconds of a dark panel before learning whose site it was.
Think of a restaurant where the menu isn't on the table. You sit down and wait until a waiter arrives and reads it to you. That's a client-rendered page: nothing readable exists until the JavaScript has downloaded, parsed and run. The usual fix is to print the menu and leave it on the table, so the waiter only has to take the order. That was my plan too, but the trace showed more standing between the guest and the menu.
Following one load on a phone
In a throttled mobile run, the browser downloads an index.html whose root is empty, plus a stylesheet that blocks rendering. It downloads the JavaScript, about 286 KB of it the app itself, because every language's copy ships in the same bundle. On a slowed-down phone CPU, evaluating that main chunk takes 4.3 seconds. React renders the page. The fonts start downloading, the Latin and the Latin Extended file for every weight, and the text reflows when they land. Then the headline letters begin their animation from opacity 0.
Each step waits on the one before it.
Printing the menu, without Next.js
I didn't want to move the site to a framework for one page, so the build renders the home page itself. After the normal Vite build, a second vite build --ssr produces a server bundle of the same app, and a small script calls React's renderToString and writes the result into dist/index.html. The page is readable before a single script runs, and search engines and link previews get real text.
The tricky part is hydration. React can only adopt server HTML that matches what it would render itself, and this page depends on three things the build can't know: the visitor's theme, their language and whether they've asked for reduced motion. So the build renders the defaults, and the browser decides:
const matchesPrerender = () =>
location.pathname === '/' &&
!document.documentElement.classList.contains('dark') &&
initialLanguage() === 'en' &&
!matchMedia('(prefers-reduced-motion: reduce)').matches;
if (root.hasChildNodes() && matchesPrerender()) hydrateRoot(root, app);
else { root.replaceChildren(); createRoot(root).render(app); }
A visitor who matches gets hydration. Anyone else gets a fresh render over HTML they could already read, which is what every visitor got before. I preferred that to hydrating everyone and letting React recover from mismatches, because recovery logs errors and throws the work away anyway.
What still stood in the way
With the menu printed, the remaining delays were about what arrived after it.
The font problem was the surprising one. Fontsource's per-subset stylesheets, the latin-ext-400.css kind, declare their face without a unicode-range, so the browser downloaded every subset and the last one declared won for all text. The whole-weight files declare each subset with its range, so switching to them cut the downloads to what the page uses. I preload the three faces the first screen paints with, and reshape the fallback fonts with size-adjust and ascent-override to match Roboto's metrics, so text drawn before a webfont arrives takes the same space. Cumulative Layout Shift went from 0.041 to 0, and it has been 0 in every run since.
The headline was a measurement trap. Chrome doesn't count an element at opacity 0 as painted, and my letters stay invisible for about 1.6 seconds until the fire reaches them, so Largest Contentful Paint waited for the animation. Starting them at opacity 0.001 looks identical and lets the metric see text that's genuinely on screen. That changes what the metric measures, not what a reader sees, so I fixed the reader's side as well: the line a skimmer needs most, my job titles and one line of proof, is plain text that's there on the first paint.
Then the scripts. Because the HTML is complete, the app is an enhancement, so the home page starts its bundle after the load event, the fonts and one painted frame. Each language other than English moved into its own chunk, fetched before the first render when it's needed, and the main bundle dropped from 286 KB to 123 KB uncompressed. The dragon's canvas setup, the heaviest work on the page, waits for an idle moment, and its clock starts at first paint so it still breathes fire on the letters as they burn in. The cost is a short window where the theme switch and the mobile menu aren't wired up yet.
How I checked it
Lab numbers from a busy laptop are noisy: three Lighthouse mobile runs of the deployed page scored 79, 94 and 99. So I lean on what doesn't move with that noise: zero layout shift in every run, the bundle size, and the best run's 1.7-second LCP with 47 ms of blocking time.
Desktop scores 100. On a local production build, Chrome's trace records the largest paint about 0.2 seconds after navigation. For the hydration rule, I loaded the page in English, French and Persian in Chrome driven by Playwright and checked that each rendered fully with no hydration errors.
What carries over
- Before tuning JavaScript, ask whether the first screen needs it at all. Prerendering one route doesn't need a framework.
- Hydrate only when you can show the markup matches, and render fresh when you can't.
- Check what your font CSS actually downloads, not what you think you imported.
- Know what your metric counts. An animation can make a fast page look slow, and the honest fix makes the important text visible, not only measurable.
- Treat a single lab score as one sample from a range.
For the person opening the link, the page now says who I am and what I do before any script runs. Underneath, that's a build step, a hydration rule and a loading order, and each one fails safely: a visitor the prerender doesn't match still gets the whole page, a beat later, exactly as before.
Further reading
- Largest Contentful Paint, web.devweb.dev
- CSS size-adjust for @font-face, web.devweb.dev
- hydrateRoot, React documentationreact.dev
- Lighthouse performance scoring, Chrome for Developersdeveloper.chrome.com
More writing
-
Don't let the model convert the calendar: Jalali dates in an MCP server for a gold inventory
Why a gold inventory's MCP server converts Jalali dates itself, rejects days that don't exist, and returns every date in both calendars to AI agents.

-
A cold start that draws last launch: persisting the TanStack Query cache in a mobile word game
How ClueShot restores last launch's TanStack Query cache as stale, checks whose it is, and revalidates with ETags so a cold start never opens empty.

-
Two failure handlers, one refund: idempotent cleanup in an async AI image pipeline
How a BullMQ image worker, its failed event and a Telegram bot all reach one refund in MrPicasso, and how two Redis claims keep it to one credit.
