r/reactjs • u/ajaypatel9016 • 4d ago
Resource How I load fonts for a first-screen hero in React 19 without a flash (preconnect + critical/late split)
I ran into this while building a hero with a canvas animation. The headline font has to be there on the first paint or the whole screen visibly jumps, but loading every font up front slows the page. What worked for me was splitting fonts into two groups.
- Critical fonts: the ones the first screen can’t look right without (headline and UI). I render these as a stylesheet link with a precedence prop, so React treats the stylesheet specially.
- Late fonts: decorative ones that only appear after interaction or in small labels. I load these after mount, so they never block the first paint.
import { preconnect } from 'react-dom'
import { LateFonts } from './late-fonts'
const CRITICAL_FONTS =
'https://fonts.googleapis.com/css2?family=Archivo:wght@800&display=swap'
const LATE_FONTS =
'https://fonts.googleapis.com/css2?family=Martian+Mono:wght@400&display=swap'
export default function Hero() {
// React places these hints in <head>, ahead of the stylesheet
preconnect('https://fonts.googleapis.com')
preconnect('https://fonts.gstatic.com', { crossOrigin: 'anonymous' })
return (
<>
<link rel="stylesheet" href={CRITICAL_FONTS} precedence="default" />
<LateFonts href={LATE_FONTS} />
<main>{/* hero content */}</main>
</>
)
}
'use client'
import { useEffect } from 'react'
export function LateFonts({ href }: { href: string }) {
useEffect(() => {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = href
document.head.appendChild(link)
return () => link.remove()
}, [href])
return null
}Things to watch:
precedenceis what makes React handle the stylesheet specially. Without it, you get a plain<link>.- A component that renders a stylesheet link with
precedencesuspends until the stylesheet loads, so decide where your Suspense boundary sits. preconnectonly helps if you call it while rendering.- I left out the late fonts’ fallback styling. If a late font changes layout, you may still see a small shift.
Curious how others split font loading for first-screen sections, or whether you just self-host everything.