Skip to content

Commit 73ccf2b

Browse files
committed
WIP cd840
1 parent 9b7d224 commit 73ccf2b

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

docs/site/app/api/og/route.tsx

+24-30
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,33 @@ import { ImageResponse } from "next/og";
33
import type { NextApiRequest } from "next/index";
44
import { RepoLogo } from "../../_components/logos/og/repo-logo";
55
import { VercelLogo } from "../../_components/logos/og/vercel-logo";
6-
import fs from "fs";
7-
import path from "path";
86

9-
const getBackgroundImage = () => {
10-
const bgImagePath = path.join(process.cwd(), "app", "api", "og", "bg.jpeg");
11-
const bgImageBuffer = fs.readFileSync(bgImagePath);
12-
return `data:image/jpeg;base64,${bgImageBuffer.toString("base64")}`;
13-
};
14-
15-
// Choosing to pull these from node_modules so that its always consistent across the site
16-
// TODO(maybe?): Import them like any other package? import {} from 'geist'
17-
const GEIST_BASE = path.join(
18-
process.cwd(),
19-
"node_modules",
20-
"geist",
21-
"dist",
22-
"fonts"
23-
);
24-
25-
const loadFonts = () => {
26-
const geistSans = fs.readFileSync(
27-
path.join(GEIST_BASE, "geist-sans", "Geist-Regular.ttf")
28-
);
29-
const geistMono = fs.readFileSync(
30-
path.join(GEIST_BASE, "geist-mono", "GeistMono-Regular.ttf")
31-
);
32-
return { geistSans, geistMono };
33-
};
34-
35-
const { geistSans, geistMono } = loadFonts();
7+
function _arrayBufferToBase64(buffer: ArrayBuffer): string {
8+
let binary = "";
9+
const bytes = new Uint8Array(buffer);
10+
const len = bytes.byteLength;
11+
for (let i = 0; i < len; i++) {
12+
binary += String.fromCharCode(bytes[i]);
13+
}
14+
return btoa(binary);
15+
}
3616

3717
export async function GET(req: NextApiRequest): Promise<Response> {
3818
try {
19+
const [geistSans, geistMono, bg] = await Promise.all([
20+
fetch(new URL("./Geist-Regular.ttf", import.meta.url)).then((res) =>
21+
res.arrayBuffer()
22+
),
23+
fetch(new URL("./GeistMono-Regular.ttf", import.meta.url)).then((res) =>
24+
res.arrayBuffer()
25+
),
26+
_arrayBufferToBase64(
27+
await fetch(new URL("./bg.jpeg", import.meta.url)).then((res) =>
28+
res.arrayBuffer()
29+
)
30+
),
31+
]);
32+
3933
if (!req.url) {
4034
throw new Error("No URL was provided");
4135
}
@@ -62,7 +56,7 @@ export async function GET(req: NextApiRequest): Promise<Response> {
6256
fontFamily: "Geist Sans",
6357
fontWeight: 700,
6458
fontSize: 60,
65-
backgroundImage: `url(${getBackgroundImage()})`,
59+
backgroundImage: `url(${bg})`,
6660
backgroundSize: "1200px 630px",
6761
color: "#fff",
6862
}}

0 commit comments

Comments
 (0)