Unverified Commit 86050df6 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

perf: lazy eval headers from the requestStore (#41353)

The `ReadOnlyHeaders` class extends the `Headers` class that is provided
via a polyfill. This incurs some costs the first time it's created
because we need to load that polyfill. This saves approximately 10ms on
my machine on a cold request.

This is only useful when a user does not access headers during the
request of course and when the runtime does not support fetch naitvely.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a 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 a 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/examples/adding-examples.md)
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 4ff348cb
...@@ -83,6 +83,7 @@ const INTERNAL_COOKIES_INSTANCE = Symbol('internal for cookies readonly') ...@@ -83,6 +83,7 @@ const INTERNAL_COOKIES_INSTANCE = Symbol('internal for cookies readonly')
function readonlyCookiesError() { function readonlyCookiesError() {
return new Error('ReadonlyCookies cannot be modified') return new Error('ReadonlyCookies cannot be modified')
} }
class ReadonlyNextCookies { class ReadonlyNextCookies {
[INTERNAL_COOKIES_INSTANCE]: NextCookies [INTERNAL_COOKIES_INSTANCE]: NextCookies
...@@ -1602,18 +1603,33 @@ export async function renderToHTMLOrFlight( ...@@ -1602,18 +1603,33 @@ export async function renderToHTMLOrFlight(
(renderOpts as any).previewProps (renderOpts as any).previewProps
) )
let cachedHeadersInstance: ReadonlyHeaders | undefined
let cachedCookiesInstance: ReadonlyNextCookies | undefined
const requestStore = { const requestStore = {
headers: new ReadonlyHeaders(headersWithoutFlight(req.headers)), get headers() {
cookies: new ReadonlyNextCookies({ if (!cachedHeadersInstance) {
headers: { cachedHeadersInstance = new ReadonlyHeaders(
get: (key) => { headersWithoutFlight(req.headers)
if (key !== 'cookie') { )
throw new Error('Only cookie header is supported') }
} return cachedHeadersInstance
return req.headers.cookie },
}, get cookies() {
}, if (!cachedCookiesInstance) {
}), cachedCookiesInstance = new ReadonlyNextCookies({
headers: {
get: (key) => {
if (key !== 'cookie') {
throw new Error('Only cookie header is supported')
}
return req.headers.cookie
},
},
})
}
return cachedCookiesInstance
},
previewData, previewData,
} }
......
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