Skip to content

Commit b4698a7

Browse files
authored
Update matching query and route param handling (#81209)
Validated this is un-necessary and handling an edge case we don't need to be worried about and it's better to leave the query param for this case instead Deploy tests checked https://github.com/vercel/vercel/actions/runs/16058085256/job/45317414336?pr=13522 and https://github.com/vercel/next.js/actions/runs/16059083218/job/45320691422
1 parent 1c677a2 commit b4698a7

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

packages/next/src/build/templates/app-route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export async function handler(
195195
}
196196
const nodeNextReq = new NodeNextRequest(req)
197197
const nodeNextRes = new NodeNextResponse(res)
198+
198199
const nextReq = NextRequestAdapter.fromNodeNextRequest(
199200
nodeNextReq,
200201
signalFromNodeResponse(res)

packages/next/src/server/route-modules/app-route/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export class AppRouteRouteModule extends RouteModule<
228228
// Automatically implement some methods if they aren't implemented by the
229229
// userland module.
230230
this.methods = autoImplementMethods(userland)
231+
this.isAppRouter = true
231232

232233
// Get the non-static methods for this route.
233234
this.hasNonStaticMethods = hasNonStaticMethods(userland)

packages/next/src/server/route-modules/route-module.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -641,25 +641,34 @@ export abstract class RouteModule<
641641
}
642642

643643
const routeParamKeys = new Set<string>()
644-
const combinedParamKeys = [...routeParamKeys]
645-
646-
for (const key of rewriteParamKeys) {
647-
// We only want to filter rewrite param keys from the URL
648-
// if they are matches from the URL e.g. the key/value matches
649-
// before and after applying the rewrites /:path for /hello and
650-
// { path: 'hello' } but not for { path: 'another' } and /hello
651-
// TODO: we should prefix rewrite param keys the same as we do
652-
// for dynamic routes so we can identify them properly
653-
const originalValue = Array.isArray(originalQuery[key])
654-
? originalQuery[key].join('')
655-
: originalQuery[key]
656-
657-
const queryValue = Array.isArray(query[key])
658-
? query[key].join('')
659-
: query[key]
660-
661-
if (!(key in originalQuery) || originalValue === queryValue) {
662-
combinedParamKeys.push(key)
644+
const combinedParamKeys = []
645+
646+
// we don't include rewriteParamKeys in the combinedParamKeys
647+
// for app router since the searchParams is populated from the
648+
// URL so we don't want to strip the rewrite params from the URL
649+
// so that searchParams can include them
650+
if (!this.isAppRouter) {
651+
for (const key of [
652+
...rewriteParamKeys,
653+
...Object.keys(serverUtils.defaultRouteMatches || {}),
654+
]) {
655+
// We only want to filter rewrite param keys from the URL
656+
// if they are matches from the URL e.g. the key/value matches
657+
// before and after applying the rewrites /:path for /hello and
658+
// { path: 'hello' } but not for { path: 'another' } and /hello
659+
// TODO: we should prefix rewrite param keys the same as we do
660+
// for dynamic routes so we can identify them properly
661+
const originalValue = Array.isArray(originalQuery[key])
662+
? originalQuery[key].join('')
663+
: originalQuery[key]
664+
665+
const queryValue = Array.isArray(query[key])
666+
? query[key].join('')
667+
: query[key]
668+
669+
if (!(key in originalQuery) || originalValue === queryValue) {
670+
combinedParamKeys.push(key)
671+
}
663672
}
664673
}
665674

packages/next/src/server/server-utils.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ import { getSelectedParams } from '../client/components/router-reducer/compute-c
3434

3535
function filterInternalQuery(
3636
query: Record<string, undefined | string | string[]>,
37-
paramKeys: string[],
38-
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
37+
paramKeys: string[]
3938
) {
4039
// this is used to pass query information in rewrites
4140
// but should not be exposed in final query
@@ -52,8 +51,7 @@ function filterInternalQuery(
5251
if (
5352
isNextQueryPrefix ||
5453
isNextInterceptionMarkerPrefix ||
55-
paramKeys.includes(key) ||
56-
(defaultRouteRegex && Object.keys(defaultRouteRegex.groups).includes(key))
54+
paramKeys.includes(key)
5755
) {
5856
delete query[key]
5957
}
@@ -62,8 +60,7 @@ function filterInternalQuery(
6260

6361
export function normalizeCdnUrl(
6462
req: BaseNextRequest | IncomingMessage,
65-
paramKeys: string[],
66-
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
63+
paramKeys: string[]
6764
) {
6865
// make sure to normalize req.url from CDNs to strip dynamic and rewrite
6966
// params from the query which are added during routing
@@ -74,7 +71,7 @@ export function normalizeCdnUrl(
7471
return req.url
7572
}
7673
delete (_parsedUrl as any).search
77-
filterInternalQuery(_parsedUrl.query, paramKeys, defaultRouteRegex)
74+
filterInternalQuery(_parsedUrl.query, paramKeys)
7875

7976
req.url = formatUrl(_parsedUrl)
8077
}
@@ -484,15 +481,15 @@ export function getServerUtils({
484481
normalizeCdnUrl: (
485482
req: BaseNextRequest | IncomingMessage,
486483
paramKeys: string[]
487-
) => normalizeCdnUrl(req, paramKeys, defaultRouteRegex),
484+
) => normalizeCdnUrl(req, paramKeys),
488485

489486
interpolateDynamicPath: (
490487
pathname: string,
491488
params: Record<string, undefined | string | string[]>
492489
) => interpolateDynamicPath(pathname, params, defaultRouteRegex),
493490

494491
filterInternalQuery: (query: ParsedUrlQuery, paramKeys: string[]) =>
495-
filterInternalQuery(query, paramKeys, defaultRouteRegex),
492+
filterInternalQuery(query, paramKeys),
496493
}
497494
}
498495

packages/next/src/server/web-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export default class NextWebServer extends BaseServer<
186186
normalizedParams,
187187
routeRegex
188188
)
189-
normalizeCdnUrl(req, Object.keys(routeRegex.routeKeys), routeRegex)
189+
normalizeCdnUrl(req, Object.keys(routeRegex.routeKeys))
190190
}
191191
}
192192

0 commit comments

Comments
 (0)