Skip to content

Commit 3769262

Browse files
committed
Restore user's original Next config.
1 parent cbeec0b commit 3769262

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

packages/@apphosting/adapter-nextjs/src/bin/build.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "../utils.js";
1010
import { join } from "path";
1111
import { getBuildOptions, runBuild } from "@apphosting/common";
12-
import { addRouteOverrides, overrideNextConfig, validateNextConfigOverride } from "../overrides.js";
12+
import { addRouteOverrides, overrideNextConfig, restoreNextConfig, validateNextConfigOverride } from "../overrides.js";
1313

1414
const root = process.cwd();
1515
const opts = getBuildOptions();
@@ -19,29 +19,33 @@ process.env.NEXT_PRIVATE_STANDALONE = "true";
1919
// Opt-out sending telemetry to Vercel
2020
process.env.NEXT_TELEMETRY_DISABLED = "1";
2121

22-
const originalConfig = await loadConfig(root, opts.projectDirectory);
22+
const nextConfig = await loadConfig(root, opts.projectDirectory);
2323

2424
/**
2525
* Override user's Next Config to optimize the app for Firebase App Hosting
2626
* and validate that the override resulted in a valid config that Next.js can
2727
* load.
2828
*
29+
* We restore the user's Next Config at the end of the build, after the config file has been
30+
* copied over to the output directory, so that the user's original code is not modified.
31+
*
2932
* If the app does not have a next.config.[js|mjs|ts] file in the first place,
3033
* then can skip config override.
3134
*
3235
* Note: loadConfig always returns a fileName (default: next.config.js) even if
3336
* one does not exist in the app's root: https://github.com/vercel/next.js/blob/23681508ca34b66a6ef55965c5eac57de20eb67f/packages/next/src/server/config.ts#L1115
3437
*/
35-
const originalConfigPath = join(root, originalConfig.configFileName);
36-
if (await exists(originalConfigPath)) {
37-
await overrideNextConfig(root, originalConfig.configFileName);
38-
await validateNextConfigOverride(root, opts.projectDirectory, originalConfig.configFileName);
38+
const nextConfigPath = join(root, nextConfig.configFileName);
39+
if (await exists(nextConfigPath)) {
40+
await overrideNextConfig(root, nextConfig.configFileName);
41+
await validateNextConfigOverride(root, opts.projectDirectory, nextConfig.configFileName);
3942
}
4043

4144
await runBuild();
4245

46+
4347
const adapterMetadata = getAdapterMetadata();
44-
const nextBuildDirectory = join(opts.projectDirectory, originalConfig.distDir);
48+
const nextBuildDirectory = join(opts.projectDirectory, nextConfig.distDir);
4549
const outputBundleOptions = populateOutputBundleOptions(
4650
root,
4751
opts.projectDirectory,
@@ -50,7 +54,7 @@ const outputBundleOptions = populateOutputBundleOptions(
5054

5155
await addRouteOverrides(
5256
outputBundleOptions.outputDirectoryAppPath,
53-
originalConfig.distDir,
57+
nextConfig.distDir,
5458
adapterMetadata,
5559
);
5660

@@ -64,3 +68,5 @@ await generateBuildOutput(
6468
adapterMetadata,
6569
);
6670
await validateOutputDirectory(outputBundleOptions, nextBuildDirectory);
71+
72+
await restoreNextConfig(root, nextConfig.configFileName);

packages/@apphosting/adapter-nextjs/src/overrides.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ export async function validateNextConfigOverride(
142142
}
143143
}
144144

145+
/**
146+
*
147+
*/
148+
export async function restoreNextConfig(projectRoot: string, nextConfigFileName: string) {
149+
// Check if the file exists in the current working directory
150+
const configPath = join(projectRoot, nextConfigFileName);
151+
if (!(await exists(configPath))) {
152+
return;
153+
}
154+
155+
// Determine the file extension
156+
const fileExtension = extname(nextConfigFileName);
157+
const originalConfigPath = join(projectRoot, `next.config.original${fileExtension}`);
158+
if (!(await exists(originalConfigPath))) {
159+
console.warn(`next config may have been overwritten but original contents not found`);
160+
return;
161+
}
162+
console.log(`Restoring original next config in project root`);
163+
164+
try {
165+
await renamePromise(originalConfigPath, configPath);
166+
} catch (error) {
167+
console.error(`Error restoring Next config: ${error}`);
168+
}
169+
return;
170+
171+
}
172+
145173
/**
146174
* Modifies the app's route manifest (routes-manifest.json) to add Firebase App Hosting
147175
* specific overrides (i.e headers).

0 commit comments

Comments
 (0)