Skip to content

fix: preserve status and headers when compressing a Fetch Response - #420

Merged
climba03003 merged 1 commit into
fastify:mainfrom
priitkaard:main
Aug 4, 2026
Merged

climba03003 merged 1 commit into
fastify:mainfrom
priitkaard:main

Conversation

@priitkaard

Copy link
Copy Markdown
Contributor

Compressing a Fetch Response drops its status code and headers.

Fastify doesn't unwrap a Response before onSend - it does that afterwards in onSendEnd and
only if the payload is still a Response. We swap in the compressed stream before then, so that
never runs and reply.code()/reply.header() are never called.

Practically this means content-type, Cache-Control and Set-Cookie disappear and every status
becomes 200.

app.register(compress, { threshold: 0 })
app.get('/', (_req, reply) =>
  reply.send(new Response('hello', { status: 401, headers: { 'set-cookie': 'a=b' } })))
accept-encoding: gzip      -> 200, no set-cookie
accept-encoding: identity  -> 401, set-cookie: a=b

Fix applies the status and headers when we take the body, same as onSendEnd would have.

This affects every tRPC v11 app using this plugin, since its Fastify adapter always sends a
Response, and those are streams so the threshold never skips them.
Related issue: trpc/trpc#6628.

Checklist

@mcollina mcollina 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

@climba03003
climba03003 merged commit a782a78 into fastify:main Aug 4, 2026
14 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