如何在电子/React应用程序中预加载字体?
我正在使用Electron-Forge,无法弄清楚如何制作相对于构建的路径。
之后的摘要在开发模式下工作,但在生产模式下不工作。
<!DOCTYPE html>
<html lang="en" data-color-scheme="auto">
<head>
<link
rel="preload"
href="/c5c3959c04004102ea46.woff2"
as="font"
crossorigin="anonymous"
/>
<link
rel="preload"
href="/535bc89d4af715503b01.woff2"
as="font"
crossorigin="anonymous"
/>
<meta charset="utf-8" />
<title>Test app</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
这些是引发的错误:
index.html:1 GET file:///c5c3959c04004102ea46.woff2 net::ERR_FILE_NOT_FOUND
index.html:1 GET file:///535bc89d4af715503b01.woff2 net::ERR_FILE_NOT_FOUND
感谢您的帮助!
I’m using electron-forge and can’t figure out how to make paths relative to build.
Following snippet works in development mode, but not in productions mode.
<!DOCTYPE html>
<html lang="en" data-color-scheme="auto">
<head>
<link
rel="preload"
href="/c5c3959c04004102ea46.woff2"
as="font"
crossorigin="anonymous"
/>
<link
rel="preload"
href="/535bc89d4af715503b01.woff2"
as="font"
crossorigin="anonymous"
/>
<meta charset="utf-8" />
<title>Test app</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
These are the errors thrown:
index.html:1 GET file:///c5c3959c04004102ea46.woff2 net::ERR_FILE_NOT_FOUND
index.html:1 GET file:///535bc89d4af715503b01.woff2 net::ERR_FILE_NOT_FOUND
Thanks for helping out!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的电子应用程序将以不同的目录构建,因此您的用户在其选择的任何OS上的任何目录中的任何驱动器上运行,对文件路径的引用必须是相对的(不是绝对)。
以下是您的
&lt; link&gt;
href
的示例,取决于典型的目录结构。示例1
平面
渲染过程
目录结构。index.html
(渲染过程)示例2
向下
渲染过程
目录结构。index.html
(渲染过程)示例3
up/down
渲染过程
目录结构。index.html
(渲染过程)示例4
更深的上/下
渲染过程
目录结构。index.html
(渲染过程)As your Electron application will be built in a different directory, run by your user(s) on any drive in any directory on any OS of their choosing, reference to file paths must be relative (not absolute).
Below are examples of what your
<link>
href
would look like depending on a typical directory structure.Example 1
Flat
render-process
directory structure.index.html
(render process)Example 2
Downwards
render-process
directory structure.index.html
(render process)Example 3
Up / down
render-process
directory structure.index.html
(render process)Example 4
An even deeper up / down
render-process
directory structure.index.html
(render process)