Unverified Commit 668466bc authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix rewrite/dynamic interpolation E2E cases (#37669)

parent 3b9f180d
...@@ -340,7 +340,10 @@ export function getUtils({ ...@@ -340,7 +340,10 @@ export function getUtils({
} }
} }
function normalizeDynamicRouteParams(params: ParsedUrlQuery) { function normalizeDynamicRouteParams(
params: ParsedUrlQuery,
ignoreOptional?: boolean
) {
let hasValidParams = true let hasValidParams = true
if (!defaultRouteRegex) return { params, hasValidParams: false } if (!defaultRouteRegex) return { params, hasValidParams: false }
...@@ -361,7 +364,10 @@ export function getUtils({ ...@@ -361,7 +364,10 @@ export function getUtils({
}) })
: value?.includes(defaultValue as string) : value?.includes(defaultValue as string)
if (isDefaultValue || (typeof value === 'undefined' && !isOptional)) { if (
isDefaultValue ||
(typeof value === 'undefined' && !(isOptional && ignoreOptional))
) {
hasValidParams = false hasValidParams = false
} }
......
...@@ -517,27 +517,20 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -517,27 +517,20 @@ export default abstract class Server<ServerOptions extends Options = Options> {
parsedUrl.query parsedUrl.query
) )
// for prerendered ISR paths we attempt parsing the route
// params from the URL directly as route-matches may not
// contain the correct values due to the filesystem path
// matching before the dynamic route has been matched
if ( if (
!isDynamicRoute(normalizedUrlPath) || !paramsResult.hasValidParams &&
!isDynamicRoute(matchedPath) pageIsDynamic &&
!isDynamicRoute(normalizedUrlPath)
) { ) {
// we favor matching against req.url although if there's a let matcherParams = utils.dynamicRouteMatcher?.(normalizedUrlPath)
// rewrite and it's SSR we use the x-matched-path instead
let matcherRes = utils.dynamicRouteMatcher?.(normalizedUrlPath)
if (!matcherRes) { if (matcherParams) {
matcherRes = utils.dynamicRouteMatcher?.(matchedPath) Object.assign(paramsResult.params, matcherParams)
} paramsResult.hasValidParams = true
const parsedResult = utils.normalizeDynamicRouteParams(
matcherRes || {}
)
if (parsedResult.hasValidParams) {
if (paramsResult.hasValidParams) {
Object.assign(paramsResult.params, parsedResult.params)
} else {
paramsResult = parsedResult
}
} }
} }
...@@ -547,9 +540,8 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -547,9 +540,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if ( if (
req.headers['x-now-route-matches'] && req.headers['x-now-route-matches'] &&
(!paramsResult.hasValidParams || isDynamicRoute(matchedPath) &&
(isDynamicRoute(matchedPath) && !paramsResult.hasValidParams
isDynamicRoute(normalizedUrlPath)))
) { ) {
const opts: Record<string, string> = {} const opts: Record<string, string> = {}
const routeParams = utils.getParamsFromRouteMatches( const routeParams = utils.getParamsFromRouteMatches(
...@@ -561,17 +553,29 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -561,17 +553,29 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (opts.locale) { if (opts.locale) {
parsedUrl.query.__nextLocale = opts.locale parsedUrl.query.__nextLocale = opts.locale
} }
paramsResult = utils.normalizeDynamicRouteParams(routeParams) paramsResult = utils.normalizeDynamicRouteParams(
routeParams,
true
)
if (paramsResult.hasValidParams) { if (paramsResult.hasValidParams) {
params = paramsResult.params params = paramsResult.params
} }
} }
// handle the actual dynamic route name being requested
if (
pageIsDynamic &&
utils.defaultRouteMatches &&
normalizedUrlPath === srcPathname &&
!paramsResult.hasValidParams &&
!utils.normalizeDynamicRouteParams({ ...params }, true)
.hasValidParams
) {
params = utils.defaultRouteMatches
}
if (params) { if (params) {
if (!paramsResult.hasValidParams) {
params = utils.normalizeDynamicRouteParams(params).params
}
matchedPath = utils.interpolateDynamicPath(srcPathname, params) matchedPath = utils.interpolateDynamicPath(srcPathname, params)
req.url = utils.interpolateDynamicPath(req.url!, params) req.url = utils.interpolateDynamicPath(req.url!, params)
} }
......
...@@ -1029,6 +1029,23 @@ describe('should set-up next', () => { ...@@ -1029,6 +1029,23 @@ describe('should set-up next', () => {
const html = await res.text() const html = await res.text()
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#slug-page').text()).toBe('[slug] page') expect($('#slug-page').text()).toBe('[slug] page')
expect(JSON.parse($('#router').text()).query).toEqual({
slug: 'slug-1',
})
const res2 = await fetchViaHTTP(appPort, '/[slug]', undefined, {
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#slug-page').text()).toBe('[slug] page')
expect(JSON.parse($2('#router').text()).query).toEqual({
slug: '[slug]',
})
}) })
it('should have correct asPath on dynamic SSG page correctly', async () => { it('should have correct asPath on dynamic SSG page correctly', async () => {
......
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