Next.js Discord

Discord Forum

Next.js API Route Responses Not Gzipped Despite Accept-Encoding Header

Unanswered
Florida White posted this in #help-forum
Open in Discord
Avatar
Florida WhiteOP
Hi everyone,

I've noticed that Next.js doesn't gzip API route responses even when the Accept-Encoding: gzip header is included in the request. Here's what I've tested so far:

Environment:

Next.js versions: 14.1.0 and 15.0.4
Simple API route under the App Router, returning a JSON payload larger than 1KB.
Using curl to send requests with Accept-Encoding: gzip.
Issue:

Static files are gzipped as expected, but API route responses are not gzipped.
The Content-Encoding: gzip header is missing, and the response size remains uncompressed.
Reproduction Steps:

Create a simple API route:
export async function GET() {
    const data = Array(5000).fill({ id: 1, name: "example" });
    return new Response(JSON.stringify(data), {
        headers: { "Content-Type": "application/json" },
    });
}
Start the application in development mode using next dev. Send a GET request using:curl -H "Accept-Encoding: gzip" http://localhost:3000/api/example`

Observe that the Content-Encoding header is missing, and the response size is uncompressed.

Question: Does Next.js rely on external tools like Nginx or a CDN for gzip compression in API routes? Or is there a built-in way to enable gzip compression for API responses?

https://github.com/vercel/next.js/issues/73695

1 Reply

Avatar
Chub mackerel
Hey! 👋 You're absolutely right—Next.js doesn’t gzip API route responses out of the box. Static files are gzipped in production, but for API routes, compression isn’t built-in. Instead, Next.js expects you to handle this at the server or proxy level (like with Nginx, Vercel, or Cloudflare).

If you want to enable gzip for API responses, here are two approaches:

1️⃣ Use a custom compression library
You can manually gzip the response in your API route using Node.js's zlib or similar libraries. This involves compressing your data and setting the Content-Encoding header to gzip.

2️⃣ Deploy behind a CDN or proxy
Platforms like Vercel, AWS CloudFront, or Nginx can handle response compression automatically when configured.

If you need help with either of these, feel free to ask! 😊