next/font 自动优化 web 字体以提高性能并消除布局移动 — 在构建时自我托管字体,并为您处理加载细节。
使用 Google Font
tsx
{ } ;
inter = ({
: [],
: ,
});
() {
(
);
}
next/font 自动优化 web 字体以提高性能并消除布局移动 — 在构建时自我托管字体,并为您处理加载细节。
{ } ;
inter = ({
: [],
: ,
});
() {
(
);
}
import localFont from "next/font/local";
const myFont = localFont({ src: "./MyFont.woff2", display: "swap" });
字体是典型的性能和 CLS 问题:第三方获取会阻止文本渲染,然后在字体加载时导致布局跳跃(FOUT/FOIT)。手动做好这一点需要 @font-face、font-display、预加载和备用指标匹配。
next/font 自动化了整个字体性能检查清单 — self-hosting(速度 + 隐私)、subsetting 和 preloading(更小、更快)、以及备用指标匹配(无布局移动)— 这一切都在一个 import 后面。
它消除了一个易出错的优化,是在 Next.js 中使用 Google 和本地字体的推荐方式。