Skip to content

Commit

Permalink
Fix running server with Polyfilled fetch (#32368)
Browse files Browse the repository at this point in the history
**Note**: This PR is applying again changes landed #31935 that were reverted from an investigation.

This PR fixes #30398

By default Next will polyfill some fetch APIs (Request, Response, Header and fetch) only if fetch is not found in the global scope in certain entry points. If we have a custom server which is adding a global fetch (and only fetch) at the very top then the rest of APIs will not be polyfilled.

This PR adds a test on the custom server where we can add a custom polyfill for fetch with an env variable. This reproduces the issue since next-server.js will be required without having a polyfill for Response which makes it fail on requiring NextResponse. Then we remove the code that checks for subrequests to happen within the **sandbox** so that we don't need to polyfill `next-server` anymore.

The we also introduce an improvement on how we handle relative requests. Since #31858 introduced a `port` and `hostname` options for the server, we can always pass absolute URLs to the Middleware so we can always use the original `nextUrl` to pass it to fetch. This brings a lot of simplification for `NextURL` since we don't have to consider relative URLs no more.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
javivelasco authored Dec 13, 2021
1 parent c658fd3 commit 59f7676
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 165 deletions.
50 changes: 35 additions & 15 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 59f7676

Please sign in to comment.