@@ -6,7 +6,7 @@ import type {
6
6
CriticalClientHintsConfiguration ,
7
7
} from '../shared-types/types'
8
8
import { useHttpClientHintsState } from './state'
9
- import { writeClientHintHeaders , writeHeaders } from './headers'
9
+ import { lookupHeader , writeClientHintHeaders , writeHeaders } from './headers'
10
10
import {
11
11
defineNuxtPlugin ,
12
12
useCookie ,
@@ -171,8 +171,12 @@ function lookupClientHints(
171
171
// Since sec-ch-ua-mobile is a low entropy header, we don't need to include it in Accept-CH,
172
172
// the user agent will send it always unless blocked by a user agent permission policy, check:
173
173
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile
174
- const mobileHeader = headers [ SecChUaMobile ]
175
- if ( mobileHeader === '?1' )
174
+ const mobileHeader = lookupHeader (
175
+ 'boolean' ,
176
+ SecChUaMobile ,
177
+ headers ,
178
+ )
179
+ if ( mobileHeader )
176
180
features . devicePixelRatioAvailable = browserFeatureAvailable ( userAgent , 'devicePixelRatio' )
177
181
}
178
182
@@ -239,48 +243,56 @@ function collectClientHints(
239
243
}
240
244
241
245
if ( hints . viewportHeightAvailable && criticalClientHintsConfiguration . viewportSize ) {
242
- const header = headers [ AcceptClientHintsRequestHeaders . viewportHeight ]
243
- if ( header ) {
246
+ const viewportHeight = lookupHeader (
247
+ 'int' ,
248
+ AcceptClientHintsRequestHeaders . viewportHeight ,
249
+ headers ,
250
+ )
251
+ if ( typeof viewportHeight === 'number' ) {
244
252
hints . firstRequest = false
245
- try {
246
- hints . viewportHeight = Number . parseInt ( header )
247
- }
248
- catch {
249
- hints . viewportHeight = criticalClientHintsConfiguration . clientHeight
250
- }
253
+ hints . viewportHeight = viewportHeight
254
+ }
255
+ else {
256
+ hints . viewportHeight = criticalClientHintsConfiguration . clientHeight
251
257
}
252
258
}
253
259
else {
254
260
hints . viewportHeight = criticalClientHintsConfiguration . clientHeight
255
261
}
256
262
257
263
if ( hints . viewportWidthAvailable && criticalClientHintsConfiguration . viewportSize ) {
258
- const header = headers [ AcceptClientHintsRequestHeaders . viewportWidth ]
259
- if ( header ) {
264
+ const viewportWidth = lookupHeader (
265
+ 'int' ,
266
+ AcceptClientHintsRequestHeaders . viewportWidth ,
267
+ headers ,
268
+ )
269
+ if ( typeof viewportWidth === 'number' ) {
260
270
hints . firstRequest = false
261
- try {
262
- hints . viewportWidth = Number . parseInt ( header )
263
- }
264
- catch {
265
- hints . viewportWidth = criticalClientHintsConfiguration . clientWidth
266
- }
271
+ hints . viewportWidth = viewportWidth
272
+ }
273
+ else {
274
+ hints . viewportWidth = criticalClientHintsConfiguration . clientWidth
267
275
}
268
276
}
269
277
else {
270
278
hints . viewportWidth = criticalClientHintsConfiguration . clientWidth
271
279
}
272
280
273
281
if ( hints . devicePixelRatioAvailable && criticalClientHintsConfiguration . viewportSize ) {
274
- const header = headers [ AcceptClientHintsRequestHeaders . devicePixelRatio ]
275
- if ( header ) {
282
+ const devicePixelRatio = lookupHeader (
283
+ 'float' ,
284
+ AcceptClientHintsRequestHeaders . devicePixelRatio ,
285
+ headers ,
286
+ )
287
+ if ( typeof devicePixelRatio === 'number' ) {
276
288
hints . firstRequest = false
277
289
try {
278
- hints . devicePixelRatio = Number . parseFloat ( header )
279
- if ( ! Number . isNaN ( hints . devicePixelRatio ) && hints . devicePixelRatio > 0 ) {
290
+ hints . devicePixelRatio = devicePixelRatio
291
+ if ( ! Number . isNaN ( devicePixelRatio ) && devicePixelRatio > 0 ) {
280
292
if ( typeof hints . viewportWidth === 'number' )
281
- hints . viewportWidth = Math . round ( hints . viewportWidth / hints . devicePixelRatio )
293
+ hints . viewportWidth = Math . round ( hints . viewportWidth / devicePixelRatio )
282
294
if ( typeof hints . viewportHeight === 'number' )
283
- hints . viewportHeight = Math . round ( hints . viewportHeight / hints . devicePixelRatio )
295
+ hints . viewportHeight = Math . round ( hints . viewportHeight / devicePixelRatio )
284
296
}
285
297
}
286
298
catch {
@@ -290,15 +302,14 @@ function collectClientHints(
290
302
}
291
303
292
304
if ( hints . widthAvailable && criticalClientHintsConfiguration . width ) {
293
- const header = headers [ AcceptClientHintsRequestHeaders . width ]
294
- if ( header ) {
305
+ const width = lookupHeader (
306
+ 'int' ,
307
+ AcceptClientHintsRequestHeaders . width ,
308
+ headers ,
309
+ )
310
+ if ( typeof width === 'number' ) {
295
311
hints . firstRequest = false
296
- try {
297
- hints . width = Number . parseInt ( header )
298
- }
299
- catch {
300
- // just ignore
301
- }
312
+ hints . width = width
302
313
}
303
314
}
304
315
0 commit comments