Capítulo 14 de 456
O módulo next/font otimiza fontes automaticamente com self-hosting embutido, removendo requests externas (privacidade e performance) e eliminando layout shift no carregamento.
next/font/google: importa e self-hosta qualquer Google Font como asset estático servido do mesmo domínio do deploy; o navegador não faz request ao Google.next/font/local: carrega arquivo de fonte local via src (path resolvido relativo ao arquivo onde localFont é chamado); fontes podem ficar em public/ ou colocalizadas em app/.weight.className.src como array: permite múltiplos arquivos de uma mesma família (pesos/estilos diferentes) num único localFont.import { Geist } from 'next/font/google'
const geist = Geist({ subsets: ['latin'] })
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" className={geist.className}>
<body>{children}</body>
</html>
)
}
import { Roboto } from 'next/font/google'
const roboto = Roboto({ weight: '400', subsets: ['latin'] })
weight explícito.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' },
],
})
localFont.className da fonte por página em vez do Root Layout: fragmenta o escopo desnecessariamente quando a intenção é fonte global; use o Root Layout para aplicação em toda a app.weight: comportamento inconsistente; sempre declarar weight para fontes estáticas.next/font elimina requests de rede externas para fontes: tudo é self-hosted automaticamente.weight quando não houver alternativa variável.app/), path resolvido relativo ao módulo que chama localFont.className no Root Layout usado para estilos globais.