@@ -6,6 +6,7 @@ import type {
6
6
} from '../shared-types/types'
7
7
import { useHttpClientHintsState } from './state'
8
8
import { lookupHeader , writeClientHintHeaders , writeHeaders } from './headers'
9
+ import { browserFeatureAvailable } from './features'
9
10
import {
10
11
defineNuxtPlugin ,
11
12
useCookie ,
@@ -100,31 +101,6 @@ const allowedBrowsers: [browser: Browser, features: BrowserFeatures][] = [
100
101
101
102
const ClientHeaders = [ 'Accept-CH' , 'Vary' , 'Critical-CH' ]
102
103
103
- function browserFeatureAvailable ( userAgent : ReturnType < typeof parseUserAgent > , feature : AcceptClientHintsHeadersKey ) {
104
- if ( userAgent == null || userAgent . type !== 'browser' )
105
- return false
106
-
107
- try {
108
- const browserName = userAgent . name
109
- const android = userAgent . os ?. toLowerCase ( ) . startsWith ( 'android' ) ?? false
110
- const versions = userAgent . version . split ( '.' ) . map ( v => Number . parseInt ( v ) )
111
- return allowedBrowsers . some ( ( [ name , check ] ) => {
112
- if ( browserName !== name )
113
- return false
114
-
115
- try {
116
- return check [ feature ] ( android , versions )
117
- }
118
- catch {
119
- return false
120
- }
121
- } )
122
- }
123
- catch {
124
- return false
125
- }
126
- }
127
-
128
104
function lookupClientHints (
129
105
userAgent : ReturnType < typeof parseUserAgent > ,
130
106
criticalClientHintsConfiguration : CriticalClientHintsConfiguration ,
@@ -145,21 +121,21 @@ function lookupClientHints(
145
121
return features
146
122
147
123
if ( criticalClientHintsConfiguration . prefersColorScheme )
148
- features . prefersColorSchemeAvailable = browserFeatureAvailable ( userAgent , 'prefersColorScheme' )
124
+ features . prefersColorSchemeAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'prefersColorScheme' )
149
125
150
126
if ( criticalClientHintsConfiguration . prefersReducedMotion )
151
- features . prefersReducedMotionAvailable = browserFeatureAvailable ( userAgent , 'prefersReducedMotion' )
127
+ features . prefersReducedMotionAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'prefersReducedMotion' )
152
128
153
129
if ( criticalClientHintsConfiguration . prefersReducedTransparency )
154
- features . prefersReducedMotionAvailable = browserFeatureAvailable ( userAgent , 'prefersReducedTransparency' )
130
+ features . prefersReducedMotionAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'prefersReducedTransparency' )
155
131
156
132
if ( criticalClientHintsConfiguration . viewportSize ) {
157
- features . viewportHeightAvailable = browserFeatureAvailable ( userAgent , 'viewportHeight' )
158
- features . viewportWidthAvailable = browserFeatureAvailable ( userAgent , 'viewportWidth' )
133
+ features . viewportHeightAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'viewportHeight' )
134
+ features . viewportWidthAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'viewportWidth' )
159
135
}
160
136
161
137
if ( criticalClientHintsConfiguration . width ) {
162
- features . widthAvailable = browserFeatureAvailable ( userAgent , 'width' )
138
+ features . widthAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'width' )
163
139
}
164
140
165
141
if ( features . viewportWidthAvailable || features . viewportHeightAvailable ) {
@@ -173,7 +149,7 @@ function lookupClientHints(
173
149
headers ,
174
150
)
175
151
if ( mobileHeader )
176
- features . devicePixelRatioAvailable = browserFeatureAvailable ( userAgent , 'devicePixelRatio' )
152
+ features . devicePixelRatioAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'devicePixelRatio' )
177
153
}
178
154
179
155
return features
0 commit comments