【问题标题】:Font loading with next.js and style jsx使用 next.js 和样式 jsx 加载字体
【发布时间】:2020-07-05 23:18:52
【问题描述】:

我最近开始将我的 create react app 应用程序转换为 next.js。我正在使用 vercel 的开源 next.js 网站作为构建我的指南。我声明了一个 font.ts 并使用了样式 jsx 文件并将其导入到 _app.js 但是当我使用该字体时,我从服务器得到一个 404。

这是 font.ts 文件:

import css from 'styled-jsx/css'

export default css.global`
    /* latin */
    @font-face {
        font-family: bon;
        font-display: auto;
        src: url('../public/static/fonts/Bon.woff2') format('woff2');
    }

    @font-face {
        font-family: bonitalic;
        font-display: auto;
        src: url('../public/static/fonts/Bonitalic.woff2') format('woff2');
    }

    @font-face {
        font-family: maison;
        font-display: auto;
        src: url('../public/static/fonts/MaisonNeue-Book.woff2') format('woff2');
    }
`;

【问题讨论】:

    标签: javascript reactjs typescript next.js


    【解决方案1】:

    您需要在此处使用的字体文件的位置是它们的公共 URL,而不是它们的文件系统位置。所以不要使用url('../public/static/fonts/...'),而是使用url('/static/fonts/...')

    【讨论】:

      猜你喜欢
      • 2021-04-08
      • 2019-07-21
      • 2019-01-26
      • 1970-01-01
      • 2020-12-03
      • 2022-08-17
      • 1970-01-01
      • 2022-06-12
      • 2023-02-01
      相关资源
      最近更新 更多