Skip to content

Commit

Permalink
refactor: remove isRSCRequestCheck (#65587)
Browse files Browse the repository at this point in the history
In preparation for the following request adapter work, this removes a
helper function for checking if a request is an RSC one, instead
preferring to use the metadata directly.

This helps readability because we know where it's coming from, and it
keeps the implementation simple.
  • Loading branch information
wyattjoh authored May 29, 2024
1 parent 4fbf3d3 commit b5ab9ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
21 changes: 8 additions & 13 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,7 @@ export default abstract class Server<
this.handleRequestImpl(req, res, parsedUrl).finally(() => {
if (!span) return

const isRSCRequest = isRSCRequestCheck(req) ?? false

const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false
span.setAttributes({
'http.status_code': res.statusCode,
'next.rsc': isRSCRequest,
Expand Down Expand Up @@ -940,6 +939,7 @@ export default abstract class Server<
)
}

// Update the `x-forwarded-*` headers.
const { originalRequest = null } = isNodeNextRequest(req) ? req : {}
const xForwardedProto = originalRequest?.headers['x-forwarded-proto']
const isHttps = xForwardedProto
Expand Down Expand Up @@ -1831,7 +1831,7 @@ export default abstract class Server<
resolvedPathname: string
): void {
const baseVaryHeader = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH_HEADER}`
const isRSCRequest = isRSCRequestCheck(req)
const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false

let addedNextUrlToVary = false

Expand Down Expand Up @@ -1955,9 +1955,11 @@ export default abstract class Server<
* prefetch request.
*/
const isPrefetchRSCRequest =
(req.headers[NEXT_ROUTER_PREFETCH_HEADER.toLowerCase()] === '1' ||
getRequestMeta(req, 'isPrefetchRSCRequest')) ??
false
getRequestMeta(req, 'isPrefetchRSCRequest') ?? false

// NOTE: Don't delete headers[RSC] yet, it still needs to be used in renderToHTML later

const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false

// when we are handling a middleware prefetch and it doesn't
// resolve to a static data route we bail early to avoid
Expand Down Expand Up @@ -2000,9 +2002,6 @@ export default abstract class Server<
)
}

// Don't delete headers[RSC] yet, it still needs to be used in renderToHTML later
const isRSCRequest = isRSCRequestCheck(req)

const { routeModule } = components

/**
Expand Down Expand Up @@ -3648,7 +3647,3 @@ export default abstract class Server<
return this.renderError(null, req, res, pathname!, query, setHeaders)
}
}

export function isRSCRequestCheck(req: BaseNextRequest): boolean {
return getRequestMeta(req, 'isRSCRequest') === true
}
7 changes: 5 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import type {
NextEnabledDirectories,
BaseRequestHandler,
} from './base-server'
import BaseServer, { NoFallbackError, isRSCRequestCheck } from './base-server'
import BaseServer, { NoFallbackError } from './base-server'
import { getMaybePagePath, getPagePath, requireFontManifest } from './require'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
Expand Down Expand Up @@ -1130,7 +1130,10 @@ export default class NextNodeServer extends BaseServer<

if (!routeMatch || isMiddlewareRequest) return

const isRSC = isRSCRequestCheck(normalizedReq)
// NOTE: this is only attached after handle has started, this runs
// after the response has been sent, so it should have it set.
const isRSC = getRequestMeta(normalizedReq, 'isRSCRequest')

const reqEnd = Date.now()
const fetchMetrics = normalizedReq.fetchMetrics || []
const reqDuration = reqEnd - reqStart
Expand Down

0 comments on commit b5ab9ab

Please sign in to comment.