Blog>
Snippets

Streaming responses in Edge runtime

Provide an example on how to stream large datasets in a Next.js API route using the Edge runtime for efficient data handling.
export const config = {
  runtime: 'experimental-edge',
};

export default async function handler(req, res) {
  // Initialize the ReadableStream
  const stream = new ReadableStream({
    start(controller) {
      // Large dataset loop simulation
      for (let i = 0; i < 1e6; i++) {
        controller.enqueue(`Data chunk ${i}\n`);
      }
      controller.close();
    }
  });

  // Set headers for streaming content
  res.setHeader('Content-Type', 'text/plain; charset=utf-8');
  // Respond with the stream
  return new Response(stream);
}
This code defines an API route in a Next.js application using the Edge runtime. It creates a stream of large datasets, encodes that data, and uses the Fetch API's Response interface to return it as a stream to the client. A loop simulates large dataset stream generation. The 'Content-Type' header is set to 'text/plain' to indicate the nature of the streamed content.