Skip to content

Commit 9b7d224

Browse files
committed
fix(docs): asset importing failure for OG images
1 parent 2d9015d commit 9b7d224

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

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

+28-32
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,35 @@ import { VercelLogo } from "../../_components/logos/og/vercel-logo";
66
import fs from "fs";
77
import path from "path";
88

9-
export type Products = "repo";
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+
};
1014

11-
export async function GET(req: NextApiRequest): Promise<Response> {
12-
const fontPath = path.join(
13-
process.cwd(),
14-
"node_modules",
15-
"geist",
16-
"dist",
17-
"fonts",
18-
"geist-sans",
19-
"Geist-Regular.ttf"
20-
);
21-
const geistSans = fs.readFileSync(fontPath);
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+
);
2224

23-
const fontMonoPath = path.join(
24-
process.cwd(),
25-
"node_modules",
26-
"geist",
27-
"dist",
28-
"fonts",
29-
"geist-mono",
30-
"GeistMono-Regular.ttf"
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")
3131
);
32-
const geistMono = fs.readFileSync(fontMonoPath);
32+
return { geistSans, geistMono };
33+
};
3334

34-
const bgImagePath = path.join(process.cwd(), "app", "api", "og", "bg.jpeg");
35-
const bgImageBuffer = fs.readFileSync(bgImagePath);
36-
const bg = `data:image/jpeg;base64,${bgImageBuffer.toString("base64")}`;
35+
const { geistSans, geistMono } = loadFonts();
36+
37+
export async function GET(req: NextApiRequest): Promise<Response> {
3738
try {
3839
if (!req.url) {
3940
throw new Error("No URL was provided");
@@ -61,12 +62,11 @@ export async function GET(req: NextApiRequest): Promise<Response> {
6162
fontFamily: "Geist Sans",
6263
fontWeight: 700,
6364
fontSize: 60,
64-
backgroundImage: `url(${bg})`,
65+
backgroundImage: `url(${getBackgroundImage()})`,
6566
backgroundSize: "1200px 630px",
6667
color: "#fff",
6768
}}
6869
>
69-
{}
7070
<div
7171
style={{ display: "flex", height: 97 * 1.1, alignItems: "center" }}
7272
>
@@ -121,16 +121,12 @@ export async function GET(req: NextApiRequest): Promise<Response> {
121121
}
122122
);
123123
} catch (err: unknown) {
124-
if (process.env.NODE_ENV === "development") {
125-
// eslint-disable-next-line no-console
126-
console.error(err);
127-
}
128-
124+
// Protects us from serving no image at all if something is broken.
129125
if (process.env.VERCEL) {
130126
return new Response(undefined, {
131127
status: 302,
132128
headers: {
133-
Location: "https://turbo.build/og-image.png",
129+
Location: "/og-image.png",
134130
},
135131
});
136132
}

docs/site/public/og-image.png

-69 KB
Loading

0 commit comments

Comments
 (0)