@@ -82,17 +82,30 @@ class XHR {
8282 }
8383
8484 size ( ) {
85+ // Use a Range request to get the actual file size from Content-Range header.
86+ // This avoids issues with Content-Length returning compressed size on some CDNs.
8587 let retry = 0 ;
8688 let size = - 1 ;
8789 this . xhr . onload = ( ) => {
88- size = + this . xhr . getResponseHeader ( 'Content-Length' ) ;
90+ const range = this . xhr . getResponseHeader ( 'Content-Range' ) ;
91+ if ( range ) {
92+ // Content-Range: bytes 0-99/1234567 -> extract total size after '/'
93+ const match = range . match ( / \/ ( \d + ) / ) ;
94+ if ( match ) {
95+ size = + match [ 1 ] ;
96+ }
97+ }
98+ if ( size < 0 ) {
99+ // Fallback to Content-Length if Content-Range is not available
100+ size = + this . xhr . getResponseHeader ( 'Content-Length' ) ;
101+ }
89102 } ;
90103 do {
91104 retry += 1 ;
92- this . xhr . open ( 'HEAD ' , this . url , false ) ;
93- this . xhr . setRequestHeader ( 'Accept-Encoding ' , 'identity' )
105+ this . xhr . open ( 'GET ' , this . url , false ) ;
106+ this . xhr . setRequestHeader ( 'Range ' , 'bytes=0-0' ) ;
94107 this . xhr . send ( null ) ;
95- } while ( retry < 3 && this . xhr . status != 200 )
108+ } while ( retry < 3 && this . xhr . status != 206 && this . xhr . status != 200 ) ;
96109 this . _size = size ;
97110 return size ;
98111 }
@@ -121,7 +134,6 @@ class XHR {
121134 do {
122135 retry += 1 ;
123136 this . xhr . open ( 'GET' , this . url , false ) ;
124- this . xhr . setRequestHeader ( 'Accept-Encoding' , 'identity' )
125137 this . xhr . setRequestHeader ( 'Range' , `bytes=${ pos } -${ Math . min ( this . _size - 1 , pos + len - 1 ) } ` ) ;
126138 this . xhr . send ( null ) ;
127139 } while ( retry < 3 && this . xhr . status != 206 ) ;
0 commit comments