Skip to content
Dashboard

Function streaming to be framework-agnostic on Vercel

Streaming will soon be enabled by default for all Node.js Vercel Functions

Copy link to headingWhat changes when streaming responses?

Copy link to headingResponses on runtime errors

api/index.js
import { setTimeout } from "node:timers/promises";
export default async function (req, res) {
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("Chunk 1\\n");
await setTimeout(1000);
throw new Error("Oh no!");
}

An example function that writes to the response with a delay.

HTTP/2 500
cache-control: public, max-age=0, must-revalidate
content-type: text/plain; charset=utf-8
date: Mon, 24 Jun 2024 10:26:35 GMT
server: Vercel
strict-transport-security: max-age=63072000; includeSubDomains; preload
x-robots-tag: noindex
x-vercel-error: FUNCTION_INVOCATION_FAILED
x-vercel-id: cdg1::hgz6d-1719224792404-c4031128df3b
content-length: 56
A server error has occurred
FUNCTION_INVOCATION_FAILED

An example of a 500 error response from a Vercel Function.

HTTP/2 200
age: 0
cache-control: public, max-age=0, must-revalidate
content-type: text/plain
date: Mon, 24 Jun 2024 10:29:58 GMT
server: Vercel
strict-transport-security: max-age=63072000; includeSubDomains; preload
x-robots-tag: noindex
x-vercel-cache: MISS
x-vercel-execution-region: iad1
x-vercel-id: cdg1::iad1::zclv9-1719224997952-452256b220b0
Chunk 1

An example of a 200 response from a streamed function.

Copy link to headingEnhanced runtime logs experience

import { setTimeout } from "node:timers/promises";
export default async function (req, res) {
console.log("Before setting a response");
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("Chunk 1\\n");
await setTimeout(1000);
console.warn("Before writing a second chunk");
res.write("Chunk 2\\n");
await setTimeout(1000);
console.error("Before ending the response");
res.end("Done!");
}

An example function that writes a response with a delay.

Copy link to headingEnabling streaming functions today

Ready to deploy?