8a1c9937b6
This makes sure to also check if a dynamic route matched after resolving a rewrite on the client to match behavior on the server. It also adds tests for this behavior to ensure it is working properly. Fixes: https://github.com/vercel/next.js/issues/16454
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { ParsedUrlQuery } from 'querystring'
|
|
import pathMatch from './path-match'
|
|
import prepareDestination from './prepare-destination'
|
|
import { Rewrite } from '../../../../lib/load-custom-routes'
|
|
|
|
const customRouteMatcher = pathMatch(true)
|
|
|
|
export default function resolveRewrites(
|
|
asPath: string,
|
|
pages: string[],
|
|
basePath: string,
|
|
rewrites: Rewrite[],
|
|
query: ParsedUrlQuery,
|
|
resolveHref: (path: string) => string
|
|
) {
|
|
if (!pages.includes(asPath)) {
|
|
for (const rewrite of rewrites) {
|
|
const matcher = customRouteMatcher(rewrite.source)
|
|
const params = matcher(asPath)
|
|
|
|
if (params) {
|
|
if (!rewrite.destination) {
|
|
// this is a proxied rewrite which isn't handled on the client
|
|
break
|
|
}
|
|
const destRes = prepareDestination(
|
|
rewrite.destination,
|
|
params,
|
|
query,
|
|
true,
|
|
rewrite.basePath === false ? '' : basePath
|
|
)
|
|
asPath = destRes.parsedDestination.pathname!
|
|
Object.assign(query, destRes.parsedDestination.query)
|
|
|
|
if (pages.includes(asPath)) {
|
|
// check if we now match a page as this means we are done
|
|
// resolving the rewrites
|
|
break
|
|
}
|
|
|
|
// check if we match a dynamic-route, if so we break the rewrites chain
|
|
const resolvedHref = resolveHref(asPath)
|
|
|
|
if (resolvedHref !== asPath && pages.includes(resolvedHref)) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return asPath
|
|
}
|