Unverified Commit 45492eac authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Fix originalRequest not available for edge requests (#46741)

For requests made via the edge runtime, they will not contain the
`originalRequest` object used by the request storage to enable
headers/cookie access. This uses the request object passed when in the
edge runtime.
parent dda9be09
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
} from '../app-render' } from '../app-render'
import { AsyncStorageWrapper } from './async-storage-wrapper' import { AsyncStorageWrapper } from './async-storage-wrapper'
import type { tryGetPreviewData } from '../api-utils/node' import type { tryGetPreviewData } from '../api-utils/node'
import type { BaseNextRequest, BaseNextResponse } from '../base-http'
function headersWithoutFlight(headers: IncomingHttpHeaders) { function headersWithoutFlight(headers: IncomingHttpHeaders) {
const newHeaders = { ...headers } const newHeaders = { ...headers }
...@@ -20,8 +21,8 @@ function headersWithoutFlight(headers: IncomingHttpHeaders) { ...@@ -20,8 +21,8 @@ function headersWithoutFlight(headers: IncomingHttpHeaders) {
} }
export type RequestContext = { export type RequestContext = {
req: IncomingMessage req: IncomingMessage | BaseNextRequest
res: ServerResponse res: ServerResponse | BaseNextResponse
renderOpts?: RenderOpts renderOpts?: RenderOpts
} }
......
...@@ -433,14 +433,19 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> { ...@@ -433,14 +433,19 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> {
// Get the handler function for the given method. // Get the handler function for the given method.
const handle = this.resolve(req.method, module) const handle = this.resolve(req.method, module)
const requestContext: RequestContext =
process.env.NEXT_RUNTIME === 'edge'
? { req, res }
: {
req: (req as NodeNextRequest).originalRequest,
res: (res as NodeNextResponse).originalResponse,
}
// Run the handler with the request AsyncLocalStorage to inject the helper // Run the handler with the request AsyncLocalStorage to inject the helper
// support. // support.
const response = await this.requestAsyncLocalStorageWrapper.wrap( const response = await this.requestAsyncLocalStorageWrapper.wrap(
requestAsyncStorage, requestAsyncStorage,
{ requestContext,
req: (req as NodeNextRequest).originalRequest,
res: (res as NodeNextResponse).originalResponse,
},
() => () =>
this.staticAsyncLocalStorageWrapper.wrap( this.staticAsyncLocalStorageWrapper.wrap(
staticGenerationAsyncStorage, staticGenerationAsyncStorage,
......
...@@ -405,6 +405,16 @@ createNextDescribe( ...@@ -405,6 +405,16 @@ createNextDescribe(
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
expect(await res.text()).toContain('hello, world') expect(await res.text()).toContain('hello, world')
}) })
it('returns a response when headers are accessed', async () => {
const meta = { ping: 'pong' }
const res = await next.fetch('/edge/headers', {
headers: withRequestMeta(meta),
})
expect(res.status).toEqual(200)
expect(await res.json()).toEqual(meta)
})
}) })
describe('dynamic = "force-static"', () => { describe('dynamic = "force-static"', () => {
......
import { headers } from 'next/headers'
import { NextResponse } from 'next/server'
import { getRequestMeta } from '../../../helpers'
export const runtime = 'experimental-edge'
export function GET() {
const meta = getRequestMeta(headers())
return NextResponse.json(meta)
}
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