Skip to content

fix(etag): avoid skipping headers when filtering 304 response headers - #5234

Merged
yusukebe merged 2 commits into
honojs:mainfrom
spellsaif:fix/etag-304-header-filtering
Aug 16, 2026
Merged

yusukebe merged 2 commits into
honojs:mainfrom
spellsaif:fix/etag-304-header-filtering

Conversation

@spellsaif

Copy link
Copy Markdown
Contributor

Fixes an issue in the etag middleware where non-retained headers could be skipped during 304 response filtering due to mutating the Headers instance during forEach iteration.

Problem

In src/middleware/etag/index.ts, non-retained headers are deleted inside c.res.headers.forEach:

    c.res.headers.forEach((_, key) => {
      if (retainedHeaders.indexOf(key.toLowerCase()) === -1) {
        c.res.headers.delete(key)
      }
    })

Under standard Headers (and Map) iterator semantics, calling .delete() on the underlying collection during .forEach() shifts internal iterator indices, causing the iterator to skip subsequent header entries.

Solution

Collect the keys to delete in a temporary array first, and then delete them in a separate loop so the collection being iterated is not mutated in-flight.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@usualoma

Copy link
Copy Markdown
Member

Hi @spellsaif,

Thanks for opening this PR. Nice catch! The proposed fix is functionally correct as written, and I don’t see any behavioral issue with it.

That said, how about using Array.from() to snapshot the header keys before deleting them? This avoids manually building headersToDelete and keeps the code shorter.

Both approaches allocate a temporary array. The performance difference may depend on the proportion of headers being deleted, but with realistic header counts, I expect it to be too small to measure in practice.

      for (const key of Array.from(c.res.headers.keys())) {
        if (retainedHeaders.indexOf(key.toLowerCase()) === -1) {
          c.res.headers.delete(key)
        }
      }

@spellsaif

Copy link
Copy Markdown
Contributor Author

@usualoma Hi! Thanks for the review and suggestion. :)

I've updated the implementation to use Array.from(c.res.headers.keys()) as suggested. It keeps the code much cleaner and concise. All tests are passing green.

@usualoma

Copy link
Copy Markdown
Member

Thank you 👍

@codecov

codecov Bot commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.77%. Comparing base (41bdc42) to head (224a89c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5234   +/-   ##
=======================================
  Coverage   79.77%   79.77%           
=======================================
  Files         155      155           
  Lines       10934    10934           
  Branches     2292     2292           
=======================================
  Hits         8723     8723           
  Misses       2211     2211           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe yusukebe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe

Copy link
Copy Markdown
Member

@spellsaif @usualoma

Ah, this was actually a bug. Thanks!

@yusukebe
yusukebe merged commit 546eca0 into honojs:main Aug 16, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants