Unverified Commit d1d0258f authored by Shu Ding's avatar Shu Ding Committed by GitHub

fix: Impl `attachRequestMeta` in base server to handle meta differently in edge…

fix: Impl `attachRequestMeta` in base server to handle meta differently in edge and node servers (#38932)

This line was introduced in #38862 to the base server but should only be useful for the Node.js server:

```js
addRequestMeta(req, '__NEXT_CLONABLE_BODY', getClonableBody(req.body))
```

It also introduces polyfill of `stream` to the edge server.

Here we add an abstract method to handle it differently, where only `__NEXT_INIT_QUERY` is used.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 4d8d99e4
......@@ -16,7 +16,6 @@ import type { ParsedUrlQuery } from 'querystring'
import type { RenderOpts, RenderOptsPartial } from './render'
import type { ResponseCacheEntry, ResponseCacheValue } from './response-cache'
import type { UrlWithParsedQuery } from 'url'
import type { TLSSocket } from 'tls'
import {
CacheFs,
NormalizeError,
......@@ -27,7 +26,6 @@ import {
import type { PreviewData } from 'next/types'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { NodeNextRequest } from './base-http/node'
import type { PayloadOptions } from './send-payload'
import { join, resolve } from '../shared/lib/isomorphic/path'
......@@ -80,7 +78,6 @@ import { getHostname } from '../shared/lib/get-hostname'
import { parseUrl as parseUrlUtil } from '../shared/lib/router/utils/parse-url'
import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { getClonableBody } from './body-streams'
export type FindComponentsResult = {
components: LoadComponentsReturnType
......@@ -230,6 +227,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
protected abstract getRoutesManifest(): CustomRoutes
protected abstract getPrerenderManifest(): PrerenderManifest
protected abstract getServerComponentManifest(): any
protected abstract attachRequestMeta(
req: BaseNextRequest,
parsedUrl: NextUrlWithParsedQuery
): void
protected abstract sendRenderResult(
req: BaseNextRequest,
......@@ -419,22 +420,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
parsedUrl.query = parseQs(parsedUrl.query)
}
const protocol = (
(req as NodeNextRequest).originalRequest?.socket as TLSSocket
)?.encrypted
? 'https'
: 'http'
// When there are hostname and port we build an absolute URL
const initUrl =
this.hostname && this.port
? `${protocol}://${this.hostname}:${this.port}${req.url}`
: req.url
addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query })
addRequestMeta(req, '_protocol', protocol)
addRequestMeta(req, '__NEXT_CLONABLE_BODY', getClonableBody(req.body))
this.attachRequestMeta(req, parsedUrl)
const domainLocale = detectDomainLocale(
this.nextConfig.i18n?.domains,
......
import './node-polyfill-fetch'
import './node-polyfill-web-streams'
import type { TLSSocket } from 'tls'
import type { Route } from './router'
import {
CacheFs,
......@@ -80,7 +81,7 @@ import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
import ResponseCache from '../server/response-cache'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info'
import { bodyStreamToNodeStream } from './body-streams'
import { bodyStreamToNodeStream, getClonableBody } from './body-streams'
import { checkIsManualRevalidate } from './api-utils'
import { isDynamicRoute } from '../shared/lib/router/utils'
import { shouldUseReactRoot } from './utils'
......@@ -1511,6 +1512,28 @@ export default class NextNodeServer extends BaseServer {
return require(join(this.distDir, ROUTES_MANIFEST))
}
protected attachRequestMeta(
req: BaseNextRequest,
parsedUrl: NextUrlWithParsedQuery
) {
const protocol = (
(req as NodeNextRequest).originalRequest?.socket as TLSSocket
)?.encrypted
? 'https'
: 'http'
// When there are hostname and port we build an absolute URL
const initUrl =
this.hostname && this.port
? `${protocol}://${this.hostname}:${this.port}${req.url}`
: req.url
addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query })
addRequestMeta(req, '_protocol', protocol)
addRequestMeta(req, '__NEXT_CLONABLE_BODY', getClonableBody(req.body))
}
protected async runEdgeFunction(params: {
req: BaseNextRequest | NodeNextRequest
res: BaseNextResponse | NodeNextResponse
......
import type { WebNextRequest, WebNextResponse } from './base-http/web'
import type { RenderOpts } from './render'
import type RenderResult from './render-result'
import type { NextParsedUrlQuery } from './request-meta'
import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta'
import type { Params } from '../shared/lib/router/utils/route-matcher'
import type { PayloadOptions } from './send-payload'
import type { LoadComponentsReturnType } from './load-components'
......@@ -11,6 +11,7 @@ import BaseServer from './base-server'
import { renderToHTML } from './render'
import { byteLength } from './api-utils/web'
import { generateETag } from './lib/etag'
import { addRequestMeta } from './request-meta'
interface WebServerOptions extends Options {
webServerConfig: {
......@@ -103,6 +104,12 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
protected getFilesystemPaths() {
return new Set<string>()
}
protected attachRequestMeta(
req: WebNextRequest,
parsedUrl: NextUrlWithParsedQuery
) {
addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query })
}
protected getPrerenderManifest() {
return {
version: 3 as const,
......
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