Skip to content

Commit a2dc2eb

Browse files
committed
add validate next config override function
1 parent fff3505 commit a2dc2eb

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
} from "../utils.js";
99
import { join } from "path";
1010
import { getBuildOptions, runBuild } from "@apphosting/common";
11-
import { addRouteOverrides, overrideNextConfig } from "../overrides.js";
11+
import {
12+
addRouteOverrides,
13+
overrideNextConfig,
14+
validateNextConfigOverrides,
15+
} from "../overrides.js";
1216

1317
const root = process.cwd();
1418
const opts = getBuildOptions();
@@ -21,19 +25,26 @@ if (!process.env.FRAMEWORK_VERSION) {
2125
throw new Error("Could not find the nextjs version of the application");
2226
}
2327

24-
const { distDir, configFileName } = await loadConfig(root, opts.projectDirectory);
25-
await overrideNextConfig(root, configFileName);
28+
const originalConfig = await loadConfig(root, opts.projectDirectory);
29+
30+
await overrideNextConfig(root, originalConfig.configFileName);
31+
await validateNextConfigOverrides(root, opts.projectDirectory);
32+
2633
await runBuild();
2734

2835
const adapterMetadata = getAdapterMetadata();
29-
const nextBuildDirectory = join(opts.projectDirectory, distDir);
36+
const nextBuildDirectory = join(opts.projectDirectory, originalConfig.distDir);
3037
const outputBundleOptions = populateOutputBundleOptions(
3138
root,
3239
opts.projectDirectory,
3340
nextBuildDirectory,
3441
);
3542

36-
await addRouteOverrides(outputBundleOptions.outputDirectoryAppPath, distDir, adapterMetadata);
43+
await addRouteOverrides(
44+
outputBundleOptions.outputDirectoryAppPath,
45+
originalConfig.distDir,
46+
adapterMetadata,
47+
);
3748

3849
await generateBuildOutput(
3950
root,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ describe("next config overrides", () => {
314314
`),
315315
);
316316
});
317+
318+
it("should not do anything if no next.config.* file exists", async () => {
319+
const { overrideNextConfig } = await importOverrides;
320+
await overrideNextConfig(tmpDir, "next.config.js");
321+
322+
// Assert that no next.config* files were created
323+
const files = fs.readdirSync(tmpDir);
324+
const nextConfigFiles = files.filter((file) => file.startsWith("next.config"));
325+
assert.strictEqual(nextConfigFiles.length, 0, "No next.config files should exist");
326+
});
317327
});
318328

319329
// Normalize whitespace for comparison

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import {
55
loadMiddlewareManifest,
66
exists,
77
writeFile,
8+
loadConfig,
89
} from "./utils.js";
910
import { join, extname } from "path";
1011
import { rename as renamePromise } from "fs/promises";
12+
import { NextConfigComplete } from "next/dist/server/config-shared.js";
1113

1214
/**
1315
* Overrides the user's Next Config file (next.config.[ts|js|mjs]) to add configs
@@ -98,10 +100,26 @@ function getCustomNextConfig(importStatement: string, fileExtension: string) {
98100
}
99101
: fahOptimizedConfig(originalConfig);
100102
103+
const firebaseAppHostingSymbol = Symbol("__createdByFirebaseAppHosting__");
104+
config[firebaseAppHostingSymbol] = true;
105+
101106
${fileExtension === ".mjs" ? "export default config;" : "module.exports = config;"}
102107
`;
103108
}
104109

110+
export async function validateNextConfigOverrides(root: string, projectRoot: string) {
111+
try {
112+
const overriddenConfig = await loadConfig(root, projectRoot);
113+
if (!overriddenConfig.__createdByFirebaseAppHosting__) {
114+
throw new Error("Firebase App Hosting overrides are missing");
115+
}
116+
} catch (error) {
117+
throw new Error(
118+
`Invalid Next.js config: ${error instanceof Error ? error.message : "Unknown error"}`,
119+
);
120+
}
121+
}
122+
105123
/**
106124
* Modifies the app's route manifest (routes-manifest.json) to add Firebase App Hosting
107125
* specific overrides (i.e headers).

0 commit comments

Comments
 (0)