Unverified Commit eef157f9 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

@next/font change default font-display (#45430)

Change default font-display from `optional` to `swap`.

[slack
ref](https://vercel.slack.com/archives/C03KED0D4N7/p1674949620477649)

Fixes NEXT-430

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent a5e9d032
......@@ -107,7 +107,7 @@ Examples:
### `display`
The font [`display`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display) with possible string [values](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display#values) of `'auto'`, `'block'`, `'swap'`, `'fallback'` or `'optional'` with default value of `'optional'`.
The font [`display`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display) with possible string [values](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display#values) of `'auto'`, `'block'`, `'swap'`, `'fallback'` or `'optional'` with default value of `'swap'`.
Used in `@next/font/google` and `@next/font/local`
......
......@@ -22,12 +22,6 @@ To get started, install `@next/font`:
npm install @next/font
```
### Choosing font-display
[`font-display`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display) lets you to control how your font is displayed while it's loading. `@next/font` uses `font-display: optional` by default. When the primary font used by `@next/font` does not load within 100ms, the auto generated fallback font will be displayed in the browser. While it is automatically generated to be as visually similar to the primary font as possible to reduce layout shift, it does come with the tradeoff that your intended font might not be shown on slower networks unless it's cached.
If you want guarantees around your intended font always showing, and accept the tradeoff of minimal layout shift from swapping your fallback font for the primary font, you can [use `font-display: swap`](/docs/api-reference/next/font.md#display).
### Google Fonts
Automatically self-host any Google Font. Fonts are included in the deployment and served from the same domain as your deployment. **No requests are sent to Google by the browser.**
......
......@@ -29,9 +29,7 @@ export function validateData(
weight,
style,
preload = true,
// If preload is disabled set display to 'swap' by default.
// If display is 'optional' and we don't preload, we will never fetch the font in time to display it, not even in dev.
display = preload ? 'optional' : 'swap',
display = 'swap',
axes,
fallback,
adjustFontFallback = true,
......@@ -52,9 +50,8 @@ export function validateData(
const availableSubsets = fontFamilyData.subsets
if (availableSubsets.length === 0) {
// If the font doesn't have any preloaded subsets, disable preload and set display to 'swap'
// If the font doesn't have any preloaded subsets, disable preload
preload = false
display = 'swap'
} else {
if (preload && !callSubsets && !config?.subsets) {
nextFontError(
......
......@@ -36,7 +36,7 @@ export function validateData(functionName: string, fontData: any): FontOptions {
}
let {
src,
display = 'optional',
display = 'swap',
weight,
style,
fallback,
......
module.exports = {
'https://fonts.googleapis.com/css2?family=Open+Sans:wght@300..800&display=optional': `
'https://fonts.googleapis.com/css2?family=Open+Sans:wght@300..800&display=swap': `
/* latin */
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 300 800;
font-stretch: 100%;
font-display: optional;
font-display: swap;
src: url(https://fonts.gstatic.com/s/opensans/v34/memvYaGs126MiZpBA-UvWbX2vVnXBbObj2OVTS-mu0SC55I.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
......
......@@ -13,12 +13,12 @@ describe('@next/font/google loader', () => {
[
'Inter',
{},
'https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=optional',
'https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap',
],
[
'Inter',
{ weight: '400' },
'https://fonts.googleapis.com/css2?family=Inter:wght@400&display=optional',
'https://fonts.googleapis.com/css2?family=Inter:wght@400&display=swap',
],
[
'Inter',
......@@ -33,7 +33,7 @@ describe('@next/font/google loader', () => {
[
'Source_Sans_Pro',
{ weight: '200', style: 'italic' },
'https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@1,200&display=optional',
'https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@1,200&display=swap',
],
[
'Roboto_Flex',
......@@ -56,32 +56,32 @@ describe('@next/font/google loader', () => {
[
'Oooh_Baby',
{ weight: '400' },
'https://fonts.googleapis.com/css2?family=Oooh+Baby:wght@400&display=optional',
'https://fonts.googleapis.com/css2?family=Oooh+Baby:wght@400&display=swap',
],
[
'Albert_Sans',
{ weight: 'variable', style: 'italic' },
'https://fonts.googleapis.com/css2?family=Albert+Sans:ital,wght@1,100..900&display=optional',
'https://fonts.googleapis.com/css2?family=Albert+Sans:ital,wght@1,100..900&display=swap',
],
[
'Fraunces',
{ weight: 'variable', style: 'italic', axes: ['WONK', 'opsz', 'SOFT'] },
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght,SOFT,WONK@1,9..144,100..900,0..100,0..1&display=optional',
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght,SOFT,WONK@1,9..144,100..900,0..100,0..1&display=swap',
],
[
'Molle',
{ weight: '400' },
'https://fonts.googleapis.com/css2?family=Molle:ital,wght@1,400&display=optional',
'https://fonts.googleapis.com/css2?family=Molle:ital,wght@1,400&display=swap',
],
[
'Roboto',
{ weight: ['500', '300', '400'], style: ['normal', 'italic'] },
'https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=optional',
'https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=swap',
],
[
'Roboto Mono',
{ style: ['italic', 'normal'] },
'https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=optional',
'https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap',
],
[
'Fraunces',
......@@ -89,27 +89,27 @@ describe('@next/font/google loader', () => {
style: ['normal', 'italic'],
axes: ['WONK', 'opsz', 'SOFT'],
},
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght,SOFT,WONK@0,9..144,100..900,0..100,0..1;1,9..144,100..900,0..100,0..1&display=optional',
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght,SOFT,WONK@0,9..144,100..900,0..100,0..1;1,9..144,100..900,0..100,0..1&display=swap',
],
[
'Poppins',
{ weight: ['900', '400', '100'] },
'https://fonts.googleapis.com/css2?family=Poppins:wght@100;400;900&display=optional',
'https://fonts.googleapis.com/css2?family=Poppins:wght@100;400;900&display=swap',
],
[
'Nabla',
{},
'https://fonts.googleapis.com/css2?family=Nabla&display=optional',
'https://fonts.googleapis.com/css2?family=Nabla&display=swap',
],
[
'Nabla',
{ axes: ['EDPT', 'EHLT'] },
'https://fonts.googleapis.com/css2?family=Nabla:EDPT,EHLT@0..200,0..24&display=optional',
'https://fonts.googleapis.com/css2?family=Nabla:EDPT,EHLT@0..200,0..24&display=swap',
],
[
'Ballet',
{},
'https://fonts.googleapis.com/css2?family=Ballet&display=optional',
'https://fonts.googleapis.com/css2?family=Ballet&display=swap',
],
])('%s', async (functionName: string, data: any, url: string) => {
fetch.mockResolvedValue({
......
......@@ -23,7 +23,7 @@ describe('@next/font/local', () => {
"@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
}
"
`)
......@@ -50,7 +50,7 @@ describe('@next/font/local', () => {
"@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 100 900;
font-style: italic;
}
......@@ -89,7 +89,7 @@ describe('@next/font/local', () => {
ascent-override: 90%;
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
}
"
`)
......@@ -140,7 +140,7 @@ describe('@next/font/local', () => {
"@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 100;
font-style: italic;
}
......@@ -148,7 +148,7 @@ describe('@next/font/local', () => {
@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 400;
font-style: italic;
}
......@@ -156,7 +156,7 @@ describe('@next/font/local', () => {
@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 700;
font-style: italic;
}
......@@ -164,7 +164,7 @@ describe('@next/font/local', () => {
@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 400;
font-style: normal;
}
......@@ -212,7 +212,7 @@ describe('@next/font/local', () => {
"@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 400;
font-style: normal;
}
......@@ -220,7 +220,7 @@ describe('@next/font/local', () => {
@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 400;
font-style: italic;
}
......@@ -228,7 +228,7 @@ describe('@next/font/local', () => {
@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2) format('woff2');
font-display: optional;
font-display: swap;
font-weight: 700;
}
"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment