-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
update font optimization page #42266
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
989954d
update font optimization page
ismaelrumzan 71c4a97
updated api ref fonts to use /pages
ismaelrumzan 8424e63
Merge branch 'canary' into v13-docs-fonts-update
ismaelrumzan d158823
Merge branch 'canary' into v13-docs-fonts-update
delbaoliveira 7838cd4
Update docs/basic-features/font-optimization.md
timneutkens 49e5b38
Merge branch 'canary' into v13-docs-fonts-update
delbaoliveira f770565
Update docs/basic-features/font-optimization.md
delbaoliveira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,49 +26,82 @@ Automatically self-host any Google Font. Fonts are included in the deployment an | |
|
||
Import the font you would like to use from `@next/font/google` as a function. We recommend using [**variable fonts**](https://fonts.google.com/variablefonts) for the best performance and flexibility. | ||
|
||
```jsx | ||
// app/layout.tsx | ||
import { Inter } from '@next/font/google' | ||
To use the font in all your pages, add it to [`_app.js` file](https://nextjs.org/docs/advanced-features/custom-app) under `/pages` as shown below: | ||
|
||
```js:pages/_app.js | ||
import { Inter } from '@next/font/google'; | ||
|
||
// If loading a variable font, you don't need to specify the font weight | ||
const inter = Inter() | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode, | ||
}) { | ||
export default function MyApp({ Component, pageProps }) { | ||
return ( | ||
<html lang="en" className={inter.className}> | ||
<body>{children}</body> | ||
</html> | ||
<main className={inter.className}> | ||
<Component {...pageProps} /> | ||
</main> | ||
) | ||
} | ||
``` | ||
|
||
If you can't use a variable font, you will **need to specify a weight**: | ||
|
||
```jsx | ||
// app/layout.tsx | ||
import { Roboto } from '@next/font/google' | ||
```js:pages/_app.js | ||
import { Roboto } from '@next/font/google'; | ||
|
||
const roboto = Roboto({ | ||
weight: '400', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing multiple weights/styles in API reference for both google and local. |
||
}) | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode, | ||
}) { | ||
export default function MyApp({ Component, pageProps }) { | ||
return ( | ||
<main className={roboto.className}> | ||
<Component {...pageProps} /> | ||
</main> | ||
) | ||
} | ||
``` | ||
|
||
#### Apply the font in `<head>` | ||
|
||
You can also use the font without a wrapper and `className` by injecting it inside the `<head>` as follows: | ||
|
||
```js:pages/_app.js | ||
import { Inter } from '@next/font/google'; | ||
|
||
const inter = Inter(); | ||
|
||
export default function MyApp({ Component, pageProps }) { | ||
return ( | ||
<html lang="en" className={roboto.className}> | ||
<body>{children}</body> | ||
</html> | ||
<> | ||
<style jsx global>{` | ||
html { | ||
font-family: ${inter.style.fontFamily}; | ||
} | ||
`}</style> | ||
<Component {...pageProps} /> | ||
</> | ||
) | ||
} | ||
``` | ||
|
||
#### Single page usage | ||
|
||
To use the font on a single page, add it to the specific page as shown below: | ||
|
||
```js:pages/index.js | ||
import { Inter } from '@next/font/google'; | ||
|
||
const inter = Inter(); | ||
|
||
export default function Home() { | ||
return ( | ||
<div className={inter.className}> | ||
<p>Hello World</p> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
#### Specifying a subset | ||
|
||
Google Fonts are automatically [subset](https://fonts.google.com/knowledge/glossary/subsetting). This reduces the size of the font file and improves performance. You'll need to define which of these subsets you want to preload. Failing to specify any subsets while [`preload`](/docs/api-reference/next/font.md#preload) is true will result in a warning. | ||
|
@@ -77,9 +110,8 @@ This can be done in 2 ways: | |
|
||
- On a font per font basis by adding it to the function call | ||
|
||
```tsx | ||
// app/layout.tsx | ||
const inter = Inter({ subsets: ['latin'] }) | ||
```js:pages/_app.js | ||
const inter = Inter({ subsets: ["latin"] }); | ||
``` | ||
|
||
- Globally for all your fonts in your `next.config.js` | ||
|
@@ -103,22 +135,17 @@ View the [Font API Reference](/docs/api-reference/next/font.md#nextfontgoogle) f | |
|
||
Import `@next/font/local` and specify the `src` of your local font file. We recommend using [**variable fonts**](https://fonts.google.com/variablefonts) for the best performance and flexibility. | ||
|
||
```jsx | ||
/// app/layout.tsx | ||
import localFont from '@next/font/local' | ||
```js:pages/_app.js | ||
import localFont from '@next/font/local'; | ||
|
||
// Font files can be colocated inside of `app` | ||
const myFont = localFont({ src: './my-font.woff2' }) | ||
// Font files can be colocated inside of `pages` | ||
const myFont = localFont({ src: './my-font.woff2' }); | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode, | ||
}) { | ||
export default function MyApp({ Component, pageProps }) { | ||
return ( | ||
<html lang="en" className={myFont.className}> | ||
<body>{children}</body> | ||
</html> | ||
<main className={myFont.className}> | ||
<Component {...pageProps} /> | ||
</main> | ||
) | ||
} | ||
``` | ||
|
@@ -129,9 +156,8 @@ View the [Font API Reference](/docs/api-reference/next/font.md#nextfontlocal) fo | |
|
||
When a font function is called on a page of your site, it is not globally available and preloaded on all routes. Rather, the font is only preloaded on the related route/s based on the type of file where it is used: | ||
|
||
- if it's a [unique page](https://beta.nextjs.org/docs/routing/pages-and-layouts#pages), it is preloaded on the unique route for that page | ||
- if it's a [layout](https://beta.nextjs.org/docs/routing/pages-and-layouts#layouts), it is preloaded on all the routes wrapped by the layout | ||
- if it's the [root layout](https://beta.nextjs.org/docs/routing/pages-and-layouts#root-layout-required), it is preloaded on all routes | ||
- if it's a [unique page](/docs/basic-features/pages), it is preloaded on the unique route for that page | ||
- if it's in the [custom App](/docs/advanced-features/custom-app), it is preloaded on all the routes of the site under `/pages` | ||
|
||
## Reusing fonts | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct
It's always relative to where it's called.