Skip to content

Why doesn’t streaming(ReadableStream) work for Next.js API routes on AWS Amplify Hosting?

0

I’m hosting a Next.js app on AWS Amplify Hosting and trying to stream API responses (e.g., using ReadableStream) from my /api/... routes.

Locally (next dev) streaming works as expected, but in Amplify production the entire response is buffered and only sent after completion, there’s no incremental data sent to the client.

I’ve confirmed that:

  • My API route returns a proper ReadableStream.
  • Environment variables are available and the API logic runs fine, only streaming fails.
  • From my research (AWS docs on SSR support) and GitHub discussions, it seems Amplify Hosting might not support streaming yet for Next.js API routes.

Question: Can Amplify Hosting currently support streaming responses from Next.js API routes? If not, is there an official workaround or planned timeline for enabling it?


minimal reproducible example

// app/api/test/route.ts
export async function POST() {
  const encoder = new TextEncoder();

  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue(encoder.encode("Starting...\n"));

      let i = 0;
      const interval = setInterval(() => {
        i++;
        controller.enqueue(encoder.encode(`Chunk ${i}\n`));

        if (i >= 10) {
          clearInterval(interval);
          controller.enqueue(encoder.encode("Done!\n"));
          controller.close();
        }
      }, 1000);
    },
  });

  return new Response(stream, {
    headers: {
      "Content-Type": "text/plain; charset=utf-8",
      "Transfer-Encoding": "chunked",
    },
  });
}

asked a year ago480 views

1 Answer
0
Accepted Answer

Based on the available information, AWS Amplify Hosting does not currently natively support streaming responses from Next.js API routes. This explains why your streaming implementation works locally with next dev but fails when deployed to Amplify Hosting, where the entire response is buffered and only sent after completion.

The issue is similar to challenges faced with API Gateway, which also doesn't natively support streaming responses or chunked transfer encoding by default. When you deploy a Next.js application to Amplify Hosting, the underlying infrastructure may be buffering your response rather than streaming it incrementally to the client.

For streaming functionality in AWS environments, there are a few potential workarounds:

  1. WebSocket API: While not your preferred solution, implementing a WebSocket API would be the most straightforward way to achieve true streaming behavior in AWS environments.

  2. Custom solution at the application level: You could implement a custom streaming solution by breaking your response into smaller chunks and having the client reassemble them, or implementing long-polling techniques.

  3. Direct Lambda integration: If your use case allows, you might consider exposing certain streaming functionality directly through AWS Lambda with response streaming capabilities, bypassing some of the Amplify Hosting limitations.

  4. Polling mechanism: As an alternative approach, you could implement a polling mechanism where the client periodically checks for updates from the backend.

It's worth noting that even if you manage to get chunked transfer encoding working, there might be timeout limits that could interfere with long-running streams.

There is no publicly available timeline for when Amplify Hosting might add native support for streaming responses from Next.js API routes. For critical streaming requirements, you might need to consider alternative hosting solutions or implement one of the workarounds mentioned above.
Sources
Streaming response from api gateway | AWS re:Post
Streaming Http response from Lambda function with Amplify Gen2 | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.