Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Fix running server with Polyfilled fetch (#31935)" #32100

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 15 additions & 35 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url'
import isError from '../lib/is-error'
import { getMiddlewareInfo } from './require'
import { MIDDLEWARE_ROUTE } from '../lib/constants'
import { NextResponse } from './web/spec-extension/response'
import { run } from './web/sandbox'
import { addRequestMeta, getRequestMeta } from './request-meta'
import { toNodeHeaders } from './web/utils'
import { relativizeURL } from '../shared/lib/router/utils/relativize-url'

const getCustomRouteMatcher = pathMatch(true)

Expand Down Expand Up @@ -384,13 +384,7 @@ export default class Server {
parsedUrl.query = parseQs(parsedUrl.query)
}

// When there are hostname and port we build an absolute URL
const initUrl =
this.hostname && this.port
? `http://${this.hostname}:${this.port}${req.url}`
: req.url

addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
addRequestMeta(req, '__NEXT_INIT_URL', req.url)
addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query })

const url = parseNextUrl({
Expand Down Expand Up @@ -673,14 +667,6 @@ export default class Server {
}): Promise<FetchEventResult | null> {
this.middlewareBetaWarning()

// For middleware to "fetch" we must always provide an absolute URL
const url = getRequestMeta(params.request, '__NEXT_INIT_URL')!
if (!url.startsWith('http')) {
throw new Error(
'To use middleware you must provide a `hostname` and `port` to the Next.js Server'
)
}

const page: { name?: string; params?: { [key: string]: string } } = {}
if (await this.hasPage(params.parsedUrl.pathname)) {
page.name = params.parsedUrl.pathname
Expand All @@ -695,6 +681,8 @@ export default class Server {
}
}

const subreq = params.request.headers[`x-middleware-subrequest`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
const allHeaders = new Headers()
let result: FetchEventResult | null = null

Expand All @@ -714,6 +702,14 @@ export default class Server {
serverless: this._isLikeServerless,
})

if (subrequests.includes(middlewareInfo.name)) {
result = {
response: NextResponse.next(),
waitUntil: Promise.resolve(),
}
continue
}

result = await run({
name: middlewareInfo.name,
paths: middlewareInfo.paths,
Expand All @@ -725,7 +721,7 @@ export default class Server {
i18n: this.nextConfig.i18n,
trailingSlash: this.nextConfig.trailingSlash,
},
url: url,
url: getRequestMeta(params.request, '__NEXT_INIT_URL')!,
page: page,
},
useCache: !this.nextConfig.experimental.concurrentFeatures,
Expand Down Expand Up @@ -1185,13 +1181,9 @@ export default class Server {
type: 'route',
name: 'middleware catchall',
fn: async (req, res, _params, parsed) => {
if (!this.middleware?.length) {
return { finished: false }
}

const initUrl = getRequestMeta(req, '__NEXT_INIT_URL')!
const fullUrl = getRequestMeta(req, '__NEXT_INIT_URL')
const parsedUrl = parseNextUrl({
url: initUrl,
url: fullUrl,
headers: req.headers,
nextConfig: {
basePath: this.nextConfig.basePath,
Expand Down Expand Up @@ -1230,18 +1222,6 @@ export default class Server {
return { finished: true }
}

if (result.response.headers.has('x-middleware-rewrite')) {
const value = result.response.headers.get('x-middleware-rewrite')!
const rel = relativizeURL(value, initUrl)
result.response.headers.set('x-middleware-rewrite', rel)
}

if (result.response.headers.has('Location')) {
const value = result.response.headers.get('Location')!
const rel = relativizeURL(value, initUrl)
result.response.headers.set('Location', rel)
}

if (
!result.response.headers.has('x-middleware-rewrite') &&
!result.response.headers.has('x-middleware-next') &&
Expand Down
9 changes: 6 additions & 3 deletions packages/next/server/web/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { NextMiddleware, RequestData, FetchEventResult } from './types'
import type { RequestInit } from './spec-extension/request'
import { DeprecationError } from './error'
import { fromNodeHeaders } from './utils'
import { NextFetchEvent } from './spec-extension/fetch-event'
import { NextRequest } from './spec-extension/request'
import { NextRequest, RequestInit } from './spec-extension/request'
import { NextResponse } from './spec-extension/response'
import { waitUntilSymbol } from './spec-compliant/fetch-event'

Expand All @@ -12,9 +11,13 @@ export async function adapter(params: {
page: string
request: RequestData
}): Promise<FetchEventResult> {
const url = params.request.url.startsWith('/')
? `https://${params.request.headers.host}${params.request.url}`
: params.request.url

const request = new NextRequestHint({
page: params.page,
input: params.request.url,
input: url,
init: {
geo: params.request.geo,
headers: fromNodeHeaders(params.request.headers),
Expand Down
Loading