字型最佳化
next/font
將自動最佳化您的字型(包含自訂字型),並移除外部網路請求,以提升隱私權和效能。
🎥 觀看: 深入瞭解如何使用
next/font
→ YouTube (6 分鐘)。
next/font
包含 內建自動託管 任何 字型檔案的功能。這表示您可以最佳化載入網頁字型,且零版面配置偏移,這要歸功於底層使用的 CSS size-adjust
屬性。
這個新的字型系統也讓您可以方便地使用所有 Google Fonts,同時兼顧效能和隱私權。CSS 和字型檔案會在建置時下載並與其他靜態資源一起託管。瀏覽器不會向 Google 發送任何請求。
Google Fonts
自動託管任何 Google Font。字型包含在部署中,並從與您的部署相同的網域提供。瀏覽器不會向 Google 發送任何請求。
首先從 next/font/google
匯入您想要使用的字型作為函式。我們建議使用可變字型,以獲得最佳效能和彈性。
import { Inter } from 'next/font/google'
// If loading a variable font, you don't need to specify the font weight
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}
如果您無法使用可變字型,則需要指定粗細
import { Roboto } from 'next/font/google'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={roboto.className}>
<body>{children}</body>
</html>
)
}
您可以使用陣列來指定多個粗細和/或樣式
const roboto = Roboto({
weight: ['400', '700'],
style: ['normal', 'italic'],
subsets: ['latin'],
display: 'swap',
})
小提示:對於包含多個單字的字型名稱,請使用底線 (_)。例如,
Roboto Mono
應匯入為Roboto_Mono
。
指定子集
Google Fonts 會自動進行子集化。這會減少字型檔案的大小並提升效能。您需要定義想要預先載入的這些子集。如果未指定任何子集,且 preload
為 true
,將會導致警告。
這可以透過將其新增至函式呼叫來完成
const inter = Inter({ subsets: ['latin'] })
請參閱Font 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 } from './fonts'
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" className={inter.className}>
<body>
<div>{children}</div>
</body>
</html>
)
}
import { roboto_mono } from './fonts'
export default function Page() {
return (
<>
<h1 className={roboto_mono.className}>My page</h1>
</>
)
}
在以上範例中,Inter
將全域套用,而 Roboto Mono
可以根據需要匯入和套用。
或者,您可以建立一個 CSS 變數,並將其與您偏好的 CSS 解決方案一起使用
import { Inter, Roboto_Mono } from 'next/font/google'
import styles from './global.css'
const inter = Inter({
subsets: ['latin'],
variable: '--font-inter',
display: 'swap',
})
const roboto_mono = Roboto_Mono({
subsets: ['latin'],
variable: '--font-roboto-mono',
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={`${inter.variable} ${roboto_mono.variable}`}>
<body>
<h1>My App</h1>
<div>{children}</div>
</body>
</html>
)
}
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'
// Font files can be colocated inside of `app`
const myFont = localFont({
src: './my-font.woff2',
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={myFont.className}>
<body>{children}</body>
</html>
)
}
如果您想要為單一字型系列使用多個檔案,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',
},
],
})
請參閱Font API 參考以取得更多資訊。
搭配 Tailwind CSS
next/font
可以透過 CSS 變數 與 Tailwind CSS 一起使用。
在以下範例中,我們使用來自 next/font/google
的字型 Inter
(您可以使用來自 Google 或本機字型的任何字型)。使用 variable
選項載入您的字型,以定義您的 CSS 變數名稱並將其指派給 inter
。然後,使用 inter.variable
將 CSS 變數新增至您的 HTML 文件。
import { Inter, Roboto_Mono } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
display: 'swap',
variable: '--font-inter',
})
const roboto_mono = Roboto_Mono({
subsets: ['latin'],
display: 'swap',
variable: '--font-roboto-mono',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={`${inter.variable} ${roboto_mono.variable}`}>
<body>{children}</body>
</html>
)
}
最後,將 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 字型函式時,該字型都會作為一個實例託管在您的應用程式中。因此,如果您在多個檔案中載入相同的字型函式,則會託管相同字型的多個實例。在這種情況下,建議執行以下操作
- 在一個共用檔案中呼叫字型載入器函式
- 將其匯出為常數
- 在您想要使用此字型的每個檔案中匯入常數
這有幫助嗎?