Como usar fontes
O módulo next/font otimiza automaticamente suas fontes e remove requisições de rede externas, para mais privacidade e desempenho.
Ele inclui hospedagem própria integrada para qualquer arquivo de fonte. Isso significa que você pode carregar fontes web de forma otimizada, sem deslocamento de layout.
Para começar a usar o next/font, importe-o de next/font/local ou next/font/google, chame-o como uma função com as opções apropriadas e defina o className do elemento ao qual você quer aplicar a fonte. Por exemplo, você pode aplicar fontes globalmente no seu App personalizado (pages/_app):
import { Geist } from 'next/font/google'
import type { AppProps } from 'next/app'
const geist = Geist({
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<main className={geist.className}>
<Component {...pageProps} />
</main>
)
}import { Geist } from 'next/font/google'
const geist = Geist({
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={geist.className}>
<Component {...pageProps} />
</main>
)
}Google Fonts
Você pode hospedar qualquer Google Font por conta própria, de forma automática. As fontes são incluídas como arquivos estáticos e servidas a partir do mesmo domínio do seu deploy, o que significa que o navegador não envia requisições ao Google quando o usuário visita seu site.
Para começar a usar uma Google Font, importe a fonte escolhida de next/font/google:
import { Geist } from 'next/font/google'
import type { AppProps } from 'next/app'
const geist = Geist({
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<main className={geist.className}>
<Component {...pageProps} />
</main>
)
}import { Geist } from 'next/font/google'
const geist = Geist({
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={geist.className}>
<Component {...pageProps} />
</main>
)
}Recomendamos usar fontes variáveis para o melhor desempenho e flexibilidade. Mas, se não puder usar uma fonte variável, será necessário especificar um peso:
import { Roboto } from 'next/font/google'
import type { AppProps } from 'next/app'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<main className={roboto.className}>
<Component {...pageProps} />
</main>
)
}import { Roboto } from 'next/font/google'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={roboto.className}>
<Component {...pageProps} />
</main>
)
}Fontes locais
Para usar uma fonte local, importe sua fonte de next/font/local e especifique o src do arquivo da fonte local. Você pode guardar as fontes na pasta public ou dentro da pasta pages. Por exemplo:
import localFont from 'next/font/local'
import type { AppProps } from 'next/app'
const myFont = localFont({
src: './my-font.woff2',
})
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<main className={myFont.className}>
<Component {...pageProps} />
</main>
)
}import localFont from 'next/font/local'
const myFont = localFont({
src: './my-font.woff2',
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={myFont.className}>
<Component {...pageProps} />
</main>
)
}Se você quiser usar vários arquivos para uma única família de fontes, src pode ser um array:
const roboto = localFont({
src: [
{
path: './Roboto-Regular.woff2',
weight: '400',
style: 'normal',
},
{
path: './Roboto-Italic.woff2',
weight: '400',
style: 'italic',
},
{
path: './Roboto-Bold.woff2',
weight: '700',
style: 'normal',
},
{
path: './Roboto-BoldItalic.woff2',
weight: '700',
style: 'italic',
},
],
})Referência da API
Consulte a Referência da API para ver todos os recursos de Next.js Font