Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/runtime/nitro-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
: ''
}

/**
Expand Down
36 changes: 35 additions & 1 deletion test/compression-v1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<h1>Hello World</h1>'
const json = {
Expand Down Expand Up @@ -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)
})
})
15 changes: 15 additions & 0 deletions test/nuxt-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down
Loading