javascript - 如何在域的子路径下的 Next.js 应用程序上使用自定义字体?
问题描述
我有一个 Next.js 应用程序部署在域的子路径下(例如example.com/my-next-js-app
)。对于捆绑脚本和样式,我可以使用 Next.js 配置解决它们:
const isProd = process.env.NODE_ENV === 'production';
module.exports = {
basePath: isProd ? '/my-next-js-app' : '',
};
对于图像,我可以创建一个函数,如果它在production
env 上,它将为图像 url 添加一个前缀。
export function getAssetUrl(path: string) {
return process.env.NODE_ENV === 'production' ? `${MAIN_URL}${path}` : path;
}
但是对于字体,我不太确定推荐的方式是什么。目前我的自定义字体styles/globals.css
如下:
@font-face {
font-family: 'MyCustomFont';
font-style: normal;
font-weight: 400;
src: url('/fonts/MyCustomFont-Regular.ttf') format('truetype');
font-display: swap;
}
@font-face {
font-family: 'MyCustomFont';
font-style: normal;
font-weight: 700;
src: url('/fonts/MyCustomFont-Bold.ttf') format('truetype');
font-display: swap;
}
@font-face {
font-family: 'MyCustomFont';
font-style: italic;
font-weight: 700;
src: url('/fonts/MyCustomFont-BoldItalic.ttf') format('truetype');
font-display: swap;
}
因此,在部署时,字体将不在根公用文件夹中,而是在example.com/my-next-js-app/fonts/MyCustomFont-xxx.ttf
.
解决方案
推荐阅读
- api - 如何使 URL 链接到工作表?
- php - Stripe 不会在我的网站上使用 composer 安装依赖项
- asp.net-web-api - 是否可以转发当前访问令牌或为代理请求新令牌
- javascript - 购物车 PLUS & MINUS 计算总数
- c# - Visual Studio 表单中的必选复选框(由用户检查)
- php - 无法处理消息,因为内容类型为 'text/xml; charset=utf-8' 不是预期的类型 'application/soap+xml; 字符集=utf-8'
- reactjs - 如何将来自firebase的所有图像实时列出到我的reactjs Web应用程序中?
- javascript - HTML 数字字段 toLocalString 不起作用
- sql - 获取 PLS-00103:在创建包时遇到符号“/”错误
- javascript - 单击设置后从播放更改为暂停