Unverified Commit 70fb25d3 authored by Nathan Rajlich's avatar Nathan Rajlich Committed by GitHub

fix: resolve function configs in unstable_getResponseFromNextConfig (#92501)

## Summary

- `unstable_getResponseFromNextConfig` now resolves async function
configs before passing them to `loadCustomRoutes`
- Uses `normalizeConfig` — the same utility Next.js already uses at
build time to handle `(phase, ctx) => config` wrappers

## Problem

Plugins that wrap `next.config.mjs` in an async function (e.g.
`withSentry`, `withWorkflow`, `withMDX`) caused
`unstable_getResponseFromNextConfig` to silently return `null` for all
rewrites, redirects, and headers. The function config was passed
directly to `loadCustomRoutes`, which accessed `.rewrites`/`.redirects`
on a function object.

## Fix

Call `normalizeConfig('phase-production-build', nextConfig)` before
`loadCustomRoutes`. This is a 4-line change that makes the testing
utility consistent with how Next.js resolves configs at build time.

## Test

Added a test that passes an async function config with rewrites and
redirects, verifying both are matched correctly.

Fixes #92500

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 46e2114e
......@@ -248,6 +248,45 @@ describe('config-testing-utils', () => {
})
})
describe('function config', () => {
it('resolves async function configs before matching routes', async () => {
// Plugins like withSentry, withWorkflow, etc. wrap the config in an
// async (phase, ctx) => config function. This should be resolved
// transparently.
const fnConfig = async (
_phase: string,
_ctx: { defaultConfig: object }
) => ({
async rewrites() {
return [{ source: '/old', destination: '/new' }]
},
async redirects() {
return [
{ source: '/legacy', destination: '/modern', permanent: true },
]
},
})
const rewriteResponse = await unstable_getResponseFromNextConfig({
url: 'https://example.com/old',
nextConfig: fnConfig,
})
expect(isRewrite(rewriteResponse)).toEqual(true)
expect(getRewrittenUrl(rewriteResponse)).toEqual(
'https://example.com/new'
)
const redirectResponse = await unstable_getResponseFromNextConfig({
url: 'https://example.com/legacy',
nextConfig: fnConfig,
})
expect(redirectResponse.status).toEqual(308)
expect(redirectResponse.headers.get('location')).toEqual(
'https://example.com/modern'
)
})
})
it('basePath', async () => {
const response = await unstable_getResponseFromNextConfig({
url: 'https://nextjs.org/test',
......
......@@ -5,9 +5,10 @@ import {
matchHas,
prepareDestination,
} from '../../../shared/lib/router/utils/prepare-destination'
import { PHASE_PRODUCTION_BUILD } from '../../../shared/lib/constants'
import { buildCustomRoute } from '../../../lib/build-custom-route'
import loadCustomRoutes from '../../../lib/load-custom-routes'
import type { NextConfig } from '../../../server/config-shared'
import { normalizeConfig, type NextConfig } from '../../../server/config-shared'
import { NextResponse } from '../../../server/web/exports'
import { getRedirectStatus } from '../../../lib/redirect-status'
import type {
......@@ -81,13 +82,19 @@ export async function unstable_getResponseFromNextConfig({
cookies = {},
}: {
url: string
nextConfig: NextConfig
nextConfig:
| NextConfig
| ((...args: any[]) => NextConfig | Promise<NextConfig>)
headers?: IncomingHttpHeaders
cookies?: Record<string, string>
}): Promise<NextResponse> {
const parsedUrl = parse(url, true)
const request = constructRequest({ url, headers, cookies })
const routes = await loadCustomRoutes(nextConfig)
const resolvedConfig = await normalizeConfig(
PHASE_PRODUCTION_BUILD,
nextConfig
)
const routes = await loadCustomRoutes(resolvedConfig)
const headerRoutes = routes.headers.map((route) =>
buildCustomRoute('header', route)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment