@@ -16,15 +16,14 @@ const DEFAULT_NEXT_CONFIG_FILE = "next.config.js";
1616 * Overrides the user's Next Config file (next.config.[ts|js|mjs]) to add configs
1717 * optimized for Firebase App Hosting.
1818 */
19- export async function overrideNextConfig ( projectRoot : string , nextConfigFileName : string ) {
19+ export async function overrideNextConfig (
20+ projectRoot : string ,
21+ nextConfigFileName : string ,
22+ userNextConfigExists : boolean ,
23+ ) {
2024 console . log ( `Overriding Next Config to add configs optmized for Firebase App Hosting` ) ;
21- // Check if the file exists in the current working directory
22- const configPath = join ( projectRoot , nextConfigFileName ) ;
23-
24- if ( ! ( await exists ( configPath ) ) ) {
25- console . log (
26- `No Next config file found at ${ configPath } , creating one with Firebase App Hosting overrides` ,
27- ) ;
25+ if ( ! userNextConfigExists ) {
26+ console . log ( `No Next config file found, creating one with Firebase App Hosting overrides` ) ;
2827 try {
2928 await writeFile ( join ( projectRoot , DEFAULT_NEXT_CONFIG_FILE ) , defaultNextConfigForFAH ( ) ) ;
3029 console . log (
@@ -45,6 +44,7 @@ export async function overrideNextConfig(projectRoot: string, nextConfigFileName
4544
4645 // Rename the original config file
4746 try {
47+ const configPath = join ( projectRoot , nextConfigFileName ) ;
4848 const originalPath = join ( projectRoot , originalConfigName ) ;
4949 await renamePromise ( configPath , originalPath ) ;
5050
@@ -141,15 +141,29 @@ function defaultNextConfigForFAH() {
141141/**
142142 * This function is used to validate the state of an app after running
143143 * overrideNextConfig. It validates that:
144- * 1. a next config is exists (should be created with FAH overrides
144+ * 1. if user has a next config it is preserved in a next.config.original.[js|ts|mjs] file
145+ * 2. a next config exists (should be created with FAH overrides
145146 * even if user did not create one)
146147 * 3. next config can be loaded by NextJs without any issues.
147148 */
148149export async function validateNextConfigOverride (
149150 root : string ,
150151 projectRoot : string ,
151152 originalConfigFileName : string ,
153+ userNextConfigExists : boolean ,
152154) {
155+ if ( userNextConfigExists ) {
156+ // Ensure user's existing next config is preserved in a next.config.origin.* file
157+ const originalConfigExtension = extname ( originalConfigFileName ) ;
158+ const prservedConfigFileName = `next.config.original${ originalConfigExtension } ` ;
159+ const preservedConfigFilePath = join ( root , prservedConfigFileName ) ;
160+ if ( ! ( await exists ( preservedConfigFilePath ) ) ) {
161+ throw new Error (
162+ `Next Config Override Failed: User's original Next.js config file not preserved ${ preservedConfigFilePath } ` ,
163+ ) ;
164+ }
165+ }
166+
153167 const originalNextConfigFilePath = join ( root , originalConfigFileName ) ;
154168 if ( ! ( await exists ( originalNextConfigFilePath ) ) ) {
155169 throw new Error (
0 commit comments