Fonts make your website look nice and readable. Optimizing and self-hosting fonts helps your site load faster and work well even without internet.
Font optimization and self-hosting in NextJS
import localFont from 'next/font/local'; const myFont = localFont({ src: [ { path: './fonts/MyFont-Regular.woff2', weight: '400', style: 'normal' }, { path: './fonts/MyFont-Bold.woff2', weight: '700', style: 'normal' } ], variable: '--my-font' });
Use next/font/local to load fonts stored in your project folder.
Define font weights and styles to match your font files for better control.
import localFont from 'next/font/local'; const roboto = localFont({ src: './fonts/Roboto-Regular.woff2', weight: '400', style: 'normal' });
import localFont from 'next/font/local'; const openSans = localFont({ src: [ { path: './fonts/OpenSans-Regular.woff2', weight: '400', style: 'normal' }, { path: './fonts/OpenSans-Italic.woff2', weight: '400', style: 'italic' } ], variable: '--open-sans' });
This Next.js component uses a self-hosted font loaded with next/font/local. The font is applied using a CSS variable for consistent styling.
import localFont from 'next/font/local'; const myFont = localFont({ src: [ { path: './fonts/MyFont-Regular.woff2', weight: '400', style: 'normal' }, { path: './fonts/MyFont-Bold.woff2', weight: '700', style: 'normal' } ], variable: '--my-font' }); export default function Home() { return ( <main className={myFont.className} style={{ fontFamily: 'var(--my-font)' }}> <h1>Hello, this text uses a self-hosted font!</h1> <p>Font optimization helps this page load faster and look consistent.</p> </main> ); }
Always include multiple font weights and styles if your design needs them.
Place font files in the public/fonts or a similar folder accessible by your app.
Using next/font automatically optimizes fonts for best performance.
Self-hosting fonts improves speed and privacy.
Next.js provides next/font/local to easily load local fonts.
Use CSS variables to apply fonts consistently across your app.
Practice
Solution
Step 1: Understand what self-hosting means
Self-hosting fonts means serving font files from your own server instead of external sources.Step 2: Identify benefits of self-hosting in Next.js
This improves loading speed because fonts are fetched faster and enhances privacy by not contacting third-party servers.Final Answer:
Improves website speed and user privacy by serving fonts locally -> Option CQuick Check:
Self-hosting = faster load + privacy [OK]
- Thinking self-hosting converts font formats automatically
- Believing fonts load only on mobile devices
- Confusing self-hosting with using external CDNs
Solution
Step 1: Recall Next.js font import syntax
Local fonts are imported from 'next/font/local' as a default import.Step 2: Check option correctness
import localFont from 'next/font/local'; uses default import from 'next/font/local', which is correct. Options A, B, and D incorrectly use 'google' path or named imports.Final Answer:
import localFont from 'next/font/local'; -> Option DQuick Check:
Local fonts use default import from 'next/font/local' [OK]
- Using named imports instead of default import
- Importing local fonts from 'next/font/google'
- Confusing Google fonts import with local fonts
<main> element?import localFont from 'next/font/local';
const myFont = localFont({
src: './fonts/MyFont.woff2',
variable: '--my-font',
});
export default function Home() {
return (
<main className={myFont.variable} style={{ fontFamily: 'var(--my-font)' }}>
Hello World
</main>
);
}Solution
Step 1: Understand localFont usage with CSS variable
The localFont function creates a CSS variable '--my-font' that holds the font-family name for the loaded font.Step 2: Analyze how fontFamily is applied
The style uses fontFamily: 'var(--my-font)', which correctly applies the font loaded and assigned to the CSS variable.Final Answer:
The font-family will be 'MyFont' loaded from the local file -> Option AQuick Check:
CSS variable font applied = local font used [OK]
- Thinking 'var(--my-font)' is a literal font name
- Assuming variable className is unused
- Believing code throws error due to style object
import localFont from 'next/font/local';
const myFont = localFont({
src: './fonts/MyFont.ttf',
variable: '--font',
});
export default function Page() {
return <div className={myFont.className} style={{ fontFamily: '--font' }}>Text</div>;
}Solution
Step 1: Check fontFamily style syntax
CSS variables must be used with var(), so fontFamily should be 'var(--font)', not '--font'.Step 2: Verify other parts
The src path can be relative, .ttf is supported, and className can be on any element.Final Answer:
The fontFamily style should use 'var(--font)' instead of '--font' -> Option AQuick Check:
CSS variable usage requires var() [OK]
- Omitting var() around CSS variable in style
- Thinking relative paths are invalid
- Assuming .ttf fonts are unsupported
next/font/local and CSS variables?Solution
Step 1: Understand multiple font weights with localFont
Multiple weights are handled in one localFont call using src: [{ path: './OpenSans-Regular.woff2', weight: '400' }, { path: './OpenSans-Bold.woff2', weight: '700' }], with a single CSS variable like '--font-opensans'.Step 2: Combine variables in CSS or className for usage
Apply the font className or variable to an ancestor element, then use font-family: var(--font-opensans) with font-weight in styles.Step 3: Evaluate other options
Import only one font weight and use CSS font-weight property to simulate others is incorrect because CSS font-weight cannot simulate different font files. Use next/font/google for all weights and ignore local hosting ignores self-hosting. Load all font files manually in _app.js without using next/font/local is manual and not optimized.Final Answer:
Configure a single localFont with src as an array specifying path and weight for each file, assign a single CSS variable -> Option BQuick Check:
src array + single variable = correct multi-weight self-hosting [OK]
- Trying to fake font weights with CSS alone
- Using Google fonts instead of self-hosting
- Manually loading fonts without next/font/local benefits
Ready to prove it?
Pass the test and this pattern moves unknown
