@@ -3,39 +3,33 @@ import { ImageResponse } from "next/og";
3
3
import type { NextApiRequest } from "next/index" ;
4
4
import { RepoLogo } from "../../_components/logos/og/repo-logo" ;
5
5
import { VercelLogo } from "../../_components/logos/og/vercel-logo" ;
6
- import fs from "fs" ;
7
- import path from "path" ;
8
6
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
+ }
36
16
37
17
export async function GET ( req : NextApiRequest ) : Promise < Response > {
38
18
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
+
39
33
if ( ! req . url ) {
40
34
throw new Error ( "No URL was provided" ) ;
41
35
}
@@ -62,7 +56,7 @@ export async function GET(req: NextApiRequest): Promise<Response> {
62
56
fontFamily : "Geist Sans" ,
63
57
fontWeight : 700 ,
64
58
fontSize : 60 ,
65
- backgroundImage : `url(${ getBackgroundImage ( ) } )` ,
59
+ backgroundImage : `url(${ bg } )` ,
66
60
backgroundSize : "1200px 630px" ,
67
61
color : "#fff" ,
68
62
} }
0 commit comments