英文:
Next.js always fail at downloading fonts from Google Fonts
问题
我已经使用 create-next-app
第一次创建了一个新的 Next.js 项目,并且成功使用 npm run dev
运行它。
问题是,每次 Next.js 启动时,都会显示以下错误:
FetchError: request to https://fonts.gstatic.com/s/inter/v12/UcC73FwrK3iLTeHuS_fvQtMwCp50KnMa1ZL7W0Q5nw.woff2 failed, reason:
at ClientRequest.<anonymous> (C:\Users\user\Desktop\documents\node_modules\next\dist\compiled\node-fetch\index.js:1:65756)
at ClientRequest.emit (node:events:511:28)
at TLSSocket.socketErrorListener (node:_http_client:495:9)
at TLSSocket.emit (node:events:511:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
type: 'system',
errno: 'ENETUNREACH',
code: 'ENETUNREACH'
}
- error Failed to download `Inter` from Google Fonts. Using fallback font instead.
我的目标是让 Next.js 使用实际的字体而不是备用字体。
我不知道为什么会发生这种情况,因为我尚未在 Next.js 中更改任何内容。甚至在浏览器中打开 https://fonts.gstatic.com/s/inter/v12/UcC73FwrK3iLTeHuS_fvQtMwCp50KnMa1ZL7W0Q5nw.woff2
的URL也能正常工作,所以我猜这不是一个网络问题。
我尝试在 Next.js 文档中搜索,但在这里没有找到任何重复的内容。
这是位于 src/app/
内的 layout.tsx
:
import './globals.css'
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
另外,我正在运行 Node 版本 20.3.0
和 npm 版本 9.4.1
,以及 Next.js 版本 13.4
。基本上是在提问时的最新版本。
英文:
I've created a new Next.js project for the first time using create-next-app
, and I've successfully run it with npm run dev
.
The problem is that everytime Next.js starts, it says:
FetchError: request to https://fonts.gstatic.com/s/inter/v12/UcC73FwrK3iLTeHuS_fvQtMwCp50KnMa1ZL7W0Q5nw.woff2 failed, reason:
at ClientRequest.<anonymous> (C:\Users\user\Desktop\documents\node_modules\next\dist\compiled\node-fetch\index.js:1:65756)
at ClientRequest.emit (node:events:511:28)
at TLSSocket.socketErrorListener (node:_http_client:495:9)
at TLSSocket.emit (node:events:511:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
type: 'system',
errno: 'ENETUNREACH',
code: 'ENETUNREACH'
}
- error Failed to download `Inter` from Google Fonts. Using fallback font instead.
My goal is to make Next.js use the actual font not the fallback one.
I don't know why this happens as I didn't change anything in Next.js yet. Even opening the URL https://fonts.gstatic.com/s/inter/v12/UcC73FwrK3iLTeHuS_fvQtMwCp50KnMa1ZL7W0Q5nw.woff2
in my browser works well so I guess it's not a networking issue.
I tried to search in Next.js documentation and I didn't find any duplicates here.
Here's layout.tsx
that's located inside src/app/
:
import './globals.css'
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
Also, I'm running Node version 20.3.0
and npm version 9.4.1
with Next.js 13.4
. Basically the latest at the time of writing this question.
答案1
得分: 7
尝试不同的浏览器并清除缓存也没有帮助。我通过添加键display: 'swap'
来解决了这个问题,其中值swap
的含义是:
> 给字体设置了一个极小的块期和一个无限的交换期。
字体"块"期的含义是:
> 如果字体没有加载,任何试图使用它的元素必须渲染一个不可见的备用字体。如果在此期间成功加载字体,它将正常使用。
而字体"交换"期的含义是:
> 如果字体没有加载,任何试图使用它的元素必须渲染一个备用字体。如果在此期间成功加载字体,它将正常使用。
这基本上就是我在寻找的内容。
我还添加了键adjustFontFallback: false
,这意味着:
> 一个布尔值,用于设置是否应使用自动备用字体来减少累积布局位移。默认值为true
。
所以这是我的最终layout.tsx
:
import './globals.css'
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'], display: 'swap', adjustFontFallback: false})
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
英文:
Trying different browsers and clearing the cache didn't help either. I've solved it by adding key display: 'swap'
, where value swap
means:
> Gives the font face an extremely small block period and an infinite swap period.
Where font "block" period means:
> If the font face is not loaded, any element attempting to use it must render an invisible fallback font face. If the font face successfully loads during this period, it is used normally.
And font "swap" period means:
> If the font face is not loaded, any element attempting to use it must render a fallback font face. If the font face successfully loads during this period, it is used normally.
And that's pretty much what I was looking for.
I've also added key adjustFontFallback: false
which means:
> A boolean value that sets whether an automatic fallback font should be used to reduce Cumulative Layout Shift. The default is true
.
So here's my final layout.tsx
:
import './globals.css'
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'], display: 'swap', adjustFontFallback: false})
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
答案2
得分: 0
对我来说,在我安装了SSL的生产环境中,字体加载正确。
英文:
For me, the Fonts were loading correctly in the production environment where I have the SSL installed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论