Skip to content
Styling & OptimizationChapter 27 / 34Beginner5 min read

Font Optimization

Next.js next/font se Google aur local fonts self-host karna, layout shift rokna aur Tailwind ke saath CSS variables use karna, Roman Urdu mein.

On this page

1. Font Optimization kya hai?

Is ke do hisse hain:

  • next/font/google: Google Fonts ka koi bhi font, lekin request Google ko browser se nahi jati.
  • next/font/local: Aap ki apni .woff2 files.

2. Kyun zaroori hai?

Normal tareeqe mein link tag se Google Fonts load hota hai. Browser pehle HTML parhta hai, phir CSS, phir font file. Jab tak font aaye, text fallback font mein dikhta hai aur phir achanak badal jata hai.

link tag se Google Fontsnext/font
Browser se Google ke server ko extra requestFont aap ke domain se serve hota hai
Font aane par text jhatke se badalta haisize-adjust wala fallback, layout shift kam
User ka IP Google ko jata haiKoi third party request nahi
Saare weights aur subsets loadSirf chune hue subsets aur weights

3. Kahan use hota hai?

SituationApproach
Poori site ka main fontRoot layout mein next/font/google
Brand ka custom paid fontnext/font/local
Headings aur body ke alag fontsDo fonts, dono ke CSS variables
Code blocks ke liye monospaceGeist_Mono ya JetBrains_Mono variable ke saath

4. Kaise kaam karta hai?

next/font ka flow
Layout mein font function call
next build font files download karta hai
Files static assets ke saath bundle hoti hain
Unique className ya CSS variable generate
Browser font aap ke domain se hi load karta hai

Font function module ke top level par call hota hai. Jo object milta hai us mein className, style aur (agar variable diya ho) variable hota hai. Inhein aap html ya kisi element par laga dete hain.

5. Code example

Google font root layout mein

tsx
import { Inter, Roboto_Mono } from "next/font/google";
import "./globals.css";

const inter = Inter({
  subsets: ["latin"],
  display: "swap",
  variable: "--font-inter",
});

const robotoMono = Roboto_Mono({
  subsets: ["latin"],
  variable: "--font-roboto-mono",
});

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" className={`${inter.variable} ${robotoMono.variable}`}>
      <body className="font-sans">{children}</body>
    </html>
  );
}

Tailwind v4 mein variable connect karna

css
@import "tailwindcss";

@theme inline {
  --font-sans: var(--font-inter);
  --font-mono: var(--font-roboto-mono);
}

Local font

tsx
import localFont from "next/font/local";

const brand = localFont({
  src: [
    { path: "./fonts/Brand-Regular.woff2", weight: "400", style: "normal" },
    { path: "./fonts/Brand-Bold.woff2", weight: "700", style: "normal" },
  ],
  variable: "--font-brand",
});

export default function Heading({ text }: { text: string }) {
  return <h1 className={brand.className}>{text}</h1>;
}

Code kaise kaam kar raha hai?

CheezMatlab
Inter(...)Google font loader, naam mein space ho to underscore (Roboto_Mono)
subsetsSirf zaroori characters (jaise latin) preload hote hain
variableCSS variable banata hai jo Tailwind ya CSS mein use ho sake
display: "swap"Font aane tak fallback text dikhta hai (default bhi yahi hai)
@theme inlineTailwind ki font-sans utility ko font variable se jorta hai
localFontApni files, src path font loader file ke relative

6. Common mistakes

Galati: Font function component ke andar call karna

Why? Font loaders module scope mein call hone chahiye, warna har render par naya instance banta hai aur build error aata hai.

Behtar: Font ko file ke top par const mein define karo aur component mein sirf className ya variable use karo.

Galati: Har page mein alag se same font load karna

Why? Ek hi font ke kai instances ban jate hain aur extra CSS aur font files load hoti hain.

Behtar: Font ko ek shared file jaise app/fonts.ts mein define karo ya root layout mein lagao, phir wahin se import karo.

Galati: Layout mein Google Fonts ka link tag bhi rakhna

Why? Font do baar load hota hai aur third party request wapas aa jati hai, next/font ka faida khatam.

Behtar: Purana link tag hata do, sirf next/font use karo.

Interview tip

"next/font CLS kaise kam karta hai?" Jawab: yeh fallback font par size-adjust jaisi properties automatically set karta hai taake fallback aur asal font ka size qareeb qareeb same ho, is liye font swap par text nahi khiskta.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Google fontsnext/font/google, self-hosted at build time
Apni filesnext/font/local aur src
Tailwind ke saathvariable option aur @theme inline
Kahan defineModule scope, aksar root layout
FaidaNo external request, kam CLS