@@ -6,34 +6,35 @@ import { VercelLogo } from "../../_components/logos/og/vercel-logo";
6
6
import fs from "fs" ;
7
7
import path from "path" ;
8
8
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
+ } ;
10
14
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
+ ) ;
22
24
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" )
31
31
) ;
32
- const geistMono = fs . readFileSync ( fontMonoPath ) ;
32
+ return { geistSans, geistMono } ;
33
+ } ;
33
34
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 > {
37
38
try {
38
39
if ( ! req . url ) {
39
40
throw new Error ( "No URL was provided" ) ;
@@ -61,12 +62,11 @@ export async function GET(req: NextApiRequest): Promise<Response> {
61
62
fontFamily : "Geist Sans" ,
62
63
fontWeight : 700 ,
63
64
fontSize : 60 ,
64
- backgroundImage : `url(${ bg } )` ,
65
+ backgroundImage : `url(${ getBackgroundImage ( ) } )` ,
65
66
backgroundSize : "1200px 630px" ,
66
67
color : "#fff" ,
67
68
} }
68
69
>
69
- { }
70
70
< div
71
71
style = { { display : "flex" , height : 97 * 1.1 , alignItems : "center" } }
72
72
>
@@ -121,16 +121,12 @@ export async function GET(req: NextApiRequest): Promise<Response> {
121
121
}
122
122
) ;
123
123
} 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.
129
125
if ( process . env . VERCEL ) {
130
126
return new Response ( undefined , {
131
127
status : 302 ,
132
128
headers : {
133
- Location : "https://turbo.build /og-image.png" ,
129
+ Location : "/og-image.png" ,
134
130
} ,
135
131
} ) ;
136
132
}
0 commit comments