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: Impl attachRequestMeta in base server to handle meta differently in edge and node servers #38932

Merged
merged 2 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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