Skip to content

Commit e0482ba

Browse files
author
Nathan Booker
committed
Update version to 0.6.3 and clone responses in cachedFetch to prevent body consumption issues
- Bumped version in package.json to 0.6.3. - Modified cachedFetch function to clone responses before creating new Response objects, ensuring the original response body is preserved for further use.
1 parent 2dac10a commit e0482ba

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cached-middleware-fetch-next",
3-
"version": "0.6.2",
3+
"version": "0.6.3",
44
"description": "A Next.js fetch wrapper for edge middleware that uses Vercel Runtime Cache as its caching backend",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)