perf(nuxt): avoid multiple iterations in useRequestHeaders (#24853)

This commit is contained in:
Michael Brevard 2023-12-26 22:07:11 +02:00 committed by GitHub
parent 9de21173a8
commit 97a6715b2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,9 +13,17 @@ export function useRequestHeaders (): Readonly<Record<string, string>>
export function useRequestHeaders (include?: any[]) {
if (import.meta.client) { return {} }
const event = useRequestEvent()
const headers = event ? getRequestHeaders(event) : {}
if (!include) { return headers }
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
const _headers = event ? getRequestHeaders(event) : {}
if (!include || !event) { return _headers }
const headers = Object.create(null)
for (const _key of include) {
const key = _key.toLowerCase()
const header = _headers[key]
if (header) {
headers[key] = header
}
}
return headers
}
export function useRequestHeader(header: string) {