@@ -410,8 +410,8 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
410410 let cacheKey ;
411411 let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL ;
412412
413- /** @type { Response|undefined } */
414- let responseToCache ;
413+ // Whether to cache the final response in the end.
414+ let toCacheResponse = false ;
415415
416416 /** @type {Response|FileResponse|undefined } */
417417 let response ;
@@ -475,14 +475,14 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
475475 cacheKey = proposedCacheKey ;
476476 }
477477
478-
479- if ( cache && response instanceof Response && response . status === 200 ) {
480- // only clone if cache available, and response is valid
481- responseToCache = response . clone ( ) ;
482- }
478+ // Only cache the response if:
479+ toCacheResponse =
480+ cache // 1. A caching system is available
481+ && typeof Response !== 'undefined' // 2. `Response` is defined (i.e., we are in a browser-like environment)
482+ && response instanceof Response // 3. result is a `Response` object (i.e., not a `FileResponse`)
483+ && response . status === 200 // 4. request was successful (status code 200)
483484 }
484485
485-
486486 // Start downloading
487487 dispatchCallback ( options . progress_callback , {
488488 status : 'download' ,
@@ -499,16 +499,18 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
499499 } )
500500 } )
501501
502-
503502 if (
504503 // Only cache web responses
505504 // i.e., do not cache FileResponses (prevents duplication)
506- responseToCache && cacheKey
505+ toCacheResponse && cacheKey
507506 &&
508507 // Check again whether request is in cache. If not, we add the response to the cache
509508 ( await cache . match ( cacheKey ) === undefined )
510509 ) {
511- await cache . put ( cacheKey , responseToCache )
510+ // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files
511+ await cache . put ( cacheKey , new Response ( buffer , {
512+ headers : response . headers
513+ } ) )
512514 . catch ( err => {
513515 // Do not crash if unable to add to cache (e.g., QuotaExceededError).
514516 // Rather, log a warning and proceed with execution.
0 commit comments