
- Next.js - Home
- Next.js - Overview
- Next.js - Project Setup
- Next.js - Folder Structure
- Next.js - App Router
- Next.js - Page Router
- Next.js Features
- Next.js - Pages
- Next.js - Data Fetching
- Next.js - ISR
- Next.js - Static File Serving
- Next.js - Pre-Rendering
- Next.js - Partial Pre Rendering
- Next.js - Server Side Rendering
- Next.js - Client Side Rendering
- Next.js Routing
- Next.js - Routing
- Next.js - Nested Routing
- Next.js - Dynamic Routing
- Next.js - Parallel Routing
- Next.js - Imperative Routing
- Next.js - Shallow Routing
- Next.js - Intercepting Routes
- Next.js - Redirecting Routes
- Next.js - Navigation and Linking
- Next.js Configuration
- Next.js - TypeScript
- Next.js - Environment Variables
- Next.js - File Conventions
- Next.js - ESLint
- Next.js API & Backend
- Next.js - API Routes
- Next.js - Dynamic API Routes
- Next.js - Route Handlers
- Next.js - API MiddleWares
- Next.js - Response Helpers
- Next.js API Reference
- Next.js - CLI Commands
- Next.js - Functions
- Next.js - Directives
- Next.js - Components
- Next.js - Image Component
- Next.js - Font Component
- Next.js - Head Component
- Next.js - Form Component
- Next.js - Link Component
- Next.js - Script Component
- Next.js Styling & SEO
- Next.js - CSS Support
- Next.js - Global CSS Support
- Next.js - Meta Data
- Next.js Advanced Topics
- Next.js - Error Handling
- Next.js - Server Actions
- Next.js - Fast Refresh
- Next.js - Internationalization
- Next.js - Authentication
- Next.js - Session Management
- Next.js - Authorization
- Next.js - Caching
- Next.js - Data Caching
- Next.js - Router Caching
- Next.js - Full Route Caching
- Next.js - Request Memoization
- Next.js Performance Optimization
- Next.js - Optimizations
- Next.js - Image Optimization
- Next.js - Lazy Loading
- Next.js - Font Optimization
- Next.js - Video Optimization
- Next.js - Script Optimization
- Next.js - Memory Optimization
- Next.js - Using OpenTelemetry
- Next.js - Package Bundling Optimization
- Next.js Testing
- Next.js - Testing
- Next.js - Testing with Jest
- Next.js - Testing with Cypress
- Next.js - Testing with Vitest
- Next.js - Testing with Playwright
- Next.js Debugging & Deployment
- Next.js - Debugging
- Next.js - Deployment
- Next.js Useful Resources
- Next.js - Interview Questions
- Next.js - Quick Guide
- Next.js - Useful Resources
- Next.js - Discussion
Next.js - Font Optimization
Font Optimization in Next.js
Next.js provides a built-in library called 'next/font' to optimize usage of fonts in your application. This library also comes with automatic self-hosting for any local font file. This means you can optimally load web fonts with zero layout shift.
The Next.js font system allows you to easily use all Google Fonts with improved performance and privacy features. The font files are downloaded at build time and self-hosted with the rest of the static assets. Hence, your browser need not have to make additional requests to fetch the font files.
Using Fonts in Next.js
In Next.js, we can add web hosted fonts (ie, google fonts) or local fonts to application. To add a Google Font in your Next.js application, first import the specific Google Font from next/font/google, then use the returned className in your component. See the code below:
import { Inter } from 'next/font/google'; // Configure the font const inter = Inter({ subsets: ['latin'], // Subsets to include weight: '400', // Font weight (optional) }); export default function Home() { return ( <div className={inter.className}> <h1>Hello, Next.js with Inter font!</h1> </div> ); }
Output
The below image shows output of above code. The font applied to text in current component only.

Preloading Fonts in Next.js
When a font function is called on a page of your site, it may not be available globally on all routes. The preloading of fonts on other routes depend on the type of file where the font is used. If the font is used in a -
- Layout Page:(/app/layout.tsx) The font will be preloaded on all routes wrapped by the layout.
- Component Page: (/app/component/page.tsx) The font will be loaded on the component page only.
- Root Page: (/app/index.tsx) The font will be loaded on all routes.
Example
In the code below, we defined a component page (about/page.tsx) and layout page of Next.js. In the component page we are using google fonts.
// File - /app/about/page.tsx import { Inter } from 'next/font/google'; // Configure the font const inter = Inter({ subsets: ['latin'], // Subsets to include weight: '400', // Font weight (optional) }); export default function Home() { return ( <div className={inter.className}> <h1>Hello, Next.js with Inter font!</h1> </div> ); } // File - /app/layout.tsx import Link from 'next/link' // import linking component import { ReactNode } from 'react'; export default function RootLayout({ children }: RootLayoutProps) { return ( <html lang="en"> <h1>Next JS Fonts</h1> <ul> <li><Link href="/">Home</Link></li> <li><Link href="/contacts/">Contacts</Link></li> <li><Link href="/about">About Us</Link></li> </ul> <main>{children}</main> </html> ); }
Output
In the output, we can see a new font call is made at backend when we visit /about page of application.

Use Local Fonts in Next.js
To use a locally hosted fonts, use the next/font/local library. Provide the path to the font files using src argument. See the code below.
import localFont from 'next/font/local'; // Configure the local font const myFont = localFont({ src: './path/to/font.woff2', // Path to the font file weight: '400', // Optional weight style: 'normal', // Optional style }); export default function Home() { return ( <div className={myFont.className}> <h1>Hello, Next.js with Inter font!</h1> </div> ); }
Output
The image below shows output of above code. Custom font are used for h1 element.
