@@ -383,8 +383,11 @@ export async function cachedFetch(
383383 if ( cacheOption === 'no-store' || revalidate === 0 ) {
384384 const response = await fetch ( input , cleanOptions ) ;
385385
386+ // Clone the response to avoid body consumption issues
387+ const responseClone = response . clone ( ) ;
388+
386389 // Add cache status headers to indicate cache was bypassed
387- const responseWithCacheHeaders = new Response ( response . body , {
390+ const responseWithCacheHeaders = new Response ( responseClone . body , {
388391 status : response . status ,
389392 statusText : response . statusText ,
390393 headers : new Headers ( response . headers )
@@ -452,8 +455,12 @@ export async function cachedFetch(
452455 // Fetch from origin (cache miss or expired)
453456 const response = await fetch ( input , cleanOptions ) ;
454457
458+ // Clone the response first to avoid body consumption issues
459+ const responseForCaching = response . clone ( ) ;
460+ const responseForReturn = response . clone ( ) ;
461+
455462 // Add cache status headers to indicate this was a miss
456- const responseWithCacheHeaders = new Response ( response . body , {
463+ const responseWithCacheHeaders = new Response ( responseForReturn . body , {
457464 status : response . status ,
458465 statusText : response . statusText ,
459466 headers : new Headers ( response . headers )
@@ -463,7 +470,7 @@ export async function cachedFetch(
463470
464471 // Only cache successful responses (2xx) and GET/POST/PUT requests
465472 if ( response . ok && ( method === 'GET' || method === 'POST' || method === 'PUT' ) ) {
466- const cacheEntry = await responseToCache ( response . clone ( ) , init ) ;
473+ const cacheEntry = await responseToCache ( responseForCaching , init ) ;
467474
468475 // Store in cache with appropriate TTL
469476 const cacheTTL = computeTTL ( cacheEntry . expiresAt ) ;
@@ -479,8 +486,11 @@ export async function cachedFetch(
479486 console . error ( '[cached-middleware-fetch] Cache operation failed:' , error ) ;
480487 const fallbackResponse = await fetch ( input , cleanOptions ) ;
481488
489+ // Clone the response to avoid body consumption issues
490+ const fallbackResponseClone = fallbackResponse . clone ( ) ;
491+
482492 // Add cache status headers to indicate this was a miss due to error
483- const responseWithCacheHeaders = new Response ( fallbackResponse . body , {
493+ const responseWithCacheHeaders = new Response ( fallbackResponseClone . body , {
484494 status : fallbackResponse . status ,
485495 statusText : fallbackResponse . statusText ,
486496 headers : new Headers ( fallbackResponse . headers )
0 commit comments