refactor: dedupe og font loading

This commit is contained in:
Nancy
2026-06-25 21:30:07 -07:00
committed by Patrick Erichsen
parent fbd5d51d64
commit da169d4a09
+25 -64
View File
@@ -17,6 +17,23 @@ let fontBuffersPromise: Promise<Uint8Array[]> | null = null;
let publisherFontBuffersPromise: Promise<Uint8Array[]> | null = null;
let resvgInitPromise: Promise<void> | null = null;
const SHARED_FONT_PATHS = [
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-500-normal.woff2",
"node_modules/@fontsource/ibm-plex-mono/files/ibm-plex-mono-latin-500-normal.woff2",
"node_modules/@fontsource/noto-sans-sc/files/noto-sans-sc-chinese-simplified-800-normal.woff2",
"node_modules/@fontsource/noto-sans-sc/files/noto-sans-sc-chinese-simplified-500-normal.woff2",
];
const DEFAULT_FONT_PATHS = [
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-800-normal.woff2",
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-700-normal.woff2",
...SHARED_FONT_PATHS,
];
const PUBLISHER_FONT_PATHS = [
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-700-normal.woff2",
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-800-normal.woff2",
...SHARED_FONT_PATHS,
];
function getServerRootUrl() {
const nitroMain = (globalThis as unknown as GlobalNitroMain).__nitro_main__;
if (typeof nitroMain === "string") {
@@ -94,78 +111,22 @@ export async function ensureResvgWasm() {
await resvgInitPromise;
}
function readFontBuffers(paths: string[]) {
return Promise.all(paths.map((pathname) => readFile(getServerUrl(pathname)))).then((buffers) =>
buffers.map((buffer) => new Uint8Array(buffer)),
);
}
export async function getFontBuffers() {
if (!fontBuffersPromise) {
fontBuffersPromise = Promise.all([
readFile(
getServerUrl(
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-800-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-700-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-500-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/ibm-plex-mono/files/ibm-plex-mono-latin-500-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/noto-sans-sc/files/noto-sans-sc-chinese-simplified-800-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/noto-sans-sc/files/noto-sans-sc-chinese-simplified-500-normal.woff2",
),
),
]).then((buffers) => buffers.map((buffer) => new Uint8Array(buffer)));
fontBuffersPromise = readFontBuffers(DEFAULT_FONT_PATHS);
}
return fontBuffersPromise;
}
export async function getPublisherFontBuffers() {
if (!publisherFontBuffersPromise) {
publisherFontBuffersPromise = Promise.all([
readFile(
getServerUrl(
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-700-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-800-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/bricolage-grotesque/files/bricolage-grotesque-latin-500-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/ibm-plex-mono/files/ibm-plex-mono-latin-500-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/noto-sans-sc/files/noto-sans-sc-chinese-simplified-800-normal.woff2",
),
),
readFile(
getServerUrl(
"node_modules/@fontsource/noto-sans-sc/files/noto-sans-sc-chinese-simplified-500-normal.woff2",
),
),
]).then((buffers) => buffers.map((buffer) => new Uint8Array(buffer)));
publisherFontBuffersPromise = readFontBuffers(PUBLISHER_FONT_PATHS);
}
return publisherFontBuffersPromise;
}