Unverified Commit 1c2249d0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Match subrequest handling for edge and node (#77477)

Backports https://github.com/vercel/next.js/pull/77474 to v13
parent 422223e9
...@@ -134,16 +134,6 @@ export async function initialize(opts: { ...@@ -134,16 +134,6 @@ export async function initialize(opts: {
renderServer.instance = renderServer.instance =
require('./render-server') as typeof import('./render-server') require('./render-server') as typeof import('./render-server')
const randomBytes = new Uint8Array(8)
// handle node v16 not having global webcrypto
const crypto = require('crypto')
const webcrypto = crypto.webcrypto || crypto
webcrypto.getRandomValues(randomBytes)
const middlewareSubrequestId = Buffer.from(randomBytes).toString('hex')
;(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')] =
middlewareSubrequestId
const renderServerOpts: Parameters<RenderServer['initialize']>[0] = { const renderServerOpts: Parameters<RenderServer['initialize']>[0] = {
port: opts.port, port: opts.port,
dir: opts.dir, dir: opts.dir,
......
...@@ -55,16 +55,5 @@ export const filterInternalHeaders = ( ...@@ -55,16 +55,5 @@ export const filterInternalHeaders = (
if (INTERNAL_HEADERS.includes(header)) { if (INTERNAL_HEADERS.includes(header)) {
delete headers[header] delete headers[header]
} }
// If this request didn't origin from this session we filter
// out the "x-middleware-subrequest" header so we don't skip
// middleware incorrectly
if (
header === 'x-middleware-subrequest' &&
headers['x-middleware-subrequest-id'] !==
(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')]
) {
delete headers['x-middleware-subrequest']
}
} }
} }
...@@ -310,15 +310,6 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), ...@@ -310,15 +310,6 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
} }
init.headers = new Headers(init.headers ?? {}) init.headers = new Headers(init.headers ?? {})
const prevs =
init.headers.get(`x-middleware-subrequest`)?.split(':') || []
const value = prevs.concat(options.moduleName).join(':')
init.headers.set('x-middleware-subrequest', value)
init.headers.set(
'x-middleware-subrequest-id',
(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')]
)
if (!init.headers.has('user-agent')) { if (!init.headers.has('user-agent')) {
init.headers.set(`user-agent`, `Next.js Middleware`) init.headers.set(`user-agent`, `Next.js Middleware`)
......
...@@ -74,19 +74,6 @@ export async function getRuntimeContext(params: { ...@@ -74,19 +74,6 @@ export async function getRuntimeContext(params: {
export const run = withTaggedErrors(async function runWithTaggedErrors(params) { export const run = withTaggedErrors(async function runWithTaggedErrors(params) {
const runtime = await getRuntimeContext(params) const runtime = await getRuntimeContext(params)
const subreq = params.request.headers[`x-middleware-subrequest`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
if (subrequests.includes(params.name)) {
return {
waitUntil: Promise.resolve(),
response: new runtime.context.Response(null, {
headers: {
'x-middleware-next': '1',
},
}),
}
}
const edgeFunction: (args: { const edgeFunction: (args: {
request: RequestData request: RequestData
}) => Promise<FetchEventResult> = }) => Promise<FetchEventResult> =
......
...@@ -102,19 +102,6 @@ describe('Middleware Runtime', () => { ...@@ -102,19 +102,6 @@ describe('Middleware Runtime', () => {
} }
function runTests({ i18n }: { i18n?: boolean }) { function runTests({ i18n }: { i18n?: boolean }) {
it('should filter request header properly', async () => {
const res = await next.fetch('/redirect-to-somewhere', {
headers: {
'x-middleware-subrequest':
'middleware:middleware:middleware:middleware:middleware',
},
redirect: 'manual',
})
expect(res.status).toBe(307)
expect(res.headers.get('location')).toContain('/somewhere')
})
it('should work with notFound: true correctly', async () => { it('should work with notFound: true correctly', async () => {
const browser = await next.browser('/ssr-page') const browser = await next.browser('/ssr-page')
await browser.eval('window.beforeNav = 1') await browser.eval('window.beforeNav = 1')
......
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