Skip to content

fix(nuxt): read the content type from the node response on h3 v1 - #32

Merged
CodeDredd merged 1 commit into
mainfrom
fix/nuxt-content-type-v1
Nov 24, 2026
Merged

CodeDredd merged 1 commit into
mainfrom
fix/nuxt-content-type-v1

Conversation

@CodeDredd

Copy link
Copy Markdown
Owner

Found while verifying #28–#31 end-to-end in the Nuxt 4.7 playground — no separate issue.

Problem

The module's contentTypes filter didn't apply on h3 v1 (Nitro 2 → Nuxt 3 and 4) for anything going through the beforeResponse hook.

getContentType() falls back to the headers already set on the event when the hook hands over no header record — but it only looked at h3 v2's event.res.headers. On h3 v1 those headers live on the node response, so the content type that a handler or Nitro's static handler had set always read as empty. An empty content type is deliberately treated as compressible (it usually means an object body that h3 will serialize as JSON), so images, fonts and other binary public assets were compressed too.

Observed in the playground: /favicon.ico (image/vnd.microsoft.icon) came back with Content-Encoding: gzip although image/* isn't in the default contentTypes.

Fix

Fall back to event.node.res.getHeader('content-type') when event.res has nothing (array values are joined).

Tests

  • unit test: getContentType / shouldCompress on a v1-shaped event;
  • h3 v1 app hook: a handler that sets image/png is left untouched (fails on main), text/css set the same way is still compressed.

End-to-end in the Nuxt 4.7 playground after the fix: /favicon.ico is served unencoded with its original Content-Length; the SSR page and /api/items are still compressed with Vary: Accept-Encoding.

`getContentType()` only looked at h3 v2's `event.res.headers`. On h3 v1 —
Nitro 2, so Nuxt 3 and 4 — response headers live on the node response, so
in the `beforeResponse` hook the content type set by a handler or by
Nitro's static handler always read as empty. An empty content type counts
as compressible (it usually means an object body), so the `contentTypes`
filter never applied there: images, fonts and other binary public assets
were compressed as well.

It now falls back to `event.node.res.getHeader('content-type')`.
@CodeDredd
CodeDredd merged commit 08e2d4c into main Nov 24, 2026
13 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.

1 participant