@@ -30,7 +30,13 @@ import {
30
30
validateLocales ,
31
31
getNodeModuleBin ,
32
32
} from "../utils" ;
33
- import { BuildResult , FrameworkType , SupportLevel } from "../interfaces" ;
33
+ import {
34
+ BuildResult ,
35
+ Framework ,
36
+ FrameworkContext ,
37
+ FrameworkType ,
38
+ SupportLevel ,
39
+ } from "../interfaces" ;
34
40
35
41
import {
36
42
cleanEscapedChars ,
@@ -67,7 +73,7 @@ import {
67
73
APP_PATHS_MANIFEST ,
68
74
ESBUILD_VERSION ,
69
75
} from "./constants" ;
70
- import { getAllSiteDomains } from "../../hosting/api" ;
76
+ import { getAllSiteDomains , getDeploymentDomain } from "../../hosting/api" ;
71
77
import { logger } from "../../logger" ;
72
78
73
79
const DEFAULT_BUILD_SCRIPT = [ "next build" ] ;
@@ -101,7 +107,11 @@ export async function discover(dir: string) {
101
107
/**
102
108
* Build a next.js application.
103
109
*/
104
- export async function build ( dir : string ) : Promise < BuildResult > {
110
+ export async function build (
111
+ dir : string ,
112
+ target : string ,
113
+ context ?: FrameworkContext
114
+ ) : Promise < BuildResult > {
105
115
await warnIfCustomBuildScript ( dir , name , DEFAULT_BUILD_SCRIPT ) ;
106
116
107
117
const reactVersion = getReactVersion ( dir ) ;
@@ -110,10 +120,27 @@ export async function build(dir: string): Promise<BuildResult> {
110
120
process . env . __NEXT_REACT_ROOT = "true" ;
111
121
}
112
122
123
+ const env = { ...process . env } ;
124
+
125
+ if ( context ?. projectId && context ?. site ) {
126
+ const deploymentDomain = await getDeploymentDomain (
127
+ context . projectId ,
128
+ context . site ,
129
+ context . hostingChannel
130
+ ) ;
131
+
132
+ if ( deploymentDomain ) {
133
+ // Add the deployment domain to VERCEL_URL env variable, which is
134
+ // required for dynamic OG images to work without manual configuration.
135
+ // See: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#default-value
136
+ env [ "VERCEL_URL" ] = deploymentDomain ;
137
+ }
138
+ }
139
+
113
140
const cli = getNodeModuleBin ( "next" , dir ) ;
114
141
115
142
const nextBuild = new Promise ( ( resolve , reject ) => {
116
- const buildProcess = spawn ( cli , [ "build" ] , { cwd : dir } ) ;
143
+ const buildProcess = spawn ( cli , [ "build" ] , { cwd : dir , env } ) ;
117
144
buildProcess . stdout ?. on ( "data" , ( data ) => logger . info ( data . toString ( ) ) ) ;
118
145
buildProcess . stderr ?. on ( "data" , ( data ) => logger . info ( data . toString ( ) ) ) ;
119
146
buildProcess . on ( "error" , ( err ) => {
@@ -488,7 +515,12 @@ export async function ɵcodegenPublicDirectory(
488
515
/**
489
516
* Create a directory for SSR content.
490
517
*/
491
- export async function ɵcodegenFunctionsDirectory ( sourceDir : string , destDir : string ) {
518
+ export async function ɵcodegenFunctionsDirectory (
519
+ sourceDir : string ,
520
+ destDir : string ,
521
+ target : string ,
522
+ context ?: FrameworkContext
523
+ ) : ReturnType < NonNullable < Framework [ "ɵcodegenFunctionsDirectory" ] > > {
492
524
const { distDir } = await getConfig ( sourceDir ) ;
493
525
const packageJson = await readJSON ( join ( sourceDir , "package.json" ) ) ;
494
526
// Bundle their next.config.js with esbuild via NPX, pinned version was having troubles on m1
@@ -558,9 +590,25 @@ export async function ɵcodegenFunctionsDirectory(sourceDir: string, destDir: st
558
590
packageJson . dependencies [ "sharp" ] = SHARP_VERSION ;
559
591
}
560
592
593
+ const dotEnv : Record < string , string > = { } ;
594
+ if ( context ?. projectId && context ?. site ) {
595
+ const deploymentDomain = await getDeploymentDomain (
596
+ context . projectId ,
597
+ context . site ,
598
+ context . hostingChannel
599
+ ) ;
600
+
601
+ if ( deploymentDomain ) {
602
+ // Add the deployment domain to VERCEL_URL env variable, which is
603
+ // required for dynamic OG images to work without manual configuration.
604
+ // See: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#default-value
605
+ dotEnv [ "VERCEL_URL" ] = deploymentDomain ;
606
+ }
607
+ }
608
+
561
609
await mkdirp ( join ( destDir , distDir ) ) ;
562
610
await copy ( join ( sourceDir , distDir ) , join ( destDir , distDir ) ) ;
563
- return { packageJson, frameworksEntry : "next.js" } ;
611
+ return { packageJson, frameworksEntry : "next.js" , dotEnv } ;
564
612
}
565
613
566
614
/**
0 commit comments