From 388a8c17c13b04077d8d85454ecd1f4d5a791cfc Mon Sep 17 00:00:00 2001 From: Ekin Dursun Date: Wed, 25 Nov 2026 15:57:24 +0300 Subject: [PATCH] fix(nextjs): keep the config's shape in the generated next.config The generated config always wrapped a function config in an async function, and spread anything else as an object, so a config exported as a promise lost every option. It now returns a promise exactly when the original does: an object stays an object, a promise stays a promise, and a function keeps its sync or async result. With the shape kept, a .ts or .mts config exports the original's type (`export default config as typeof originalConfig`), so code importing next.config sees the same type it does without the adapter. --- .../adapter-nextjs/src/overrides.spec.ts | 67 +++++++++++++++---- .../adapter-nextjs/src/overrides.ts | 27 ++++++-- 2 files changed, 75 insertions(+), 19 deletions(-) diff --git a/packages/@apphosting/adapter-nextjs/src/overrides.spec.ts b/packages/@apphosting/adapter-nextjs/src/overrides.spec.ts index 48be7c11e..7506594a2 100644 --- a/packages/@apphosting/adapter-nextjs/src/overrides.spec.ts +++ b/packages/@apphosting/adapter-nextjs/src/overrides.spec.ts @@ -2,6 +2,7 @@ import assert from "assert"; import fs from "fs"; import path from "path"; import os from "os"; +import { pathToFileURL } from "url"; import type { MiddlewareMatcher } from "next/dist/build/analysis/get-page-static-info.js"; import { RoutesManifest, MiddlewareManifest } from "./interfaces.js"; const importOverrides = import("@apphosting/adapter-nextjs/dist/overrides.js"); @@ -289,12 +290,12 @@ describe("next config overrides", () => { }, }); + const fahOptimize = (value) => + typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value); + const config = typeof originalConfig === 'function' - ? async (...args) => { - const resolvedConfig = await originalConfig(...args); - return fahOptimizedConfig(resolvedConfig); - } - : fahOptimizedConfig(originalConfig); + ? (...args) => fahOptimize(originalConfig(...args)) + : fahOptimize(originalConfig); `; beforeEach(() => { @@ -421,7 +422,7 @@ describe("next config overrides", () => { ${nextConfigOverrideBody} - export default config; + export default config as typeof originalConfig; `), ); }); @@ -451,11 +452,53 @@ describe("next config overrides", () => { ${nextConfigOverrideBody} - export default config; + export default config as typeof originalConfig; `), ); }); + const shapes = [ + { + shape: "object", + original: `export default { distDir: "out" };`, + isFunction: false, + isPromise: false, + }, + { + shape: "promise", + original: `export default Promise.resolve({ distDir: "out" });`, + isFunction: false, + isPromise: true, + }, + { + shape: "sync function", + original: `export default (phase) => ({ distDir: phase });`, + isFunction: true, + isPromise: false, + }, + { + shape: "async function", + original: `export default async (phase) => ({ distDir: phase });`, + isFunction: true, + isPromise: true, + }, + ]; + for (const { shape, original, isFunction, isPromise } of shapes) { + it(`should keep the shape of the config - ${shape}`, async () => { + const { overrideNextConfig } = await importOverrides; + fs.writeFileSync(path.join(tmpDir, "next.config.mjs"), original); + await overrideNextConfig(tmpDir, "next.config.mjs"); + + const { default: config } = await import( + pathToFileURL(path.join(tmpDir, "next.config.mjs")).href + ); + assert.equal(typeof config === "function", isFunction); + const result = isFunction ? config("out") : config; + assert.equal(typeof result?.then === "function", isPromise); + assert.deepEqual(await result, { distDir: "out", images: { unoptimized: true } }); + }); + } + it("should restore the original config when the override fails", async () => { const { overrideNextConfig } = await importOverrides; const originalConfig = `module.exports = { /* config options here */ }`; @@ -559,12 +602,12 @@ describe("next config restore", () => { }, }); + const fahOptimize = (value) => + typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value); + const config = typeof originalConfig === 'function' - ? async (...args) => { - const resolvedConfig = await originalConfig(...args); - return fahOptimizedConfig(resolvedConfig); - } - : fahOptimizedConfig(originalConfig); + ? (...args) => fahOptimize(originalConfig(...args)) + : fahOptimize(originalConfig); `; beforeEach(() => { diff --git a/packages/@apphosting/adapter-nextjs/src/overrides.ts b/packages/@apphosting/adapter-nextjs/src/overrides.ts index 973ff4dac..a48801377 100644 --- a/packages/@apphosting/adapter-nextjs/src/overrides.ts +++ b/packages/@apphosting/adapter-nextjs/src/overrides.ts @@ -80,7 +80,20 @@ export async function overrideNextConfig(projectRoot: string, nextConfigFileName * @return The custom Next.js config. */ function getCustomNextConfig(importStatement: string, fileExtension: string) { - const isEsm = [".mjs", ".mts", ".ts"].includes(fileExtension); + let exportStatement; + switch (fileExtension) { + case ".js": + exportStatement = "module.exports = config;"; + break; + case ".mjs": + exportStatement = "export default config;"; + break; + default: + // .ts and .mts. The wrapper keeps the original's shape (object, promise, + // function, and a function's sync or async result), so importers of + // next.config see the same type they see without the adapter. + exportStatement = "export default config as typeof originalConfig;"; + } return ` // @ts-nocheck ${importStatement} @@ -96,14 +109,14 @@ function getCustomNextConfig(importStatement: string, fileExtension: string) { }, }); + const fahOptimize = (value) => + typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value); + const config = typeof originalConfig === 'function' - ? async (...args) => { - const resolvedConfig = await originalConfig(...args); - return fahOptimizedConfig(resolvedConfig); - } - : fahOptimizedConfig(originalConfig); + ? (...args) => fahOptimize(originalConfig(...args)) + : fahOptimize(originalConfig); - ${isEsm ? "export default config;" : "module.exports = config;"} + ${exportStatement} `; }