Skip to content

Bug: compress() doesn't set Vary: Accept-Encoding #5886

Closed

Description

@Manishram-ai

Expected Behavior

Responses that go through the compress() middleware include Vary: Accept-Encoding, added to any Vary value that's already set, so shared caches like CloudFront keep the gzip and plain versions apart.

Current Behavior

compress() sets Content-Encoding: gzip but never sets Vary. When cors() is used too, the response only has Vary: Origin. A shared cache can then send a gzip body to a client that didn't ask for one. The sample response in the docs (advanced_compress_res.json) shows the same thing: Content-Encoding: gzip and no Vary.

Code snippet

import { Router } from '@aws-lambda-powertools/event-handler/http';
import { compress } from '@aws-lambda-powertools/event-handler/http/middleware';
import type { Context } from 'aws-lambda';

const app = new Router();

app.use(compress());

app.get('/data', async () => ({ data: 'x'.repeat(5000) }));

export const handler = async (event: unknown, context: Context) =>
  app.resolve(event, context);

Steps to Reproduce

  1. Call the handler with an API Gateway REST event for GET /data with Accept-Encoding: gzip.
  2. The response has Content-Encoding: gzip and no Vary header.

Possible Solution

Add Accept-Encoding to Vary on responses that go through compress(), keeping any existing value (like Origin from cors()) and leaving Vary: * alone. The Python version is making the same change in aws-powertools/powertools-lambda-python#8711. I'm happy to send a PR for this.

Powertools for AWS Lambda (TypeScript) version

2.36.0

AWS Lambda function runtime

22.x (ran locally on Node.js 22)

Packaging format used

npm

Execution logs

No response

Activity

  1. self-assigned this
    on Nov 2, 2026
  2. added theissue type on Nov 2, 2026
  3. dreamorosi commented on Nov 2, 2026

    @dreamorosi
    Contributor

    Thank you for reporting the issue, I was able to confirm the bug and will be opening a PR to fix it shortly.

    Thanks for offering to fix it, in this instance I found it faster to open the PR since there were a few decision points that would have caused back & forth if discussed here, specifically around how to handle the consistency with CORS.

  4. powertools-for-aws-oss-automation commented on Nov 2, 2026

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

event-handlerThis item relates to the Event Handler Utilitypending-releaseThis item has been merged and will be released soon

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions