Skip to content

fix(event-handler): set Vary: Accept-Encoding in compress middleware - #5888

Merged
dreamorosi merged 3 commits into
mainfrom
fix/event-handler-compress-vary
Nov 2, 2026
Merged

dreamorosi merged 3 commits into
mainfrom
fix/event-handler-compress-vary

Conversation

@dreamorosi

Copy link
Copy Markdown
Contributor

Summary

compress() set Content-Encoding but never Vary, so shared caches such as CloudFront could serve a gzip body to a client that didn't ask for one. The middleware now adds Accept-Encoding to Vary whenever the response could be compressed, keeping any value already there (for example Origin from cors()). This matches the fix in aws-powertools/powertools-lambda-python#8711.

Changes

  • compress() adds Accept-Encoding to Vary when the response qualifies for compression: when it's compressed, when the client doesn't accept the encoding, and on HEAD requests. It doesn't add it when compression was never possible whatever the client sent: below the threshold, already encoded, Cache-Control: no-transform, no body, or streaming.
  • The old shouldCompress check is split into a response-only check (isCompressible), which decides Vary, and a request check (HEAD, Accept-Encoding), which decides compression.
  • New appendVaryHeader helper in middleware/commons.ts. It adds a field name to an existing Vary value, skips names already listed (ignoring case), and leaves Vary: * untouched.
  • cors() uses the same helper for Origin, so it no longer overwrites a Vary set earlier.
  • Unit tests in compress.test.ts and cors.test.ts cover when Vary is and isn't set, combining it with existing values, and cors() and compress() together.
  • The docs sample response advanced_compress_res.json now includes Vary, and the Compress section of the HTTP event handler docs describes the behaviour.
  • Note: the three appsync registry test files only change formatting (public declare → declare public). Biome now reformats them, likely after a Biome update, and they'd otherwise fail the lint check in CI.

Issue number: closes #5886


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Responses that compress() could encode now list Accept-Encoding in Vary,
whether or not the client accepts the encoding, so shared caches keep
compressed and plain bodies apart. Existing Vary values are kept,
duplicates are skipped, and Vary: * is left alone. The cors middleware
uses the same helper, so it no longer overwrites an earlier Vary.

Fixes #5886
Biome now places declare before access modifiers, likely after a Biome update.
@dreamorosi
dreamorosi enabled auto-merge (squash) November 2, 2026 13:19
@dreamorosi
dreamorosi merged commit 529b2c1 into main Nov 2, 2026
41 checks passed
@dreamorosi
dreamorosi deleted the fix/event-handler-compress-vary branch November 2, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L PRs between 100-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants