nextjs - 💡(How to fix) Fix Caching issue where the ETag is generated ONLY based on the response body

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Fix / Workaround

Today I used patch-package to patch [email protected] for the project I'm working on.

This patch resolves a specific caching issue where the ETag is generated ONLY based on the response body, causing:

THIS PATCH DOES:

  • Adds: const origin = req.headers.origin;

Code Example

diff --git a/node_modules/next/dist/server/api-utils/node/api-resolver.js b/node_modules/next/dist/server/api-utils/node/api-resolver.js
index c2c9bdc..ea6af17 100644
--- a/node_modules/next/dist/server/api-utils/node/api-resolver.js
+++ b/node_modules/next/dist/server/api-utils/node/api-resolver.js
@@ -60,13 +60,14 @@ function getMaxContentLength(responseLimit) {
         body.pipe(res);
         return;
     }
+    const origin = req.headers.origin;
     const isJSONLike = [
         'object',
         'number',
         'boolean'
     ].includes(typeof body);
     const stringifiedBody = isJSONLike ? JSON.stringify(body) : body;
-    const etag = (0, _etag.generateETag)(stringifiedBody);
+    const etag = (0, _etag.generateETag)(stringifiedBody.concat(origin || ''));
     if ((0, _sendpayload.sendEtagResponse)(req, res, etag)) {
         return;
     }
RAW_BUFFERClick to expand / collapse

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

This patch resolves a specific caching issue where the ETag is generated ONLY based on the response body, causing:

  1. CORS Issues: Different origins receive the same ETag for identical responses.
  2. Incorrect Cache: Cached responses may be served to different origins.
  3. Problematic Headers: Origin is not considered in ETag generation.

THIS PATCH DOES:

  • Adds: const origin = req.headers.origin;

  • Modifies: const etag = (0, _etag.generateETag)(stringifiedBody.concat(origin || ''));

The FALLBACK (|| '') avoids errors if origin is undefined/null and maintains functionality even without an origin header. RESULT: Unique ETag per origin, avoiding incorrect cross-origin caching.

Here is the diff that solved my problem:

diff --git a/node_modules/next/dist/server/api-utils/node/api-resolver.js b/node_modules/next/dist/server/api-utils/node/api-resolver.js
index c2c9bdc..ea6af17 100644
--- a/node_modules/next/dist/server/api-utils/node/api-resolver.js
+++ b/node_modules/next/dist/server/api-utils/node/api-resolver.js
@@ -60,13 +60,14 @@ function getMaxContentLength(responseLimit) {
         body.pipe(res);
         return;
     }
+    const origin = req.headers.origin;
     const isJSONLike = [
         'object',
         'number',
         'boolean'
     ].includes(typeof body);
     const stringifiedBody = isJSONLike ? JSON.stringify(body) : body;
-    const etag = (0, _etag.generateETag)(stringifiedBody);
+    const etag = (0, _etag.generateETag)(stringifiedBody.concat(origin || ''));
     if ((0, _sendpayload.sendEtagResponse)(req, res, etag)) {
         return;
     }

<em>This issue body was partially generated by patch-package.</em>

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING