Repository: vercel/next.js
Pull request: Match subrequest handling for edge and node

Backports https://github.com/vercel/next.js/pull/77474 to v13

Changed files:
filename: packages/next/src/server/lib/router-server.ts
patch: @@ -134,16 +134,6 @@ export async function initialize(opts: {
   renderServer.instance =
     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] = {
     port: opts.port,
     dir: opts.dir,

filename: packages/next/src/server/lib/server-ipc/utils.ts
patch: @@ -55,16 +55,5 @@ export const filterInternalHeaders = (
     if (INTERNAL_HEADERS.includes(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']
-    }
   }
 }

filename: packages/next/src/server/web/sandbox/context.ts
patch: @@ -310,15 +310,6 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
         }
 
         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')) {
           init.headers.set(`user-agent`, `Next.js Middleware`)

filename: packages/next/src/server/web/sandbox/sandbox.ts
patch: @@ -74,19 +74,6 @@ export async function getRuntimeContext(params: {
 
 export const run = withTaggedErrors(async function runWithTaggedErrors(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: {
     request: RequestData
   }) => Promise<FetchEventResult> =

filename: test/e2e/middleware-general/test/index.test.ts
patch: @@ -102,19 +102,6 @@ describe('Middleware Runtime', () => {
   }
 
   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 () => {
       const browser = await next.browser('/ssr-page')
       await browser.eval('window.beforeNav = 1')