diff --git a/packages/next/server/base-server.ts b/packages/next/server/base-server.ts index 4d243a182ee1f..b2683472fbb18 100644 --- a/packages/next/server/base-server.ts +++ b/packages/next/server/base-server.ts @@ -510,7 +510,9 @@ export default abstract class Server { } if (params) { - params = utils.normalizeDynamicRouteParams(params).params + if (!paramsResult.hasValidParams) { + params = utils.normalizeDynamicRouteParams(params).params + } matchedPathname = utils.interpolateDynamicPath( matchedPathname, diff --git a/test/production/required-server-files.test.ts b/test/production/required-server-files.test.ts index d3a1def514294..25779a1b79f12 100644 --- a/test/production/required-server-files.test.ts +++ b/test/production/required-server-files.test.ts @@ -671,6 +671,23 @@ describe('should set-up next', () => { expect(json.url).toBe('/api/optional?another=value') }) + it('should normalize index optional values correctly for API page', async () => { + const res = await fetchViaHTTP( + appPort, + '/api/optional/index', + { rest: 'index', another: 'value' }, + { + headers: { + 'x-matched-path': '/api/optional/[[...rest]]', + }, + } + ) + + const json = await res.json() + expect(json.query).toEqual({ another: 'value', rest: ['index'] }) + expect(json.url).toBe('/api/optional/index?another=value') + }) + it('should match the index page correctly', async () => { const res = await fetchViaHTTP(appPort, '/', undefined, { headers: {