Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs for experimental font optimization adjustFontFallbacks #40771

Merged
merged 55 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
cc484c5
Adding experimentalAdjustFallback feture to font optimization
janicklas-ralph Sep 2, 2022
c413060
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 6, 2022
203fde2
Fix lint
janicklas-ralph Sep 6, 2022
6cb2099
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 8, 2022
051f486
Removing serverless tests since target: serverless is being deprecated
janicklas-ralph Sep 8, 2022
1f4cfa6
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 8, 2022
be23651
Removing serverless tests since target: serverless is being deprecated
janicklas-ralph Sep 8, 2022
3a44f45
Adding font override test case
janicklas-ralph Sep 8, 2022
5f31377
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 8, 2022
8097ac8
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 13, 2022
680ea2a
Addressing review comments. Fixing failing tests
janicklas-ralph Sep 13, 2022
eda0789
Adding serverless tests back. Fixed serverless tests
janicklas-ralph Sep 13, 2022
f9c39c3
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 13, 2022
8cb7714
update font-metrics to JSON
ijjk Sep 14, 2022
16e2df4
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 15, 2022
0b2da36
Merge branch 'font-override' of github.com:janicklas-ralph/next.js in…
janicklas-ralph Sep 15, 2022
9ef2d1c
Fix types
janicklas-ralph Sep 15, 2022
cb2837f
Fix config schema
janicklas-ralph Sep 15, 2022
4d55860
Making optimizefonts backward compatible, work with either bool or ob…
janicklas-ralph Sep 15, 2022
883492c
Fix json import error
janicklas-ralph Sep 15, 2022
c5cbe14
usMerge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 15, 2022
92b9491
Changed the Fontconfig types to handle boolean values for backward co…
janicklas-ralph Sep 15, 2022
a682cf5
t Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 15, 2022
2e803f8
Separating the experimental config out of optimizeFonts flag
janicklas-ralph Sep 16, 2022
490be99
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 16, 2022
1c82ede
Fix lint
janicklas-ralph Sep 16, 2022
d2a3318
Merge branch 'canary' into font-override
ijjk Sep 16, 2022
1afdc33
tweak config a bit
ijjk Sep 16, 2022
edb5f05
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 21, 2022
42d2943
Merge branch 'font-override' of github.com:janicklas-ralph/next.js in…
janicklas-ralph Sep 21, 2022
3ea4d2f
Adding font optimize adjustFallbacks docs
janicklas-ralph Sep 21, 2022
639bf4f
Fix review comment
janicklas-ralph Sep 21, 2022
d5f9f58
Fix review comment
janicklas-ralph Sep 22, 2022
e2162f7
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 22, 2022
0511f3f
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 26, 2022
1dcafda
Change font fallback name
janicklas-ralph Sep 26, 2022
aaee9fc
Change font fallback name
janicklas-ralph Sep 26, 2022
f4c8fad
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 26, 2022
11e38d4
Fix test case
janicklas-ralph Sep 26, 2022
ab85678
Fix test case
janicklas-ralph Sep 26, 2022
5b3f143
Fix test case
janicklas-ralph Sep 26, 2022
dff8dd3
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 26, 2022
3aa3e99
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 27, 2022
0826d6c
Fix test case
janicklas-ralph Sep 27, 2022
4d3cd8f
Fix test case
janicklas-ralph Sep 28, 2022
1cbe067
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 28, 2022
2617520
Fix remaining className and variable name tests
janicklas-ralph Sep 28, 2022
dab91d6
Merge branch 'canary' of github.com:vercel/next.js into font-override
janicklas-ralph Sep 28, 2022
ededca5
Undo incorrect merge
janicklas-ralph Sep 28, 2022
d6f4788
Merge branch 'canary' into font-override
styfle Sep 28, 2022
6e122e4
Moving name changes into another PR
janicklas-ralph Sep 29, 2022
35b71fe
Merge branch 'font-override' of github.com:janicklas-ralph/next.js in…
janicklas-ralph Sep 29, 2022
299232d
Merge branch 'canary' into font-override
ijjk Sep 29, 2022
916cbee
Merge branch 'canary' into font-override
kodiakhq[bot] Sep 29, 2022
4194670
revert extra change
ijjk Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/basic-features/font-optimization.md
Expand Up @@ -65,6 +65,58 @@ module.exports = {
}
```

## Optimize CLS for Fonts

Next.js can reduce the Cumulative Layout Shift ([CLS](https://web.dev/cls/)) of your website by adjusting the size of your fallback fonts and inlining the font CSS.

Sites that load fonts with `font-display: swap` usually suffer from ([CLS](https://web.dev/cls/)) when the web font loads and replaces the fallback font. This is due to differences in height, width, and alignment between the main and fallback fonts, which is common even if the CSS font size is the same.

Next.js can reduce CLS automatically by adjusting the size of the fallback font to match that of the main font using font override metric properties such as `size-adjust`, `ascent-override`, `descent-override`, and `line-gap-override`.

To enable this experimental feature, update your `next.config.js` with the following configuration:

```js
module.exports = {
experimental: {
adjustFontFallbacks: true,
},
}
```

When enabled, Next.js will generate a fallback font definition with the correct size overrides in the format `{fontName} Fallback`.
For example, the font `Inter` will generate the fallback font `Inter Fallback`.

You can then use the fallback font in your stylesheets such as the following:

```css
body {
font-family: 'Inter', 'Inter Fallback', sans-serif;
}
```

> **NOTE**: Next.js currently supports one cross-platform serif font ('Times New Roman') and one cross-platform sans-serif font ('Arial')

The final output will include the fallback override definition.

```html
// Injected into index.html during build/render
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<style data-href="https://fonts.googleapis.com/css2?family=Inter&display=swap">
@font-face{
font-family: 'Inter';
font-style:normal
...
}

@font-face {
font-family: 'Inter Fallback',
src: local('Arial');
ascent-override: 96.975%;
...
}
</style>
```

## Related

For more information on what to do next, we recommend the following sections:
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -216,7 +216,7 @@ export function getDefineEnv({
'process.env.__NEXT_STRICT_MODE': JSON.stringify(config.reactStrictMode),
'process.env.__NEXT_REACT_ROOT': JSON.stringify(hasReactRoot),
'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify(
!dev && config.optimizeFonts
config.optimizeFonts && !dev
),
'process.env.__NEXT_OPTIMIZE_CSS': JSON.stringify(
config.experimental.optimizeCss && !dev
Expand Down
4 changes: 2 additions & 2 deletions packages/next/server/font-utils.ts
Expand Up @@ -117,7 +117,7 @@ export function calculateOverrideValues(font: string, fontMetrics: any) {
}

function calculateOverrideCSS(font: string, fontMetrics: any) {
const fontName = font.toLowerCase().trim().replace(/ /g, '-')
const fontName = font.trim()

const { ascent, descent, lineGap, fallbackFont } = calculateOverrideValues(
font,
Expand All @@ -126,7 +126,7 @@ function calculateOverrideCSS(font: string, fontMetrics: any) {

return `
@font-face {
font-family: "${fontName}-fallback";
font-family: "${fontName} Fallback";
ascent-override: ${ascent}%;
descent-override: ${descent}%;
line-gap-override: ${lineGap}%;
Expand Down
6 changes: 3 additions & 3 deletions test/integration/font-optimization/test/index.test.js
Expand Up @@ -359,14 +359,14 @@ describe('Font Optimization', () => {
)
expect(inlineStyle.length).toBe(1)
expect(inlineStyle.html()).toContain(
'@font-face{font-family:"roboto-fallback";ascent-override:92.77%;descent-override:24.41%;line-gap-override:0.00%;src:local("Arial")}'
'@font-face{font-family:"Roboto Fallback";ascent-override:92.77%;descent-override:24.41%;line-gap-override:0.00%;src:local("Arial")}'
)
expect(inlineStyleMultiple.length).toBe(1)
expect(inlineStyleMultiple.html()).toContain(
'@font-face{font-family:"libre-baskerville-fallback";ascent-override:97.00%;descent-override:27.00%;line-gap-override:0.00%;src:local("Times New Roman")}'
'@font-face{font-family:"Libre Baskerville Fallback";ascent-override:97.00%;descent-override:27.00%;line-gap-override:0.00%;src:local("Times New Roman")}'
)
expect(inlineStyleMultiple.html()).toContain(
'@font-face{font-family:"open-sans-fallback";ascent-override:106.88%;descent-override:29.30%;line-gap-override:0.00%;src:local("Arial")}'
'@font-face{font-family:"Open Sans Fallback";ascent-override:106.88%;descent-override:29.30%;line-gap-override:0.00%;src:local("Arial")}'
)
})
})
Expand Down