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

Fix running server with Polyfilled fetch #32368

Merged
merged 7 commits into from Dec 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 35 additions & 15 deletions packages/next/server/base-server.ts
Expand Up @@ -91,10 +91,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 @@ -379,7 +379,13 @@ export default abstract class Server {
parsedUrl.query = parseQs(parsedUrl.query)
}

addRequestMeta(req, '__NEXT_INIT_URL', req.url)
// 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_QUERY', { ...parsedUrl.query })

const url = parseNextUrl({
Expand Down Expand Up @@ -673,6 +679,14 @@ export default abstract 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 @@ -687,8 +701,6 @@ export default abstract 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 @@ -708,14 +720,6 @@ export default abstract 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 @@ -727,7 +731,7 @@ export default abstract class Server {
i18n: this.nextConfig.i18n,
trailingSlash: this.nextConfig.trailingSlash,
},
url: getRequestMeta(params.request, '__NEXT_INIT_URL')!,
url: url,
page: page,
},
useCache: !this.nextConfig.experimental.concurrentFeatures,
Expand Down Expand Up @@ -1185,9 +1189,13 @@ export default abstract class Server {
type: 'route',
name: 'middleware catchall',
fn: async (req, res, _params, parsed) => {
const fullUrl = getRequestMeta(req, '__NEXT_INIT_URL')
if (!this.middleware?.length) {
return { finished: false }
}

const initUrl = getRequestMeta(req, '__NEXT_INIT_URL')!
const parsedUrl = parseNextUrl({
url: fullUrl,
url: initUrl,
headers: req.headers,
nextConfig: {
basePath: this.nextConfig.basePath,
Expand Down Expand Up @@ -1226,6 +1234,18 @@ export default abstract 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: 3 additions & 6 deletions packages/next/server/web/adapter.ts
@@ -1,8 +1,9 @@
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, RequestInit } from './spec-extension/request'
import { NextRequest } from './spec-extension/request'
import { NextResponse } from './spec-extension/response'
import { waitUntilSymbol } from './spec-compliant/fetch-event'

Expand All @@ -11,13 +12,9 @@ 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: url,
input: params.request.url,
init: {
geo: params.request.geo,
headers: fromNodeHeaders(params.request.headers),
Expand Down