@@ -14,23 +14,28 @@ type TraceInfo = { version: number; files: string[] };
1414 * Patches the usage of @vercel/og to be compatible with Cloudflare Workers.
1515 *
1616 * @param buildOpts Build options.
17+ * @returns Whether the @vercel/og library is used.
1718 */
18- export function patchVercelOgLibrary ( buildOpts : BuildOptions ) {
19+ export function patchVercelOgLibrary ( buildOpts : BuildOptions ) : boolean {
1920 const { appBuildOutputPath, outputDir } = buildOpts ;
2021
2122 const functionsPath = path . join ( outputDir , "server-functions/default" ) ;
2223 const packagePath = path . join ( functionsPath , getPackagePath ( buildOpts ) ) ;
2324
25+ let useOg = false ;
26+
2427 for ( const traceInfoPath of globSync ( path . join ( appBuildOutputPath , ".next/server/**/*.nft.json" ) , {
2528 windowsPathsNoEscape : true ,
2629 } ) ) {
27- let edgeFilePatched = false ;
28-
30+ // Look for the Node version of the traced @vercel /og files
2931 const traceInfo : TraceInfo = JSON . parse ( readFileSync ( traceInfoPath , { encoding : "utf8" } ) ) ;
3032 const tracedNodePath = traceInfo . files . find ( ( p ) => p . endsWith ( "@vercel/og/index.node.js" ) ) ;
31-
3233 if ( ! tracedNodePath ) continue ;
3334
35+ // If we are here, it means the application is using the @vercel/og library
36+ // and there is an `index.edge.js` colocated file that we need to copy and patch.
37+ useOg = true ;
38+
3439 const outputDir = getOutputDir ( { functionsPath, packagePath } ) ;
3540 const outputEdgePath = path . join ( outputDir , "index.edge.js" ) ;
3641
@@ -44,12 +49,11 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
4449 copyFileSync ( tracedEdgePath , outputEdgePath ) ;
4550 }
4651
47- if ( ! edgeFilePatched ) {
48- edgeFilePatched = true ;
49- // Change font fetches in the library to use imports.
50- const node = parseFile ( outputEdgePath ) ;
51- const { edits, matches } = patchVercelOgFallbackFont ( node ) ;
52- writeFileSync ( outputEdgePath , node . commitEdits ( edits ) ) ;
52+ // Change font fetches in the library to use imports.
53+ {
54+ const ast = parseFile ( outputEdgePath ) ;
55+ const { edits, matches } = patchVercelOgFallbackFont ( ast ) ;
56+ writeFileSync ( outputEdgePath , ast . commitEdits ( edits ) ) ;
5357
5458 if ( matches . length > 0 ) {
5559 const fontFileName = matches [ 0 ] ! . getMatch ( "PATH" ) ! . text ( ) ;
@@ -59,12 +63,16 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
5963
6064 // Change node imports for the library to edge imports.
6165 // This is only useful when turbopack is not used to bundle the function.
62- const routeFilePath = traceInfoPath . replace ( appBuildOutputPath , packagePath ) . replace ( ".nft.json" , "" ) ;
66+ {
67+ const routeFilePath = traceInfoPath . replace ( appBuildOutputPath , packagePath ) . replace ( ".nft.json" , "" ) ;
6368
64- const node = parseFile ( routeFilePath ) ;
65- const { edits } = patchVercelOgImport ( node ) ;
66- writeFileSync ( routeFilePath , node . commitEdits ( edits ) ) ;
69+ const ast = parseFile ( routeFilePath ) ;
70+ const { edits } = patchVercelOgImport ( ast ) ;
71+ writeFileSync ( routeFilePath , ast . commitEdits ( edits ) ) ;
72+ }
6773 }
74+
75+ return useOg ;
6876}
6977
7078function getOutputDir ( opts : { functionsPath : string ; packagePath : string } ) {
0 commit comments