Bird
Raised Fist0
NextJSframework~10 mins

Font optimization and self-hosting in NextJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import a Google font using Next.js font optimization.

NextJS
import { [1] } from 'next/font/google';

const roboto = [1]({ subsets: ['latin'] });
Drag options to blanks, or click blank then click option'
AOpen_Sans
BInter
CLato
DRoboto
Attempts:
3 left
💡 Hint
Common Mistakes
Using a font name not exported by 'next/font/google'.
Forgetting to import the font before using it.
2fill in blank
medium

Complete the code to apply the imported font to a Next.js component.

NextJS
export default function Home() {
  return <main className=[1].className}>Hello World</main>;
}
Drag options to blanks, or click blank then click option'
Aroboto
Binter
Clato
DopenSans
Attempts:
3 left
💡 Hint
Common Mistakes
Using the font name as a string instead of the variable.
Forgetting to use .className to apply styles.
3fill in blank
hard

Fix the error in the code to self-host a local font in Next.js.

NextJS
import localFont from 'next/font/local';

const myFont = localFont({ src: '[1]' });
Drag options to blanks, or click blank then click option'
A'./fonts/my-font.woff2'
B'/fonts/my-font.woff2'
C'fonts/my-font.woff2'
D'/public/fonts/my-font.woff2'
Attempts:
3 left
💡 Hint
Common Mistakes
Including '/public' in the path which is not needed.
Using relative paths like './fonts' which won't work.
4fill in blank
hard

Fill both blanks to configure a Google font with weight and style options in Next.js.

NextJS
const inter = Inter({
  weight: ['[1]'],
  style: ['[2]'],
  subsets: ['latin'],
});
Drag options to blanks, or click blank then click option'
A400
B700
Cnormal
Ditalic
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers instead of strings for weight.
Using 'normal' style when italic is intended.
5fill in blank
hard

Fill all three blanks to create a self-hosted font with multiple weights and styles in Next.js.

NextJS
const myFont = localFont({
  src: [
    { path: '/fonts/myFont-Regular.woff2', weight: '[1]', style: '[2]' },
    { path: '/fonts/myFont-Bold.woff2', weight: '[3]', style: 'normal' },
  ],
});
Drag options to blanks, or click blank then click option'
A400
B700
Cnormal
Ditalic
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up weight numbers for regular and bold.
Using 'italic' style for bold when not intended.

Practice

(1/5)
1. What is the main benefit of self-hosting fonts in a Next.js application?
easy
A. Allows fonts to be loaded only on mobile devices
B. Automatically converts fonts to SVG format
C. Improves website speed and user privacy by serving fonts locally
D. Enables fonts to be downloaded from Google Fonts CDN

Solution

  1. Step 1: Understand what self-hosting means

    Self-hosting fonts means serving font files from your own server instead of external sources.
  2. 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.
  3. Final Answer:

    Improves website speed and user privacy by serving fonts locally -> Option C
  4. Quick Check:

    Self-hosting = faster load + privacy [OK]
Hint: Self-hosting means local font files, boosting speed and privacy [OK]
Common Mistakes:
  • Thinking self-hosting converts font formats automatically
  • Believing fonts load only on mobile devices
  • Confusing self-hosting with using external CDNs
2. Which is the correct way to import a local font using Next.js 14+ syntax?
easy
A. import localFont from 'next/font/google';
B. import { localFont } from 'next/font/google';
C. import { localFont } from 'next/font/local';
D. import localFont from 'next/font/local';

Solution

  1. Step 1: Recall Next.js font import syntax

    Local fonts are imported from 'next/font/local' as a default import.
  2. 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.
  3. Final Answer:

    import localFont from 'next/font/local'; -> Option D
  4. Quick Check:

    Local fonts use default import from 'next/font/local' [OK]
Hint: Local fonts import default from 'next/font/local' [OK]
Common Mistakes:
  • Using named imports instead of default import
  • Importing local fonts from 'next/font/google'
  • Confusing Google fonts import with local fonts
3. Given this code snippet in Next.js, what will be the font-family applied to the <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>
  );
}
medium
A. The font-family will be 'MyFont' loaded from the local file
B. The font-family will be the browser default because variable is unused
C. The font-family will be 'var(--my-font)' as a literal string
D. The code will throw a runtime error due to invalid style

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The font-family will be 'MyFont' loaded from the local file -> Option A
  4. Quick Check:

    CSS variable font applied = local font used [OK]
Hint: CSS variable holds font name, style uses var() to apply it [OK]
Common Mistakes:
  • Thinking 'var(--my-font)' is a literal font name
  • Assuming variable className is unused
  • Believing code throws error due to style object
4. Identify the error in this Next.js font self-hosting code snippet:
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>;
}
medium
A. The fontFamily style should use 'var(--font)' instead of '--font'
B. The src path must be an absolute URL, not relative
C. localFont does not support .ttf files, only .woff2
D. className should be applied to <main>, not <div>

Solution

  1. Step 1: Check fontFamily style syntax

    CSS variables must be used with var(), so fontFamily should be 'var(--font)', not '--font'.
  2. Step 2: Verify other parts

    The src path can be relative, .ttf is supported, and className can be on any element.
  3. Final Answer:

    The fontFamily style should use 'var(--font)' instead of '--font' -> Option A
  4. Quick Check:

    CSS variable usage requires var() [OK]
Hint: Use var(--variable) syntax for CSS variables in style [OK]
Common Mistakes:
  • Omitting var() around CSS variable in style
  • Thinking relative paths are invalid
  • Assuming .ttf fonts are unsupported
5. You want to optimize font loading in a Next.js 14 app by self-hosting multiple font weights of 'OpenSans'. Which approach correctly applies different weights using next/font/local and CSS variables?
hard
A. Load all font files manually in _app.js without using next/font/local
B. Configure a single localFont with src as an array specifying path and weight for each file, assign a single CSS variable
C. Use next/font/google for all weights and ignore local hosting
D. Import only one font weight and use CSS font-weight property to simulate others

Solution

  1. 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'.
  2. 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.
  3. 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.
  4. Final Answer:

    Configure a single localFont with src as an array specifying path and weight for each file, assign a single CSS variable -> Option B
  5. Quick Check:

    src array + single variable = correct multi-weight self-hosting [OK]
Hint: Single localFont src array for weights + shared CSS variable [OK]
Common Mistakes:
  • Trying to fake font weights with CSS alone
  • Using Google fonts instead of self-hosting
  • Manually loading fonts without next/font/local benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard