What is the main purpose of using next/font in a Next.js project?
The main purpose is to optimize font loading by automatically handling font subsets, preloading, and reducing layout shifts, which improves page speed and user experience.
Click to reveal answer
intermediate
How does next/font help reduce layout shifts caused by fonts?
It preloads fonts and uses font subsets to ensure text renders quickly and consistently, preventing visible changes in text size or style after the page loads.
Click to reveal answer
beginner
Which font types can be optimized using next/font?
You can optimize Google Fonts, local fonts, and custom fonts by importing them through next/font/google or next/font/local.
Click to reveal answer
intermediate
What is the benefit of using font subsets in next/font?
Font subsets load only the characters needed for your content, which reduces font file size and speeds up page loading.
Click to reveal answer
beginner
Show a simple example of importing and using a Google Font with next/font.
Example:
<pre>import { Roboto } from 'next/font/google';
const roboto = Roboto({ subsets: ['latin'], weight: '400' });
export default function Home() {
return <main className={roboto.className}>Hello with Roboto font!</main>;
}</pre>
Click to reveal answer
What does next/font automatically do to improve font loading?
APreloads fonts and uses subsets
BConverts fonts to images
CRemoves all fonts from the page
DLoads fonts only after user clicks
✗ Incorrect
next/font preloads fonts and uses subsets to speed up loading and reduce layout shifts.
Which import would you use to load a Google Font with next/font?
Aimport Roboto from 'next/font/remote';
Bimport Roboto from 'next/font/local';
Cimport { Roboto } from 'next/font/custom';
Dimport { Roboto } from 'next/font/google';
✗ Incorrect
Google Fonts are imported from 'next/font/google'.
Why are font subsets useful in next/font?
AThey load only needed characters to reduce file size
BThey add extra characters for all languages
CThey disable font loading
DThey convert fonts to SVG
✗ Incorrect
Subsets reduce font file size by loading only required characters.
What is a key benefit of using next/font over traditional font loading?
AFonts are embedded as base64 images
BFonts load slower but look better
CImproved performance and less layout shift
DFonts are only loaded on mobile devices
✗ Incorrect
next/font improves performance and reduces layout shifts by smart font loading.
How do you apply a font loaded with next/font to a component?
ASet the font in a CSS file manually
BUse the font's <code>className</code> on an element
CFonts apply automatically without code
DUse inline styles with font URL
✗ Incorrect
You apply the font by adding the font's className to your element.
Explain how next/font improves font loading and user experience in Next.js.
Think about speed and how fonts appear on the page.
You got /4 concepts.
Describe the steps to import and use a Google Font with next/font in a Next.js component.
Focus on import, configuration, and usage.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using next/font in a Next.js application?
easy
A. To convert fonts into images for display
B. To create custom font files from scratch
C. To optimize font loading for better performance and user experience
D. To disable all fonts and use system defaults only
Solution
Step 1: Understand the role of next/font
next/font is designed to load fonts efficiently in Next.js apps, improving speed and user experience.
Step 2: Compare options with this purpose
Only To optimize font loading for better performance and user experience describes optimization of font loading, which matches the purpose of next/font.
Final Answer:
To optimize font loading for better performance and user experience -> Option C
Quick Check:
Font optimization = To optimize font loading for better performance and user experience [OK]
Hint: Remember: next/font improves font loading speed [OK]
Common Mistakes:
Thinking next/font creates fonts
Confusing font optimization with image conversion
Assuming next/font disables fonts
2. Which of the following is the correct way to import the Inter font from Google Fonts using next/font/google?
easy
A. import Inter from 'next/font/google'; const inter = new Inter();
B. import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] });
C. const inter = require('next/font/google').Inter();
D. import { Inter } from 'next/font/local'; const inter = Inter();
Solution
Step 1: Check the import syntax for Google Fonts
The correct import uses named import from 'next/font/google' and calls the font function with options.
Step 2: Validate the usage
import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); matches the correct pattern: import with braces, call with options object.
Final Answer:
import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); -> Option B
Quick Check:
Correct import and call = import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); [OK]
Hint: Use named import and call font as a function with options [OK]
Common Mistakes:
Using default import instead of named import
Using require instead of import
Importing from 'next/font/local' for Google Fonts
3. Given this code snippet in a Next.js component:
import { Inter } from 'next/font/google';
const inter = Inter({ subsets: ['latin'] });
export default function Home() {
return <main className={inter.className}>Hello World</main>;
}
What will be the effect of using inter.className in the <main> element?
medium
A. It adds a random CSS class unrelated to fonts
B. It causes a runtime error because className is undefined
C. It applies default system fonts ignoring Inter
D. It applies the Inter font styles optimized and loaded by next/font
Solution
Step 1: Understand what inter.className provides
The Inter font function returns an object with a className property that applies the optimized font styles.
Step 2: Check usage in the component
Using className={inter.className} applies the Inter font styles to the <main> element.
Final Answer:
It applies the Inter font styles optimized and loaded by next/font -> Option D
Quick Check:
inter.className applies optimized font styles = It applies the Inter font styles optimized and loaded by next/font [OK]
Hint: className from next/font applies the font styles [OK]
Common Mistakes:
Assuming className is undefined
Thinking it applies system fonts instead
Confusing className with inline styles
4. Identify the error in this code snippet that tries to load a local font with next/font/local:
B. The weight property should be a number, not a string
C. The import should use braces: { localFont }
D. The style property is not supported in local fonts
Solution
Step 1: Check the src property format for localFont
Local fonts require src to be an array of objects with path and weight, not a single string.
Step 2: Validate other properties
Weight can be string or number, import default is correct, style is supported.
Final Answer:
The src path must be an array, not a string -> Option A
Quick Check:
Local font src requires array = The src path must be an array, not a string [OK]
Hint: Local font src must be an array of objects [OK]
Common Mistakes:
Using string instead of array for src
Misusing import syntax with braces
Assuming style is unsupported
5. You want to load two Google Fonts, Roboto and Open Sans, both with latin subsets, and apply them to different parts of your Next.js page. Which approach correctly uses next/font/google to optimize both fonts and apply them separately?