Skip to content

Commit

Permalink
fix: Impl attachRequestMeta in base server to handle meta different…
Browse files Browse the repository at this point in the history
…ly 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)
  • Loading branch information
shuding committed Jul 22, 2022
1 parent 4d8d99e commit d1d0258
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
24 changes: 5 additions & 19 deletions packages/next/server/base-server.ts
Expand Up @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 24 additions & 1 deletion packages/next/server/next-server.ts
@@ -1,6 +1,7 @@
import './node-polyfill-fetch'
import './node-polyfill-web-streams'

import type { TLSSocket } from 'tls'
import type { Route } from './router'
import {
CacheFs,
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion packages/next/server/web-server.ts
@@ -1,7 +1,7 @@
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'
Expand All @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d1d0258

Please sign in to comment.