Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix page url for edge routes in app dir #40361

Merged
merged 6 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 9 additions & 12 deletions packages/next/server/next-server.ts
Expand Up @@ -763,7 +763,6 @@ export default class NextNodeServer extends BaseServer {
params,
page,
appPaths: null,
isAppPath: false,
})

if (handledAsEdgeFunction) {
Expand Down Expand Up @@ -913,7 +912,6 @@ export default class NextNodeServer extends BaseServer {
params: ctx.renderOpts.params,
page,
appPaths,
isAppPath,
})
return null
}
Expand Down Expand Up @@ -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, req } = params
const { pathname } = parseUrl(req.url)
await this.ensureEdgeFunction({ page, appPaths: params.appPaths })
middlewareInfo = this.getEdgeFunctionInfo({
page,
Expand All @@ -2048,29 +2046,28 @@ 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 = params.page
if (isDynamicRoute(normalizedPathname)) {
const routeRegex = getNamedRouteRegex(params.page)
normalizedPathname = interpolateDynamicPath(
params.page,
Object.assign({}, params.params, params.query),
Object.assign({}, params.params, query),
routeRegex
)
}

const url = `${getRequestMeta(params.req, '_protocol')}://${
this.hostname
}:${this.port}${locale ? `/${locale}` : ''}${normalizedPathname}${
query ? `?${query}` : ''
}:${this.port}${locale ? `/${locale}` : ''}${pathname}${
huozhi marked this conversation as resolved.
Show resolved Hide resolved
queryString ? `?${queryString}` : ''
}`

if (!url.startsWith('http')) {
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/app-dir/rsc-basic.test.ts
Expand Up @@ -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)
Expand Down
@@ -0,0 +1,7 @@
export default function page() {
return 'dynamic route [id] page'
}

export const runtime = {
runtime: 'experimental-edge',
}
7 changes: 7 additions & 0 deletions test/e2e/app-dir/rsc-basic/app/edge/dynamic/page.server.js
@@ -0,0 +1,7 @@
export default function page() {
return 'dynamic route index page'
}

export const runtime = {
runtime: 'experimental-edge',
}