字體優化
next/font
會自動優化您的字型(包括自訂字型),並移除外部網路請求以提升隱私性和效能。
🎥 觀看影片: 深入了解如何使用
next/font
→ YouTube (6分鐘)。
next/font
包含內建的自動自我託管功能,適用於_任何_字型檔案。這意味著您可以最佳化載入網頁字型,且由於使用了底層 CSS size-adjust
屬性,實現零版面位移。
這個新的字型系統還讓您能方便地使用所有 Google 字型,同時兼顧效能和隱私。CSS 和字型檔案會在建置時下載,並與其他靜態資源一起自我託管。瀏覽器不會向 Google 發送任何請求。
Google 字型
自動自我託管任何 Google 字型。字型會包含在部署中,並從與您的部署相同的網域提供服務。瀏覽器不會向 Google 發送任何請求。
首先從 next/font/google
匯入您想使用的字型作為函式。我們建議使用可變字型以獲得最佳效能和靈活性。
要在所有頁面中使用字型,請將其新增至 /pages
下的 _app.js
檔案,如下所示:
import { Inter } from 'next/font/google'
// 如果載入可變字型,您不需要指定字重
const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) {
return (
<main className={inter.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>
)
}
您可以透過陣列指定多個字重和/或樣式:
const roboto = Roboto({
weight: ['400', '700'],
style: ['normal', 'italic'],
subsets: ['latin'],
display: 'swap',
})
須知:對於多個單字的字型名稱,請使用底線 (_)。例如
Roboto Mono
應匯入為Roboto_Mono
。
在 <head>
中套用字型
您也可以不使用包裹器和 className
,而是透過以下方式將字型注入 <head>
:
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) {
return (
<>
<style jsx global>{`
html {
font-family: ${inter.style.fontFamily};
}
`}</style>
<Component {...pageProps} />
</>
)
}
單一頁面使用
要在單一頁面中使用字型,請將其新增至特定頁面,如下所示:
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function Home() {
return (
<div className={inter.className}>
<p>Hello World</p>
</div>
)
}
指定子集
Google 字型會自動子集化。這減少了字型檔案的大小並提升效能。您需要定義要預載入哪些子集。如果未指定任何子集且 preload
為 true
,將會產生警告。
可以透過將其新增至函式呼叫來完成:
const inter = Inter({ subsets: ['latin'] })
檢視字型 API 參考以獲取更多資訊。
使用多種字型
您可以在應用程式中匯入和使用多種字型。有兩種方法可以實現。
第一種方法是建立一個匯出字型的工具函式,匯入它並在需要的地方套用其 className
。這確保字型僅在渲染時預載入:
import { Inter, Roboto_Mono } from 'next/font/google'
export const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export const roboto_mono = Roboto_Mono({
subsets: ['latin'],
display: 'swap',
})
import { Inter, Roboto_Mono } from 'next/font/google'
export const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export const roboto_mono = Roboto_Mono({
subsets: ['latin'],
display: 'swap',
})
在上面的範例中,Inter
會全域套用,而 Roboto Mono
可以按需匯入和套用。
或者,您可以建立一個 CSS 變數並與您偏好的 CSS 解決方案一起使用:
html {
font-family: var(--font-inter);
}
h1 {
font-family: var(--font-roboto-mono);
}
在上面的範例中,Inter
會全域套用,而任何 <h1>
標籤都會使用 Roboto Mono
樣式。
建議:謹慎使用多種字型,因為每種新字型都是客戶端需要下載的額外資源。
本地字型
匯入 next/font/local
並指定本地字型檔案的 src
。我們建議使用可變字型以獲得最佳效能和靈活性。
import localFont from 'next/font/local'
// 字型檔案可以與 `pages` 放在同一位置
const myFont = localFont({ src: './my-font.woff2' })
export default function MyApp({ Component, pageProps }) {
return (
<main className={myFont.className}>
<Component {...pageProps} />
</main>
)
}
如果想為單一字型家族使用多個檔案,src
可以是陣列:
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',
},
],
})
檢視字型 API 參考以獲取更多資訊。
與 Tailwind CSS 搭配使用
next/font
可以透過 CSS 變數與 Tailwind CSS 一起使用。
在以下範例中,我們使用來自 next/font/google
的字型 Inter
(您可以使用來自 Google 或本地字型的任何字型)。使用 variable
選項載入您的字型以定義 CSS 變數名稱,並將其分配給 inter
。然後,使用 inter.variable
將 CSS 變數新增至您的 HTML 文件。
import { Inter } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
variable: '--font-inter',
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={`${inter.variable} font-sans`}>
<Component {...pageProps} />
</main>
)
}
最後,將 CSS 變數新增至您的 Tailwind CSS 配置:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
'./app/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
fontFamily: {
sans: ['var(--font-inter)'],
mono: ['var(--font-roboto-mono)'],
},
},
},
plugins: [],
}
現在您可以使用 font-sans
和 font-mono
工具類別將字型套用至您的元素。
預載入
當在您網站的頁面上呼叫字型函式時,它不會全域可用,也不會在所有路由上預載入。相反,字型僅根據使用它的檔案類型在相關路由上預載入:
重複使用字型
每次呼叫 localFont
或 Google 字型函式時,該字型會作為一個實例託管在您的應用程式中。因此,如果在多個檔案中載入相同的字型函式,則會託管相同字型的多個實例。在這種情況下,建議執行以下操作:
- 在一個共享檔案中呼叫字型載入器函式
- 將其匯出為常數
- 在每個需要使用此字型的檔案中匯入該常數