Unverified Commit 1858fa96 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix page url for edge routes in app dir (#40361)

For edge routes in app dir, we passed the page name as url so that the
url will be incorrect:

* a non-dynamic route `/dashboard` will become `/dashboard/page`
* a dynamic route `/dynamic/[id]` with url `/dyanmic/123` will still hit
`/dynamic/page/[id]/page`

This PR uses normalized `params.page` with interoplated as correct
pathname for the page.
parent a234abfa
......@@ -763,7 +763,6 @@ export default class NextNodeServer extends BaseServer {
params,
page,
appPaths: null,
isAppPath: false,
})
if (handledAsEdgeFunction) {
......@@ -913,7 +912,6 @@ export default class NextNodeServer extends BaseServer {
params: ctx.renderOpts.params,
page,
appPaths,
isAppPath,
})
return null
}
......@@ -2031,12 +2029,12 @@ export default class NextNodeServer extends BaseServer {
params: Params | undefined
page: string
appPaths: string[] | null
isAppPath: boolean
onWarning?: (warning: Error) => void
}): Promise<FetchEventResult | null> {
let middlewareInfo: ReturnType<typeof this.getEdgeFunctionInfo> | undefined
const page = params.page
const { query, page } = params
await this.ensureEdgeFunction({ page, appPaths: params.appPaths })
middlewareInfo = this.getEdgeFunctionInfo({
page,
......@@ -2048,21 +2046,20 @@ export default class NextNodeServer extends BaseServer {
}
// For middleware to "fetch" we must always provide an absolute URL
const isDataReq = !!params.query.__nextDataReq
const query = urlQueryToSearchParams(params.query).toString()
const locale = params.query.__nextLocale
// Use original pathname (without `/page`) instead of appPath for url
let normalizedPathname = params.page
const locale = query.__nextLocale
const isDataReq = !!query.__nextDataReq
const queryString = urlQueryToSearchParams(query).toString()
if (isDataReq) {
params.req.headers['x-nextjs-data'] = '1'
}
let normalizedPathname = normalizeAppPath(page)
if (isDynamicRoute(normalizedPathname)) {
const routeRegex = getNamedRouteRegex(params.page)
const routeRegex = getNamedRouteRegex(normalizedPathname)
normalizedPathname = interpolateDynamicPath(
params.page,
Object.assign({}, params.params, params.query),
normalizedPathname,
Object.assign({}, params.params, query),
routeRegex
)
}
......@@ -2070,7 +2067,7 @@ export default class NextNodeServer extends BaseServer {
const url = `${getRequestMeta(params.req, '_protocol')}://${
this.hostname
}:${this.port}${locale ? `/${locale}` : ''}${normalizedPathname}${
query ? `?${query}` : ''
queryString ? `?${queryString}` : ''
}`
if (!url.startsWith('http')) {
......
......@@ -340,6 +340,20 @@ describe('app dir - react server components', () => {
expect(head).toMatch(/{color:(\s*)blue;?}/)
})
it('should stick to the url without trailing /page suffix', async () => {
const browser = await webdriver(next.url, '/edge/dynamic')
const indexUrl = await browser.url()
await browser.loadPage(`${next.url}/edge/dynamic/123`, {
disableCache: false,
beforePageLoad: null,
})
const dynamicRouteUrl = await browser.url()
expect(indexUrl).toBe(`${next.url}/edge/dynamic`)
expect(dynamicRouteUrl).toBe(`${next.url}/edge/dynamic/123`)
})
it('should support streaming for flight response', async () => {
await fetchViaHTTP(next.url, '/?__flight__=1').then(async (response) => {
const result = await resolveStreamResponse(response)
......
export default function page() {
return 'dynamic route [id] page'
}
export const config = {
runtime: 'experimental-edge',
}
export default function page() {
return 'dynamic route index page'
}
export const config = {
runtime: 'experimental-edge',
}
......@@ -7,4 +7,14 @@ module.exports = {
appDir: true,
serverComponents: true,
},
rewrites: async () => {
return {
afterFiles: [
{
source: '/rewritten-to-edge-dynamic',
destination: '/edge/dynamic',
},
],
}
},
}
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