Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Build Artifacts Across Multiple Platforms #50

Closed
KonghaYao opened this issue Jul 15, 2024 · 7 comments
Closed

Inconsistent Build Artifacts Across Multiple Platforms #50

KonghaYao opened this issue Jul 15, 2024 · 7 comments

Comments

@KonghaYao
Copy link

您好,woff-build 是一个非常强的项目,在 cn-font-split 中极大提升了 ttf 至 woff2 性能。但是在中文网字计划的 CDN 中,发现了构建产物发生变化,导致 CDN 缓存失效。最后,我们排查出是 @napi-rs/woff-build 在不同平台,构建产物会不一致,而我们最初使用的 wasm 版本的 @chinese-fonts/wawoff2 是一致的。但是我们对 napi 并不熟悉,所以需要您的帮助! @Brooooooklyn @yisibl
下面是 Windows 和 Linux 环境的对比测试:

Hello, woff-build is an excellent project that significantly improves the performance of converting ttf to woff2 in cn-font-split. However, we have noticed changes in the build artifacts in the CDN of the Chinese Web Fonts Project, which has caused CDN cache invalidation. After investigation, we found that the build artifacts of @napi-rs/woff-build are inconsistent across different platforms, whereas the wasm version of @chinese-fonts/wawoff2 that we initially used is consistent. However, we are not familiar with napi, so we need your help! @Brooooooklyn @yisibl
Below is a comparison test between Windows and Linux environments:

import { convertTTFToWOFF2 } from '@napi-rs/woff-build'
import md5 from 'md5'
import fs from 'fs'
const fixture = fs.readFileSync('./font/SmileySans-Oblique.ttf')

console.log('@napi-rs/woff-build', md5(await convertTTFToWOFF2(fixture)))

import { compress } from '@chinese-fonts/wawoff2';
compress(fixture).then(out => {
    console.log('@chinese-fonts/wawoff2', md5(out))
});

image

@Brooooooklyn
Copy link
Owner

@KonghaYao 不同系统, 编译器, CPU 会导致浮点精度不一致, 从而让 woff 内部 brotli 压缩后二进制不一样, 这个无法在 lib 层级解决. wasm 是跨平台一致的, 所以没有这个问题.

@KonghaYao
Copy link
Author

@Brooooooklyn 好的。估计我们以后的版本将会考虑一下 @napi-rs/woff-build @chinese-fonts/wawoff2 的使用策略。据我们的长期使用结果,@napi-rs/woff-build 可以达到 @chinese-fonts/wawoff2 2 倍的速度,这个是非常惊人的性能提升;但是跨平台构建产物不稳定的话,Netlify 服务器和我们的 CDN 就缓存效果不佳,甚至更改发布新的字体版本的时候导致了一些 404 的问题。

@yisibl
Copy link
Collaborator

yisibl commented Jul 16, 2024

你构建 woff2 文件给 CDN 会在不同的平台去做?如果这是不可避免的,可能需要换一种方式来做缓存了

@KonghaYao
Copy link
Author

对的,Netlify 自动分配的机器会在不同的 Linux 环境运行,导致采用不同的 .node 文件进行 woff2 构建,最终就是产物 hash 不一致导致的 BUG。@yisibl

@Brooooooklyn
Copy link
Owner

你这个情况可能不能用 content hash 了,得根据字体压缩前的内容做 hash

@yisibl
Copy link
Collaborator

yisibl commented Jul 16, 2024

你这个情况可能不能用 content hash 了,得根据字体压缩前的内容做 hash

的确,我这边目前就是根据子集化的文本生成 hash 来做 Redis 缓存的 key。

要么你只能更换稳定的服务器,在固定的系统中构建 woff2 文件。

@KonghaYao
Copy link
Author

@Brooooooklyn @yisibl 感谢两位的帮助,harfbuzz 进行字符子集化的 ttf 结果文件的二进制是固定的,在 woff2 压缩之前进行 hash 获取,这样就可以稳定缓存了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants