Commit 63c79857 authored by JJ Kasper's avatar JJ Kasper

Match subrequest handling for edge and node

parent fedcd25f
......@@ -27,7 +27,6 @@ import type { NextConfig } from './config-shared'
import type { DynamicRoutes, PageChecker } from './router'
import fs from 'fs'
import crypto from 'crypto'
import { join, relative, resolve, sep } from 'path'
import { IncomingMessage, ServerResponse } from 'http'
import { addRequestMeta, getRequestMeta } from './request-meta'
......@@ -248,11 +247,6 @@ export default class NextNodeServer extends BaseServer {
)
}
const randomBytes = crypto.randomBytes(8)
const middlewareSubrequestId = randomBytes.toString('hex')
;(global as any)[Symbol.for('@next/middleware-subrequest-id')] =
middlewareSubrequestId
if (!options.dev) {
// pre-warm _document and _app as these will be
// needed for most requests
......
......@@ -218,15 +218,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',
(global as any)[Symbol.for('@next/middleware-subrequest-id')]
)
if (!init.headers.has('user-agent')) {
init.headers.set(`user-agent`, `Next.js Middleware`)
......
......@@ -70,24 +70,6 @@ export const getRuntimeContext = async (params: {
export const run = withTaggedErrors(async (params) => {
const runtime = await getRuntimeContext(params)
const subreq = params.request.headers[`x-middleware-subrequest`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
if (
// require the subrequest-id be valid to skip
params.request.headers['x-middleware-subrequest-id'] ===
(global as any)[Symbol.for('@next/middleware-subrequest-id')] &&
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> =
......
......@@ -71,24 +71,6 @@ describe('Middleware Runtime', () => {
}
function runTests({ i18n }: { i18n?: boolean }) {
it('should filter request header properly', async () => {
const res = await fetchViaHTTP(
next.url,
'/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')
})
if ((global as any).isNextDev) {
it('refreshes the page when middleware changes ', async () => {
const browser = await webdriver(next.url, `/about`)
......
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