Skip to content

Commit ecb72ee

Browse files
committed
Match subrequest handling for edge and node (#77474)
This aligns our subrequest handling between edge and node runtimes as we did not carry over this handling for node and as such we want to remove this from edge as well.
1 parent 25f810b commit ecb72ee

File tree

8 files changed

+0
-111
lines changed

8 files changed

+0
-111
lines changed

packages/next/src/server/lib/router-server.ts

-6
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,6 @@ export async function initialize(opts: {
166166
renderServer.instance =
167167
require('./render-server') as typeof import('./render-server')
168168

169-
const randomBytes = new Uint8Array(8)
170-
crypto.getRandomValues(randomBytes)
171-
const middlewareSubrequestId = Buffer.from(randomBytes).toString('hex')
172-
;(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')] =
173-
middlewareSubrequestId
174-
175169
const requestHandlerImpl: WorkerRequestHandler = async (req, res) => {
176170
// internal headers should not be honored by the request handler
177171
if (!process.env.NEXT_PRIVATE_TEST_HEADERS) {

packages/next/src/server/lib/server-ipc/utils.ts

-11
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,5 @@ export const filterInternalHeaders = (
5757
if (INTERNAL_HEADERS.includes(header)) {
5858
delete headers[header]
5959
}
60-
61-
// If this request didn't origin from this session we filter
62-
// out the "x-middleware-subrequest" header so we don't skip
63-
// middleware incorrectly
64-
if (
65-
header === 'x-middleware-subrequest' &&
66-
headers['x-middleware-subrequest-id'] !==
67-
(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')]
68-
) {
69-
delete headers['x-middleware-subrequest']
70-
}
7160
}
7261
}

packages/next/src/server/web/sandbox/context.ts

-21
Original file line numberDiff line numberDiff line change
@@ -362,27 +362,6 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
362362

363363
init.headers = new Headers(init.headers ?? {})
364364

365-
// Forward subrequest header from incoming request to outgoing request
366-
const store = requestStore.getStore()
367-
if (
368-
store?.headers.has('x-middleware-subrequest') &&
369-
!init.headers.has('x-middleware-subrequest')
370-
) {
371-
init.headers.set(
372-
'x-middleware-subrequest',
373-
store.headers.get('x-middleware-subrequest') ?? ''
374-
)
375-
}
376-
init.headers.set(
377-
'x-middleware-subrequest-id',
378-
(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')]
379-
)
380-
381-
const prevs =
382-
init.headers.get(`x-middleware-subrequest`)?.split(':') || []
383-
const value = prevs.concat(options.moduleName).join(':')
384-
init.headers.set('x-middleware-subrequest', value)
385-
386365
if (!init.headers.has('user-agent')) {
387366
init.headers.set(`user-agent`, `Next.js Middleware`)
388367
}

packages/next/src/server/web/sandbox/sandbox.ts

-19
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,6 @@ export async function getRuntimeContext(
9393

9494
export const run = withTaggedErrors(async function runWithTaggedErrors(params) {
9595
const runtime = await getRuntimeContext(params)
96-
const subreq = params.request.headers[`x-middleware-subrequest`]
97-
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
98-
99-
const MAX_RECURSION_DEPTH = 5
100-
const depth = subrequests.reduce(
101-
(acc, curr) => (curr === params.name ? acc + 1 : acc),
102-
0
103-
)
104-
105-
if (depth >= MAX_RECURSION_DEPTH) {
106-
return {
107-
waitUntil: Promise.resolve(),
108-
response: new runtime.context.Response(null, {
109-
headers: {
110-
'x-middleware-next': '1',
111-
},
112-
}),
113-
}
114-
}
11596

11697
const edgeFunction: (args: {
11798
request: RequestData

test/e2e/app-dir/app-routes-subrequests/app-routes-subrequests.test.ts

-20
This file was deleted.

test/e2e/app-dir/app-routes-subrequests/app/route.ts

-11
This file was deleted.

test/e2e/app-dir/app-routes-subrequests/next.config.js

-10
This file was deleted.

test/e2e/middleware-general/test/index.test.ts

-13
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,6 @@ describe('Middleware Runtime', () => {
144144
}
145145
}
146146

147-
it('should filter request header properly', async () => {
148-
const res = await next.fetch('/redirect-to-somewhere', {
149-
headers: {
150-
'x-middleware-subrequest':
151-
'middleware:middleware:middleware:middleware:middleware',
152-
},
153-
redirect: 'manual',
154-
})
155-
156-
expect(res.status).toBe(307)
157-
expect(res.headers.get('location')).toContain('/somewhere')
158-
})
159-
160147
it('should handle 404 on fallback: false route correctly', async () => {
161148
const res = await next.fetch('/ssg-fallback-false/first')
162149
expect(res.status).toBe(200)

0 commit comments

Comments
 (0)