@@ -141,31 +141,43 @@ function checkIsImage (src, onResult) {
141141 if ( request . status >= 200 && request . status < 300 ) {
142142 contentType = request . getResponseHeader ( 'Content-Type' ) ;
143143 if ( contentType == null ) {
144- checkIsImageFallback ( src , onResult ) ;
144+ // No content-type, try Image. If fails, assume video.
145+ checkIsImageFallback ( src , onResult , false ) ;
145146 } else if ( contentType . startsWith ( 'image' ) ) {
146147 onResult ( true ) ;
147148 } else {
148149 onResult ( false ) ;
149150 }
150151 } else {
151- // Non-success status (404, etc.) - resource not found.
152- onResult ( null ) ;
152+ // Non-success status (3xx redirects, 404, 405, etc.) - try loading via Image tag
153+ // as it handles redirects and the server might not support HEAD requests.
154+ // If Image also fails, resource is not found.
155+ checkIsImageFallback ( src , onResult , null ) ;
153156 }
154157 request . abort ( ) ;
155158 } ) ;
156159 request . addEventListener ( 'error' , function ( ) {
157- // Network error - resource not found.
158- onResult ( null ) ;
160+ // Network error (CORS, etc.) - try loading via Image tag.
161+ // If Image also fails, resource is not found.
162+ checkIsImageFallback ( src , onResult , null ) ;
159163 } ) ;
160164 request . send ( ) ;
161165}
162166
163- function checkIsImageFallback ( src , onResult ) {
167+ /**
168+ * Try loading src as an image to determine if it's an image.
169+ *
170+ * @param {string } src - URL to test.
171+ * @param {function } onResult - Callback with result.
172+ * @param {boolean|null } onErrorResult - Value to pass to onResult if image fails to load.
173+ * false = assume video, null = resource not found.
174+ */
175+ function checkIsImageFallback ( src , onResult , onErrorResult ) {
164176 var tester = new Image ( ) ;
165177 tester . addEventListener ( 'load' , onLoad ) ;
166178 function onLoad ( ) { onResult ( true ) ; }
167179 tester . addEventListener ( 'error' , onError ) ;
168- function onError ( ) { onResult ( false ) ; }
180+ function onError ( ) { onResult ( onErrorResult ) ; }
169181 tester . src = src ;
170182}
171183
0 commit comments