【发布时间】:2021-08-02 00:14:10
【问题描述】:
我正在 NextJS 中开发一个使用大约 5 种 Google 字体的个人项目。
仅当页面New 重新加载或我使用<a></a> 标签而不是<Link></Link> 导航到页面时才会获取字体。
这是我当前需要字体的页面的代码,
export async function getStaticProps() {
const fonts = getFontNames(); // returns a static list of strings like ['Monserrat', 'Product Sans', ...].
const fontsLink = "https://fonts.googleapis.com/css2?family=" + fonts.map(cV => cV.replace(/ /g, '+')).join('&family=') + "&display=swap"
return {
props: {
fonts,
fontsLink
}
}
}
export default function New({ fonts, fontsLink }) {
return (
<div className={styles.container}>
<Head>
<title>New</title>
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link href={fontsLink} rel="stylesheet" />
</Head>
...
</div>
}
这就是我导航到页面 New 的方式,
<Link href="/new">
<a className={styles.card}>
<h2>New →</h2>
</a>
</Link>
【问题讨论】:
标签: html css reactjs next.js google-fonts