From f11eb501e6b42e99b9a24cc9ff56465be8a16763 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 24 Nov 2026 09:49:22 +0100 Subject: [PATCH] fix(nuxt): read the content type from the node response on h3 v1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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')`. --- src/runtime/nitro-plugin.ts | 20 ++++++++++++++++++-- test/compression-v1.test.ts | 36 +++++++++++++++++++++++++++++++++++- test/nuxt-plugin.test.ts | 15 +++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/runtime/nitro-plugin.ts b/src/runtime/nitro-plugin.ts index 34daf4b..5aa12e3 100644 --- a/src/runtime/nitro-plugin.ts +++ b/src/runtime/nitro-plugin.ts @@ -67,7 +67,8 @@ export function getPath(event: H3Event): string { /** * Reads the response content type from whichever shape the hook provided. * `render:response` carries a plain header record, `beforeResponse` may carry - * none at all — in which case the already-set response headers are used. + * none at all — in which case the already-set response headers are used, from + * `event.res` on h3 v2 or the node response on h3 v1. * @param { H3Event } event - A H3 event object. * @param { HookResponse } response - The response object handed over by the hook. * @returns { string } @@ -80,8 +81,23 @@ export function getContentType(event: H3Event, response: HookResponse): string { return direct } + // h3 v2 keeps the response headers on `event.res`. const resHeaders = (event as unknown as { res?: { headers?: Headers } }).res?.headers - return resHeaders?.get?.('content-type') ?? '' + const fromEvent = resHeaders?.get?.('content-type') + if (fromEvent) + return fromEvent + + // h3 v1 (Nitro 2, i.e. Nuxt 3 / 4) writes them straight to the node + // response — that is where Nitro's static handler and `setResponseHeader` + // put the content type of public assets and API routes. + const nodeRes = (event as unknown as { node?: { res?: { getHeader?: (name: string) => unknown } } }).node?.res + const fromNode = nodeRes?.getHeader?.('content-type') + if (Array.isArray(fromNode)) + return fromNode.join(', ') + + return typeof fromNode === 'string' + ? fromNode + : '' } /** diff --git a/test/compression-v1.test.ts b/test/compression-v1.test.ts index 58ebf6f..9104392 100644 --- a/test/compression-v1.test.ts +++ b/test/compression-v1.test.ts @@ -21,7 +21,7 @@ function rawParser(res: any, cb: (err: Error | null, body: Buffer) => void) { // `createApp` / `eventHandler` / `toNodeListener` exist in both h3 versions, but // the `onBeforeResponse` app hook only works in v1. -const { createApp, eventHandler, toNodeListener } = h3 as typeof import('h3') +const { createApp, eventHandler, setResponseHeader, toNodeListener } = h3 as typeof import('h3') const html = '

Hello World

' const json = { @@ -212,4 +212,38 @@ describe.runIf(isV1)('nuxt plugin on h3 v1 (#22)', () => { expect(result.headers['content-encoding']).toBeUndefined() expect(result.text).toEqual(html) }) + + // On h3 v1 the content type a handler (or Nitro's static handler) sets lives + // on the node response, not on `event.res` — it was read as empty, which + // counts as compressible, so the `contentTypes` filter never applied. + function typedAppWith(contentType: string, body: string | Buffer) { + const app = createApp({ + onBeforeResponse: (event, response) => applyCompression(event, response, {}), + }) + app.use('/', eventHandler((event) => { + setResponseHeader(event, 'Content-Type', contentType) + return body + })) + return supertest(toNodeListener(app)) + } + + it('skips content types outside the filter set on the event', async () => { + const png = Buffer.alloc(2048, 7) + const result = await typedAppWith('image/png', png) + .get('/') + .set('Accept-Encoding', 'gzip') + .buffer(true) + .parse(rawParser) + + expect(result.headers['content-encoding']).toBeUndefined() + expect(result.headers['content-type']).toEqual('image/png') + expect(Buffer.compare(result.body, png)).toEqual(0) + }) + + it('still compresses content types inside the filter set on the event', async () => { + const result = await typedAppWith('text/css', html).get('/').set('Accept-Encoding', 'gzip') + + expect(result.headers['content-encoding']).toEqual('gzip') + expect(result.text).toEqual(html) + }) }) diff --git a/test/nuxt-plugin.test.ts b/test/nuxt-plugin.test.ts index 7b9ad0a..8457cc6 100644 --- a/test/nuxt-plugin.test.ts +++ b/test/nuxt-plugin.test.ts @@ -91,6 +91,21 @@ describe.runIf(isV2)('nuxt plugin — content type handling (#22)', () => { expect(getContentType(event, {})).toEqual('text/plain') }) + + it('reads the content type from the node response on h3 v1', () => { + const v1Event = { + node: { + res: { + getHeader: (name: string) => name === 'content-type' + ? 'image/png' + : undefined, + }, + }, + } as any + + expect(getContentType(v1Event, {})).toEqual('image/png') + expect(shouldCompress(v1Event, {}, {})).toBe(false) + }) }) describe.runIf(isV2)('nuxt plugin — shouldCompress (#22)', () => {