Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 04 / Topic 4 of 8

A font scoped to one section

"Fonts are scoped to the component they're used in" is the same guide's next sentence. app/store/_components/StoreNav.tsx — a Client Component that, by construction (Part 2's colocation rules), only ever renders under /store — loads a second Google Font, Poppins, used nowhere outside it:

import { Poppins } from "next/font/google";

const poppins = Poppins({ subsets: ["latin"], weight: "600" });

// ...
<nav style={{ display: "flex", gap: "1rem", padding: "1rem 2rem 0", fontFamily: poppins.style.fontFamily }}>

Proof that this really is scoped, not merely described: the root page's Link preload header (section 3) carries exactly two font files. /store's carries three:

$ curl -s -D - -o /dev/null http://localhost:PORT/store | grep -i "^Link:"
Link: </_next/static/media/07a39a8a4660cc7c-s.p.0r6juujl39pe6.woff2>; rel=preload; as="font"; crossorigin=""; type="font/woff2", </_next/static/media/605ce01b664af129-s.p.0wgildi0cnwt9.woff2>; rel=preload; as="font"; crossorigin=""; type="font/woff2", </_next/static/media/6bafae418e8a1b08-s.p.3o_v2fun1jzxk.woff2>; rel=preload; as="font"; crossorigin=""; type="font/woff2"

The first two entries are the same two Geist files from the root layout, inherited by every page including this one; the third is Poppins, present only because StoreNav — which only ever renders on /store/* — imports it. A page that never imports StoreNav never gets a third preload, which is exactly what section 3's two-file header on / already showed.

This harness has no custom local font file to self-host, so next/font/local itself — "import the localFont function from next/font/local and specify the src of your local font file... fonts can be stored anywhere in the project, including the public folder or co-located inside the app folder," per the installed package's reference — is described here from that reference rather than built: the mechanism is identical to next/font/google (call it, get back .className/.style/.variable), the only difference being src instead of a font family name.

Full file: examples/03-store-nav-scoped-font.tsx.

Done here? The practice set for Part 4 — including the solutions — is on the part page.

Search the guide