Skip to content

fix(nextjs): keep the config's shape in the generated next.config - #731

Open
onlined wants to merge 1 commit into
firebase:mainfrom
onlined:fix/two-way-config-wrapper
Open

onlined wants to merge 1 commit into
firebase:mainfrom
onlined:fix/two-way-config-wrapper

Conversation

@onlined

@onlined onlined commented Nov 25, 2026

Copy link
Copy Markdown

The generated config (overrides.ts L99-L104) wraps a function config in an async function and passes anything else to fahOptimizedConfig as an object. Next.js accepts three shapes for the default export: an object, a promise, or a function returning either (config-shared.ts). Two of them come out of the wrapper changed:

  • A promise is spread as an object. A promise has no own properties, so the build runs with { images: { unoptimized: true } } and none of the project's options. Nothing fails: with export default Promise.resolve({ async redirects() { … } }) in next.config.mjs, dist/bin/build.js on main succeeds and .next/routes-manifest.json has no redirects, while next build alone has the redirect.

  • A sync function becomes async. The wrapper therefore can't claim the original's type. The .ts and .mts overrides end in export default config;, whose type is any, because the file is // @ts-nocheck and fahOptimizedConfig is untyped. A project file that imports the config and type-checks locally can fail under strict mode in the App Hosting build. With any, most uses still compile; the build fails where TypeScript has to infer a type from the config, such as the parameter of a callback over redirects(). With a next.config.ts typed NextConfig and:

    // src/lib/uses-config.ts
    import nextConfig from "../../next.config";
    
    export async function oldRedirect() {
      const redirects = (await nextConfig.redirects?.()) ?? [];
      return redirects.find((r) => r.source === "/old");
    }

    tsc passes locally, and dist/bin/build.js on main fails:

    ./src/lib/uses-config.ts:5:26
    Type error: Parameter 'r' implicitly has an 'any' type.
    

This change makes the wrapper return a promise exactly when the original does:

const fahOptimize = (value) =>
  typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value);

const config = typeof originalConfig === 'function'
  ? (...args) => fahOptimize(originalConfig(...args))
  : fahOptimize(originalConfig);

An object stays an object, a promise stays a promise, and a function keeps its sync or async result. typeof value?.then === 'function' is the same thenable test await applies, so the wrapper treats exactly the same values as promises as Next.js's await config does.

With the shape kept, the .ts and .mts overrides end in export default config as typeof originalConfig;, so a file importing next.config sees the same type it sees without the adapter. .mjs keeps export default config; and .js keeps module.exports = config;. // @ts-nocheck stays, for two reasons: the body has untyped parameters, and for an object config TypeScript would check the function branch as a call on a non-callable type (TS2349).

What changes for existing projects:

  • Object and async-function configs: nothing; the generated file behaves the same.
  • Sync-function configs: the wrapper returns the config instead of a promise of it. Next.js awaits either.
  • Promise configs: the project's options now apply. A project currently deployed with them dropped will see them take effect on its next rollout.
  • Files importing a .ts/.mts config: typed as the original config instead of any. Code that type-checks against the config locally keeps type-checking.

Testing

  • Unit: the five generated-file expectations now expect the new body, and the .ts and .mts ones the new last line. New cases generate the override for an object, a promise, a sync function and an async function, import it, and check three things: it's a function exactly when the original is, it returns a promise exactly when the original does, and it resolves to the config with images.unoptimized: true. Against main's template, the promise and sync-function cases fail.

  • Functional: all 11 scenarios pass.

  • Lint and environment: npm run lint:quiet and Prettier pass. I ran the unit and functional tests locally on Node 26.13.1 only; CI runs Node 18 and 22. The new spec adds three no-unsafe-* warnings where it imports the generated file, like the file's existing dynamic import.

  • Adapter builds: I ran dist/bin/build.js from main and from this branch on a minimal App Router app (Next 15.5.24):

    next.config main this branch
    .ts typed NextConfig, plus a project file importing it TS7006, build fails builds; redirect in routes-manifest.json, images.unoptimized: true, original config restored
    .mjs exporting Promise.resolve({ redirects }) builds without the redirect builds with the redirect
  • Larger app: I also ran this branch's override on a larger App Router app (Next 16.4.6) whose tests import its next.config.ts. tsc and next build pass.

Not tested: an actual App Hosting rollout.

@google-cla

google-cla Bot commented Nov 25, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors how Next.js configuration overrides are generated and exported, ensuring that the original configuration's shape (object, promise, sync function, or async function) is preserved across different file extensions (.js, .mjs, .ts, .mts). It also adds comprehensive tests to verify these shapes. The review feedback correctly identifies a potential TypeError if the configuration resolves to a falsy value, and suggests adding a defensive guard to fahOptimize in both the implementation and the corresponding test templates.

Comment on lines +293 to +294
const fahOptimize = (value) =>
typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Update the expected template string for fahOptimize to match the defensive check for falsy values.

Suggested change
const fahOptimize = (value) =>
typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value);
const fahOptimize = (value) =>
!value ? value : (typeof value.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value));

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.
@onlined
onlined force-pushed the fix/two-way-config-wrapper branch from 3841484 to 388a8c1 Compare November 25, 2026 15:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant