Skip to content

Commit 2cbbfa5

Browse files
committed
fix(landing): ship harfbuzz hb.wasm in Vercel function so /api/og stops 500ing
satori 0.33 shapes text with harfbuzzjs, which reads hb.wasm beside its hb.js through a computed path. @vercel/nft cannot trace that read, so the deployed function contained hb.js but no hb.wasm and every /api/og request failed with ENOENT. Wrap adapter-vercel to copy hb.wasm next to each traced hb.js.
1 parent c2b630a commit 2cbbfa5

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

‎landing/vite.config.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
11
import adapter from "@sveltejs/adapter-vercel";
22
import { sveltekit } from "@sveltejs/kit/vite";
3+
import { cpSync, existsSync, readdirSync, statSync } from "node:fs";
4+
import { createRequire } from "node:module";
5+
import { dirname, join } from "node:path";
36
import tailwindcss from "@tailwindcss/vite";
47
import { defineConfig } from "vite";
58

9+
// satori 0.33 shapes text with harfbuzzjs, which reads hb.wasm from beside its
10+
// own hb.js at runtime via a computed path. @vercel/nft cannot see that read,
11+
// so the Vercel function ships hb.js without hb.wasm and /api/og dies with
12+
// ENOENT -> 500. Copy the wasm next to every traced copy of hb.js.
13+
const requireFromHere = createRequire(import.meta.url);
14+
15+
function findHarfbuzzDirs(root: string): string[] {
16+
const found: string[] = [];
17+
const walk = (dir: string): void => {
18+
for (const name of readdirSync(dir)) {
19+
const full = join(dir, name);
20+
if (name === "hb.js" && dir.endsWith("harfbuzzjs")) {
21+
found.push(dir);
22+
} else if (statSync(full).isDirectory()) {
23+
walk(full);
24+
}
25+
}
26+
};
27+
walk(root);
28+
return found;
29+
}
30+
31+
function withHarfbuzzWasm(base: ReturnType<typeof adapter>) {
32+
return {
33+
...base,
34+
async adapt(builder: Parameters<typeof base.adapt>[0]) {
35+
await base.adapt(builder);
36+
const functionsDir = join(process.cwd(), ".vercel/output/functions");
37+
if (!existsSync(functionsDir)) return;
38+
const satoriDir = dirname(requireFromHere.resolve("satori/package.json"));
39+
const wasm = join(
40+
dirname(
41+
requireFromHere.resolve("harfbuzzjs/package.json", {
42+
paths: [satoriDir],
43+
}),
44+
),
45+
"hb.wasm",
46+
);
47+
for (const dir of findHarfbuzzDirs(functionsDir)) {
48+
cpSync(wasm, join(dir, "hb.wasm"));
49+
}
50+
},
51+
};
52+
}
53+
654
export default defineConfig({
755
plugins: [
856
tailwindcss(),
957
sveltekit({
10-
adapter: adapter(),
58+
adapter: withHarfbuzzWasm(adapter()),
1159
alias: {
1260
// Kit 3 removed the built-in $lib; the config alias still works
1361
// (deprecated) and keeps every existing `$lib/...` import resolving.

0 commit comments

Comments
 (0)