Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): gzip large thread snapshots #4788

Mergedt3dotgg(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/4788/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHub

Thread snapshots still crossed HTTP uncompressed, so large histories remained expensive to load even after activity payload projection.

This adds content-negotiated gzip for JSON responses above 1 KB. On cloned real T3 data, a 6,852,257-byte thread snapshot was delivered in 1,306,499 bytes (80.9% smaller), and decompression reproduced the original payload byte-for-byte. Browser, desktop, and React Native clients use their native HTTP stacks to decode it transparently.

Tests:

  • vp test run apps/server/src/http.test.ts apps/server/test/ActivityPayloadProjection.test.ts apps/server/src/server.test.ts --testNamePattern='http compression|projectActivityPayload|negotiates permessage-deflate'
  • vp run --filter t3 typecheck

Built with GPT-5.6-sol in T3 Code using the Codex harness.


Note

Medium Risk Global middleware changes how every large JSON response is sent (streaming body, no Content-Length when compressed), which can affect caching and clients that assume fixed-length bodies.

Overview Adds global HTTP middleware that gzip-compresses eligible application/json responses (≥1 KB, in-memory body, no existing Content-Encoding) when the client’s Accept-Encoding allows gzip, using a streaming CompressionStream('gzip') body and dropping Content-Length.

Eligible responses always get Vary: Accept-Encoding, merged with any existing Vary without changing * or duplicate values. Clients that decline gzip keep the original body; only Vary may be added.

httpCompressionLayer is wired into the composed server stack so all routes get this behavior. Unit tests cover compressHttpResponse and an integration test hits a real route with accept-encoding: gzip.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit b51dd02d4a6e71a2f8b90690f9b3f5676dd28418. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Add gzip compression middleware for large JSON thread snapshots in the server

  • Adds compressHttpResponse in http.ts that gzip-compresses JSON responses ≥1024 bytes when the client sends Accept-Encoding: gzip, using a streaming CompressionStream('gzip') body.
  • Always sets a Vary: Accept-Encoding header on eligible responses, preserving any existing Vary value (including *).
  • Wires in httpCompressionLayer as a global middleware in server.ts so all routes benefit automatically.
  • Behavioral Change: compressed responses drop Content-Length and switch to a streaming body; uncompressed responses are unchanged except for the added Vary header.
Macroscope summarized b51dd02.

Summary by CodeRabbit

  • New Features

    • Added automatic gzip compression for eligible large JSON responses when the client supports it.
    • Updated compression handling to set the correct negotiation headers (including Vary: Accept-Encoding) and stream compressed output.
    • Preserves existing Vary headers and avoids recompressing responses already encoded.
  • Tests

    • Added HTTP and router-level coverage for gzip-compressed responses, accurate decompression, and correct behavior when gzip is declined.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: d4117db2-9300-4d72-9c68-224602a51616

      📥 Commits

      Reviewing files that changed from the base of the PR and between 1aa1d1da15c101c32b76be3c15897acd3f5b9ed1 and b51dd02d4a6e71a2f8b90690f9b3f5676dd28418.

      📒 Files selected for processing (4)
      • apps/server/src/http.test.ts
      • apps/server/src/http.ts
      • apps/server/src/server.test.ts
      • apps/server/src/server.ts
      🚧 Files skipped from review as they are similar to previous changes (3)
      • apps/server/src/server.ts
      • apps/server/src/http.test.ts
      • apps/server/src/http.ts

      📝 Walkthrough

      Walkthrough

      Adds gzip compression for eligible JSON HTTP responses, exposes it as middleware, integrates it into server routes, and tests compressed, uncompressed, and end-to-end response behavior.

      Changes

      HTTP compression

      Layer / File(s)Summary
      Compression middleware and response handling
      apps/server/src/http.ts
      Adds gzip negotiation, streamed compression, and Vary, content-encoding, and content-length handling for eligible JSON responses.
      Server route integration
      apps/server/src/server.ts
      Provides httpCompressionLayer in the composed server routes layer.
      Compression behavior validation
      apps/server/src/http.test.ts, apps/server/src/server.test.ts
      Tests accepted and declined gzip negotiation, existing Vary preservation, decoded payloads, and route-level compression.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Suggested reviewers: juliusmarminge, d3oxy

      Sequence Diagram(s)

      sequenceDiagram
        participant Client
        participant httpCompressionLayer
        participant compressHttpResponse
        participant HTTPResponse
        Client->>httpCompressionLayer: Send request with accept-encoding
        httpCompressionLayer->>compressHttpResponse: Pass response and encoding
        compressHttpResponse->>HTTPResponse: Return gzip stream and updated headers
        HTTPResponse-->>Client: Send compressed or original body
      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly summarizes the main change: adding gzip compression for large server thread snapshots.
      Description check✅ PassedThe description explains what changed, why, and includes tests; the template sections are present in content though not fully formatted.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      📝 Generate docstrings
      • Create stacked PR
      • Commit on current branch
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `t3code/test-websocket-payload-improvements`

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      ✅ Action performed

      Review finished.

      Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Approved

      Performance optimization adding gzip compression for large JSON responses (>= 1024 bytes). The middleware is self-contained with conservative conditions, proper HTTP header handling, and good test coverage.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. Actionable comments posted: 2

      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Inline comments:
      In `@apps/server/src/http.ts`:
      - Around line 78-83: Update the Vary header handling in the variedResponse
      construction to preserve a wildcard value of "*" unchanged and avoid appending
      Accept-Encoding when it is already present. Keep appending Accept-Encoding only
      for non-wildcard values that do not already contain that token.
      
      In `@apps/server/src/server.ts`:
      - Line 376: Add a focused request-level test in the existing HTTP test suite
      that serves the makeRoutesLayer configuration and sends Accept-Encoding: gzip,
      then assert the compressible response is gzip-encoded. Ensure the test exercises
      the global httpCompressionLayer registration through the route-serving path
      rather than testing compressHttpResponse directly.
      🪄 Autofix (Beta)

      Fix all unresolved CodeRabbit comments on this PR:

      • Push a commit to this branch (recommended)
      • Create a new PR with the fixes

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 4ccb6b05-3688-49b1-ad4e-4714d096577b

      📥 Commits

      Reviewing files that changed from the base of the PR and between b64ae880e0f88cd35e2f7e1f5fab868546fbe29b and 1aa1d1da15c101c32b76be3c15897acd3f5b9ed1.

      📒 Files selected for processing (3)
      • apps/server/src/http.test.ts
      • apps/server/src/http.ts
      • apps/server/src/server.ts
    6. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      ✅ Action performed

      Review finished.

      Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign in to join this conversation.