@@ -6,6 +6,7 @@ import type {
66} from '../shared-types/types'
77import { useHttpClientHintsState } from './state'
88import { lookupHeader , writeClientHintHeaders , writeHeaders } from './headers'
9+ import { browserFeatureAvailable } from './features'
910import {
1011 defineNuxtPlugin ,
1112 useCookie ,
@@ -100,31 +101,6 @@ const allowedBrowsers: [browser: Browser, features: BrowserFeatures][] = [
100101
101102const ClientHeaders = [ 'Accept-CH' , 'Vary' , 'Critical-CH' ]
102103
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-
128104function lookupClientHints (
129105 userAgent : ReturnType < typeof parseUserAgent > ,
130106 criticalClientHintsConfiguration : CriticalClientHintsConfiguration ,
@@ -145,21 +121,21 @@ function lookupClientHints(
145121 return features
146122
147123 if ( criticalClientHintsConfiguration . prefersColorScheme )
148- features . prefersColorSchemeAvailable = browserFeatureAvailable ( userAgent , 'prefersColorScheme' )
124+ features . prefersColorSchemeAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'prefersColorScheme' )
149125
150126 if ( criticalClientHintsConfiguration . prefersReducedMotion )
151- features . prefersReducedMotionAvailable = browserFeatureAvailable ( userAgent , 'prefersReducedMotion' )
127+ features . prefersReducedMotionAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'prefersReducedMotion' )
152128
153129 if ( criticalClientHintsConfiguration . prefersReducedTransparency )
154- features . prefersReducedMotionAvailable = browserFeatureAvailable ( userAgent , 'prefersReducedTransparency' )
130+ features . prefersReducedMotionAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'prefersReducedTransparency' )
155131
156132 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' )
159135 }
160136
161137 if ( criticalClientHintsConfiguration . width ) {
162- features . widthAvailable = browserFeatureAvailable ( userAgent , 'width' )
138+ features . widthAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'width' )
163139 }
164140
165141 if ( features . viewportWidthAvailable || features . viewportHeightAvailable ) {
@@ -173,7 +149,7 @@ function lookupClientHints(
173149 headers ,
174150 )
175151 if ( mobileHeader )
176- features . devicePixelRatioAvailable = browserFeatureAvailable ( userAgent , 'devicePixelRatio' )
152+ features . devicePixelRatioAvailable = browserFeatureAvailable ( allowedBrowsers , userAgent , 'devicePixelRatio' )
177153 }
178154
179155 return features
0 commit comments