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