Unverified Commit ff7d0398 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Perf: Use headers object directly in checkIsOnDemandRevalidate (#91381)

## What?

Converting from `req.headers` to `Headers` is expensive. Here it's only
used to check/read headers only, so directly using the object when it's
available is better.
parent 43dc6643
import type { IncomingMessage } from 'http' import type { IncomingHttpHeaders, IncomingMessage } from 'http'
import type { BaseNextRequest } from '../base-http' import type { BaseNextRequest } from '../base-http'
import type { CookieSerializeOptions } from 'next/dist/compiled/cookie' import type { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import type { NextApiResponse } from '../../shared/lib/utils' import type { NextApiResponse } from '../../shared/lib/utils'
...@@ -83,12 +83,26 @@ export function checkIsOnDemandRevalidate( ...@@ -83,12 +83,26 @@ export function checkIsOnDemandRevalidate(
isOnDemandRevalidate: boolean isOnDemandRevalidate: boolean
revalidateOnlyGenerated: boolean revalidateOnlyGenerated: boolean
} { } {
const headers = HeadersAdapter.from(req.headers) // Headers is a plain object for Node.js, Headers object in Edge runtime
if (typeof req.headers.get === 'function') {
const headers = HeadersAdapter.from(req.headers)
const previewModeId = headers.get(PRERENDER_REVALIDATE_HEADER) const previewModeId = headers.get(PRERENDER_REVALIDATE_HEADER)
const isOnDemandRevalidate = previewModeId === previewProps.previewModeId
const revalidateOnlyGenerated = headers.has(
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER
)
return { isOnDemandRevalidate, revalidateOnlyGenerated }
}
const headers = req.headers as IncomingHttpHeaders
const previewModeId = headers[PRERENDER_REVALIDATE_HEADER]
const isOnDemandRevalidate = previewModeId === previewProps.previewModeId const isOnDemandRevalidate = previewModeId === previewProps.previewModeId
const revalidateOnlyGenerated = headers.has( const revalidateOnlyGenerated = headers.hasOwnProperty(
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER
) )
......
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