Unverified Commit b4698a74 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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
parent 1c677a23
...@@ -195,6 +195,7 @@ export async function handler( ...@@ -195,6 +195,7 @@ export async function handler(
} }
const nodeNextReq = new NodeNextRequest(req) const nodeNextReq = new NodeNextRequest(req)
const nodeNextRes = new NodeNextResponse(res) const nodeNextRes = new NodeNextResponse(res)
const nextReq = NextRequestAdapter.fromNodeNextRequest( const nextReq = NextRequestAdapter.fromNodeNextRequest(
nodeNextReq, nodeNextReq,
signalFromNodeResponse(res) signalFromNodeResponse(res)
......
...@@ -228,6 +228,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -228,6 +228,7 @@ export class AppRouteRouteModule extends RouteModule<
// Automatically implement some methods if they aren't implemented by the // Automatically implement some methods if they aren't implemented by the
// userland module. // userland module.
this.methods = autoImplementMethods(userland) this.methods = autoImplementMethods(userland)
this.isAppRouter = true
// Get the non-static methods for this route. // Get the non-static methods for this route.
this.hasNonStaticMethods = hasNonStaticMethods(userland) this.hasNonStaticMethods = hasNonStaticMethods(userland)
......
...@@ -641,25 +641,34 @@ export abstract class RouteModule< ...@@ -641,25 +641,34 @@ export abstract class RouteModule<
} }
const routeParamKeys = new Set<string>() const routeParamKeys = new Set<string>()
const combinedParamKeys = [...routeParamKeys] const combinedParamKeys = []
for (const key of rewriteParamKeys) { // we don't include rewriteParamKeys in the combinedParamKeys
// We only want to filter rewrite param keys from the URL // for app router since the searchParams is populated from the
// if they are matches from the URL e.g. the key/value matches // URL so we don't want to strip the rewrite params from the URL
// before and after applying the rewrites /:path for /hello and // so that searchParams can include them
// { path: 'hello' } but not for { path: 'another' } and /hello if (!this.isAppRouter) {
// TODO: we should prefix rewrite param keys the same as we do for (const key of [
// for dynamic routes so we can identify them properly ...rewriteParamKeys,
const originalValue = Array.isArray(originalQuery[key]) ...Object.keys(serverUtils.defaultRouteMatches || {}),
? originalQuery[key].join('') ]) {
: originalQuery[key] // We only want to filter rewrite param keys from the URL
// if they are matches from the URL e.g. the key/value matches
const queryValue = Array.isArray(query[key]) // before and after applying the rewrites /:path for /hello and
? query[key].join('') // { path: 'hello' } but not for { path: 'another' } and /hello
: query[key] // TODO: we should prefix rewrite param keys the same as we do
// for dynamic routes so we can identify them properly
if (!(key in originalQuery) || originalValue === queryValue) { const originalValue = Array.isArray(originalQuery[key])
combinedParamKeys.push(key) ? originalQuery[key].join('')
: originalQuery[key]
const queryValue = Array.isArray(query[key])
? query[key].join('')
: query[key]
if (!(key in originalQuery) || originalValue === queryValue) {
combinedParamKeys.push(key)
}
} }
} }
......
...@@ -34,8 +34,7 @@ import { getSelectedParams } from '../client/components/router-reducer/compute-c ...@@ -34,8 +34,7 @@ import { getSelectedParams } from '../client/components/router-reducer/compute-c
function filterInternalQuery( function filterInternalQuery(
query: Record<string, undefined | string | string[]>, query: Record<string, undefined | string | string[]>,
paramKeys: string[], paramKeys: string[]
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
) { ) {
// this is used to pass query information in rewrites // this is used to pass query information in rewrites
// but should not be exposed in final query // but should not be exposed in final query
...@@ -52,8 +51,7 @@ function filterInternalQuery( ...@@ -52,8 +51,7 @@ function filterInternalQuery(
if ( if (
isNextQueryPrefix || isNextQueryPrefix ||
isNextInterceptionMarkerPrefix || isNextInterceptionMarkerPrefix ||
paramKeys.includes(key) || paramKeys.includes(key)
(defaultRouteRegex && Object.keys(defaultRouteRegex.groups).includes(key))
) { ) {
delete query[key] delete query[key]
} }
...@@ -62,8 +60,7 @@ function filterInternalQuery( ...@@ -62,8 +60,7 @@ function filterInternalQuery(
export function normalizeCdnUrl( export function normalizeCdnUrl(
req: BaseNextRequest | IncomingMessage, req: BaseNextRequest | IncomingMessage,
paramKeys: string[], paramKeys: string[]
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
) { ) {
// make sure to normalize req.url from CDNs to strip dynamic and rewrite // make sure to normalize req.url from CDNs to strip dynamic and rewrite
// params from the query which are added during routing // params from the query which are added during routing
...@@ -74,7 +71,7 @@ export function normalizeCdnUrl( ...@@ -74,7 +71,7 @@ export function normalizeCdnUrl(
return req.url return req.url
} }
delete (_parsedUrl as any).search delete (_parsedUrl as any).search
filterInternalQuery(_parsedUrl.query, paramKeys, defaultRouteRegex) filterInternalQuery(_parsedUrl.query, paramKeys)
req.url = formatUrl(_parsedUrl) req.url = formatUrl(_parsedUrl)
} }
...@@ -484,7 +481,7 @@ export function getServerUtils({ ...@@ -484,7 +481,7 @@ export function getServerUtils({
normalizeCdnUrl: ( normalizeCdnUrl: (
req: BaseNextRequest | IncomingMessage, req: BaseNextRequest | IncomingMessage,
paramKeys: string[] paramKeys: string[]
) => normalizeCdnUrl(req, paramKeys, defaultRouteRegex), ) => normalizeCdnUrl(req, paramKeys),
interpolateDynamicPath: ( interpolateDynamicPath: (
pathname: string, pathname: string,
...@@ -492,7 +489,7 @@ export function getServerUtils({ ...@@ -492,7 +489,7 @@ export function getServerUtils({
) => interpolateDynamicPath(pathname, params, defaultRouteRegex), ) => interpolateDynamicPath(pathname, params, defaultRouteRegex),
filterInternalQuery: (query: ParsedUrlQuery, paramKeys: string[]) => filterInternalQuery: (query: ParsedUrlQuery, paramKeys: string[]) =>
filterInternalQuery(query, paramKeys, defaultRouteRegex), filterInternalQuery(query, paramKeys),
} }
} }
......
...@@ -186,7 +186,7 @@ export default class NextWebServer extends BaseServer< ...@@ -186,7 +186,7 @@ export default class NextWebServer extends BaseServer<
normalizedParams, normalizedParams,
routeRegex routeRegex
) )
normalizeCdnUrl(req, Object.keys(routeRegex.routeKeys), routeRegex) normalizeCdnUrl(req, Object.keys(routeRegex.routeKeys))
} }
} }
......
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