修复 UI 编辑器字体加载失败时的资源清理逻辑

- 在失败路径释放 URL.revokeObjectURL(url)。
- 若已注册字体面,也会从 document.fonts 移除。
This commit is contained in:
2026-08-17 17:05:58 +08:00
parent 80ff184605
commit 0b2c08472c
@@ -69,6 +69,8 @@ export function useUiEditorFontFaces(
for (const font of fontAssets) {
void (async () => {
const cssFamily = uiEditorPrivateFontFamily(font.asset_id);
let url: string | undefined;
let face: FontFace | undefined;
try {
if (typeof FontFace === 'undefined' || !document.fonts) {
throw new Error('FontFace unavailable');
@@ -80,10 +82,10 @@ export function useUiEditorFontFaces(
expectedSha256: font.content_sha256,
});
if (cancelled) return;
const url = URL.createObjectURL(
url = URL.createObjectURL(
new Blob([new Uint8Array(bytes)], { type: fontMimeType(font) }),
);
const face = new FontFace(cssFamily, `url(${JSON.stringify(url)})`, {
face = new FontFace(cssFamily, `url(${JSON.stringify(url)})`, {
style: font.metadata.italic ? 'italic' : 'normal',
weight: String(font.metadata.weight),
});
@@ -99,6 +101,12 @@ export function useUiEditorFontFaces(
[font.asset_id]: { cssFamily, status: 'loaded' },
}));
} catch {
if (face) {
document.fonts?.delete(face);
}
if (url) {
URL.revokeObjectURL(url);
}
if (!cancelled) {
setStates((current) => ({
...current,