这是我第一次使用新next/font软件包。我遵循了 Next.js 的教程,它很容易设置。我正在使用 Inter 和名为 App Takeoff 的自定义本地字体。为了实际使用这两种字体,我使用 Tailwind CSS,其中 Inter 连接到font-sans,App Takeoff 连接到font-display。
我已经在文件之间进行了大量的测试,并且出于某种原因,除了我的Modal组件之外,两种字体都可以在任何地方使用。(请参阅底部的“有用更新”以了解它在组件中不起作用的原因Modal。)
索引.tsx
\n\nmodal.tsx 通过 index.tsx
\n\n正如您所看到的,当字体不在模式中时,它们工作得很好,但是一旦它们在模式中,它们就不起作用。
\n// app.tsx\n\nimport \'@/styles/globals.css\'\nimport type { AppProps } from \'next/app\'\n\nimport { Inter } from \'next/font/google\'\nconst inter = Inter({\n subsets: [\'latin\'],\n variable: \'--font-inter\'\n})\n\nimport localFont from \'next/font/local\'\nconst …Run Code Online (Sandbox Code Playgroud) 太长了;博士
Next.js 13 的/app路由器layout和page路由改变了我们向<head>. 如何向每个页面添加架构脚本?Next.js 会自动将<head>放置在任何layout或page中的标签编译为单个<head>.
就像任何具有出色 SEO 的网站一样,我想在每个页面的头部包含一个架构脚本。
通常,这就像这样简单地编写<head>:
<!-- index.html -->
<head>
<script type="application/ld+json">
{
"@context": "https://schema.org",
// ... the rest
}
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
然后,Next.js/pages目录有点不同。我总是觉得必须使用该dangerouslySetInnerHTML属性很奇怪,但至少它有效。
// index.tsx
import Head from 'next/head'
export default function Page() {
return (
<Head>
<script id="schema" type="application/ld+json" dangerouslySetInnerHTML={{__html: `
{
"@context": "https://schema.org",
// ... the rest
}
`}} /> …Run Code Online (Sandbox Code Playgroud)