当 base64 字体被视为未使用的 css 时,如何修复 pagespeed 分数

发布于 2025-01-13 21:08:24 字数 1020 浏览 3 评论 0原文

我将字体文件直接嵌入到 fontface 中的 css 文件中作为 base64。 除了字体之外,我还有大约 150 行 css 行。

pagespeed 声称未使用 60.9 KiB 中的 60.5 KiB(gtmetrix 也是如此,但 60.4 中的 59.8 KiB)。将字体放在 html 中的 style 标签中(正如我一开始计划的那样)不会改变这种情况。

我相信由于这个问题,我的结果从 99-100 下降到 90。删除字体并复制所有样式直到文件大小相同就证明了这一点。有趣的是,所有这些重复项都不被视为未使用。我应该怎么办?

最小可重现示例(在 fontface src 中粘贴真实的 base64):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>font test</title>
    <style>
        @font-face {
            font-family: 'b64';
            font-weight: bold;
            src: url("data:application/octet-stream;base64,AAEAA [...rest of font file]");
        }
        body {
            font-family: "b64";
            font-weight: bold;
        }
    </style>
</head>

<body>
    font test
</body>
</html>

I embedded the font file directly into my css file in fontface as base64.
Besides the font, I have about 150 css lines there.

The pagespeed claims 60.5 KiB of 60.9 KiB is not used (gtmetrix too, but 59.8 of 60.4). Placing the font in html in style tag (as i planned at the beginning) does not change the situation.

I believe that because of this one problem, my result drops from 99-100 to 90. Removing the font and duplicating all styles until the file size is the same proves it. Interestingly, all these duplicates are not considered unused. What should I do?

Minimum reproducible example (paste real base64 in fontface src):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>font test</title>
    <style>
        @font-face {
            font-family: 'b64';
            font-weight: bold;
            src: url("data:application/octet-stream;base64,AAEAA [...rest of font file]");
        }
        body {
            font-family: "b64";
            font-weight: bold;
        }
    </style>
</head>

<body>
    font test
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红玫瑰 2025-01-20 21:08:24

尝试将 MIME 类型设置为以下之一:
安大略省/OTF
应用程序/字体-otf
应用程序/字体
应用程序/otf
应用程序/x-font-otf

Try to set the MIME Type to one of this:
ont/otf
application/font-otf
application/font
application/otf
application/x-font-otf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文