Skip to content

[docs] Fix Next.js font optimization to preserve typography letter-spacing#48488

Open
starboyvarun wants to merge 7 commits into
mui:masterfrom
starboyvarun:47575-typography-docs-fix
Open

[docs] Fix Next.js font optimization to preserve typography letter-spacing#48488
starboyvarun wants to merge 7 commits into
mui:masterfrom
starboyvarun:47575-typography-docs-fix

Conversation

@starboyvarun
Copy link
Copy Markdown
Contributor

Closes #47575

Problem

The current Next.js font optimization docs recommend using typography.fontFamily: 'var(--font-roboto)' in the theme. This silently breaks Roboto's letter-spacing values for all typography variants.

createTypography only applies letter-spacing when fontFamily strictly equals the default Roboto font stack string ('"Roboto", "Helvetica", "Arial", sans-serif'). A CSS variable (var(--font-roboto)) never matches that string, so all variants lose their tuned letter-spacing.

Solution

Update both the App Router and Pages Router font optimization examples to use roboto.className on the root element instead of a CSS variable. This:

  • Leaves typography.fontFamily at its default value → letter-spacing check passes ✓
  • Next.js still self-hosts and preloads Roboto at build time, so the browser uses it for any element that requests "Roboto" — including Material UI components ✓
  • Simpler setup: no custom theme.ts file needed just for fontFamily

A :::warning callout is added to both sections to explain the CSS variable limitation for users who intentionally override fontFamily.

As suggested by @siriwatknp when closing #48474.

…acing

Using `typography.fontFamily: 'var(--font-roboto)'` in the theme causes
Material UI to skip Roboto-specific letter-spacing values. This is because
createTypography only applies them when fontFamily strictly equals the
default Roboto stack string, and a CSS variable never matches that check.

Update both App Router and Pages Router font optimization examples to use
`roboto.className` on the root element instead. This leaves the theme
fontFamily at its default value, preserving all Roboto letter-spacing,
while Next.js still self-hosts and preloads the font at build time.

Adds a warning callout to explain the CSS variable limitation for users
who intentionally override fontFamily.

Closes mui#47575
@code-infra-dashboard
Copy link
Copy Markdown

code-infra-dashboard Bot commented May 6, 2026

Deploy preview

Bundle size

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

…uide

Use non-breaking spaces in all 'Material UI' brand name occurrences
added in the font optimization sections.
@zannager zannager added the docs Improvements or additions to the documentation. label May 7, 2026
@zannager zannager requested a review from siriwatknp May 7, 2026 12:34
Copy link
Copy Markdown
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for the default roboto font but using variable still required for custom font right?

If yes, I think a new section "Custom font" should be added with the variable approach.

### Font optimization

…what you have updated

### Custom font

…
const theme = createTheme({
  typography: {
    fontFamily: 'var(--font-geist)',
  },
});
…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to the documentation. nextjs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

next/font recommendation breaks letter-spacing

4 participants