Repository navigation
Conversation
|
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. |
There was a problem hiding this comment.
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.
| const fahOptimize = (value) => | ||
| typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value); |
There was a problem hiding this comment.
Update the expected template string for fahOptimize to match the defensive check for falsy values.
| 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.
3841484 to
388a8c1
Compare
The generated config (overrides.ts L99-L104) wraps a function config in an
asyncfunction and passes anything else tofahOptimizedConfigas 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: withexport default Promise.resolve({ async redirects() { … } })innext.config.mjs,dist/bin/build.json main succeeds and.next/routes-manifest.jsonhas no redirects, whilenext buildalone has the redirect.A sync function becomes async. The wrapper therefore can't claim the original's type. The
.tsand.mtsoverrides end inexport default config;, whose type isany, because the file is// @ts-nocheckandfahOptimizedConfigis untyped. A project file that imports the config and type-checks locally can fail under strict mode in the App Hosting build. Withany, most uses still compile; the build fails where TypeScript has to infer a type from the config, such as the parameter of a callback overredirects(). With anext.config.tstypedNextConfigand:tscpasses locally, anddist/bin/build.json main fails:This change makes the wrapper return 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.
typeof value?.then === 'function'is the same thenable testawaitapplies, so the wrapper treats exactly the same values as promises as Next.js'sawait configdoes.With the shape kept, the
.tsand.mtsoverrides end inexport default config as typeof originalConfig;, so a file importingnext.configsees the same type it sees without the adapter..mjskeepsexport default config;and.jskeepsmodule.exports = config;.// @ts-nocheckstays, 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:
.ts/.mtsconfig: typed as the original config instead ofany. 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
.tsand.mtsones 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 withimages.unoptimized: true. Against main's template, the promise and sync-function cases fail.Functional: all 11 scenarios pass.
Lint and environment:
npm run lint:quietand 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 threeno-unsafe-*warnings where it imports the generated file, like the file's existing dynamic import.Adapter builds: I ran
dist/bin/build.jsfrom main and from this branch on a minimal App Router app (Next 15.5.24):next.config.tstypedNextConfig, plus a project file importing itroutes-manifest.json,images.unoptimized: true, original config restored.mjsexportingPromise.resolve({ redirects })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.tscandnext buildpass.Not tested: an actual App Hosting rollout.