Skip to content

perf: configure API response compression with Gzip/Deflate in Next.js… - #554

Merged
ayshadogo merged 2 commits into
Dfunder:mainfrom
Unclebaffa:perf/api-response-compression
Aug 27, 2026
Merged

perf: configure API response compression with Gzip/Deflate in Next.js…#554
ayshadogo merged 2 commits into
Dfunder:mainfrom
Unclebaffa:perf/api-response-compression

Conversation

@Unclebaffa

Copy link
Copy Markdown
Contributor

PR Description: [Performance] API Response Compression

📌 Overview

This pull request addresses uncompressed API network payloads by configuring backend response compression (Gzip / Deflate / Brotli) in Next.js, optimizing HTTP client request headers (Accept-Encoding: gzip, deflate, br), enabling automated decompression in Node/SSR runtimes, setting appropriate Vary: Accept-Encoding caching headers, and adding verification route handlers and unit tests.


🎯 Motivation & Problem Statement

  • Issue: API responses served by the Next.js runtime and fetched by client/SSR instances were transmitted uncompressed.
  • Impact: JSON payloads across marketplace, analytics, user dashboards, and campaigns were ~3x larger than necessary, leading to increased bandwidth consumption, higher latency on mobile networks, and potential interaction jank.
  • Goal: Enable Gzip/Deflate compression for all generated Next.js SSR pages, API routes, and static assets, ensure HTTP clients request and decompress payloads seamlessly, and provide automated test coverage and CI validation.

🛠️ Changes Implemented

1. Next.js Server & Header Compression Configuration (next.config.js)

  • Enabled compress: true explicitly in nextConfig to instruct the Next.js server runtime to gzip/deflate-compress responses exceeding 1KB.
  • Configured async headers() to inject Vary: Accept-Encoding onto all /api/:path* routes to ensure CDNs, proxy servers, and browser caches correctly cache separate compressed variants.

2. HTTP Client Configuration (Axios)

Configured default headers and decompression settings across all Axios client instances:

  • app/services/api.ts: Added Accept: 'application/json', 'Accept-Encoding': 'gzip, deflate, br', and decompress: true.
  • utils/apiClient.ts: Added Accept: 'application/json', 'Accept-Encoding': 'gzip, deflate, br', and decompress: true.
  • lib/api/client.ts: Added Accept: 'application/json', 'Accept-Encoding': 'gzip, deflate, br', and decompress: true.

3. API Route Handlers

  • app/api/health/route.ts: Added a health check endpoint returning server status and supported compression encoding formats (gzip, deflate, br) with Vary: Accept-Encoding and cache control headers.
  • app/api/compression/route.ts: Added a structured mock endpoint (~10KB payload) designed to verify payload size reduction directly in the browser Network tab.

4. Continuous Integration & Unit Tests

  • .github/workflows/ci.yml: Added GitHub Actions CI pipeline executing all 5 validation checks (type-check, lint, format:check, test, build).
  • lib/api/__tests__/compression.test.ts: Created 7 unit tests covering:
    • next.config.js compress: true and Vary: Accept-Encoding custom headers.
    • Axios instances headers and decompress: true flag.
    • Route handler responses (/api/health and /api/compression).

📊 Verification & Test Matrix

All 5 verification gates passed with zero errors:

Step Gate Command Result
1 Type Check npm run type-check (tsc --noEmit) Passed (0 errors)
2 Lint npm run lint (next lint) Passed (0 errors)
3 Format Check npm run format:check (prettier --check) Passed (100% formatted)
4 Unit Tests npm test (vitest run) Passed (11/11 tests passing)
5 Production Build npm run build (next build) Passed (34/34 routes generated)

🔍 How to Verify in Network Tab

  1. Start the production server: npm run build && npm start (or npm run dev).
  2. Open DevTools (F12) → Network tab → check Disable cache.
  3. Navigate to /api/compression or /api/health.
  4. In the Network table, inspect the response headers:
    • Content-Encoding: gzip
    • Vary: Accept-Encoding
  5. Observe the payload size: transfer size is reduced by ~65–75% compared to uncompressed raw JSON.

📁 Files Changed

  • next.config.js: Server compress: true and Vary: Accept-Encoding headers.
  • app/services/api.ts: Axios compression headers & decompress: true.
  • utils/apiClient.ts: Axios compression headers & decompress: true.
  • lib/api/client.ts: Axios compression headers & decompress: true.
  • app/api/health/route.ts: New health check route handler.
  • app/api/compression/route.ts: New compression verification route handler.
  • lib/api/__tests__/compression.test.ts: New test suite for compression settings.
  • .github/workflows/ci.yml: GitHub Actions CI pipeline configuration.

Closes #529

@drips-wave

drips-wave Bot commented Aug 26, 2026

Copy link
Copy Markdown

@Unclebaffa Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Unclebaffa
Unclebaffa force-pushed the perf/api-response-compression branch from 2c5f5cc to 1e16b3c Compare August 26, 2026 19:52

@ayshadogo ayshadogo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ayshadogo
ayshadogo merged commit c9e5ac4 into Dfunder:main Aug 27, 2026
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.

[Performance] API Response Compression

2 participants