notes-api's frontend now runs on Vercel. The pages are fast, but every call to /api/hello from Singapore takes 230 ms. It turns out the function runs in iad1, Washington D.C., a new project's default region, halfway around the world from its visitors.
Where a function runs, and on which engine, is your choice. Vercel Functions are the server code you deploy together with a Next.js app on Vercel. Each function uses one of two runtimes: Node.js (full Node, running in one region you choose) or Edge (a V8 isolate like Cloudflare Workers: small, and running close to the visitor). Middleware is code that runs before a request reaches any page. Picture a housing estate with a guard at the gate and a management office inside. The guard checks every guest quickly. A guest with no access card is sent to the registration desk first. A guest from Germany is pointed to the signboard in German, then comes in through the same gate. But the guard can't open the filing cabinets: paperwork stays with the management office. In this picture, a guest is a request, the guard is the middleware and the Edge functions, and the management office is a Node.js function. The access card is the login cookie, the registration desk is the /login page, the signboard in German is the /de version of the page, and the filing cabinets are the database.
Two runtimes on Vercel
notes-api's frontend, notes-web, moves to Next.js on Vercel, at https://app.notes.example.com. Its functions can use two runtimes:
| Node.js | Edge | |
|---|---|---|
| Engine | full Node.js | V8 isolate, Web APIs only |
| Runs where | the region you choose (new projects default to iad1, Washington D.C.) |
the region closest to the visitor |
fs, child_process, native modules (explained below) |
yes | no |
| A fit for | database queries, file work, heavy work | redirects, cookie checks, A/B testing (splitting visitors between two versions of a page), geolocation (the visitor's location) |
Today Vercel recommends moving from Edge to Node.js. Next.js marks runtime = "edge" as deprecated (no longer recommended), and from Next.js 16.3 Vercel no longer supports it: routes and pages run on Node.js. You still meet Edge in middleware and in older projects.
Middleware: a guard in front of every page
This is the middleware of notes-web:
// middleware.ts (Next.js 13 to 15; in Next.js 16 it is called proxy.ts)
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// 1. The dashboard requires a login
const session = request.cookies.get("session");
if (!session && pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/login", request.url));
}
// 2. Visitors from Germany get the /de version of the page
const country = request.headers.get("x-vercel-ip-country") ?? "US";
if (country === "DE" && !pathname.startsWith("/de")) {
return NextResponse.rewrite(new URL(`/de${pathname}`, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next|static|favicon.ico).*)"],
};
NextResponse.redirect(url): the browser is told to move, with status307and alocationheader. The address bar changes to/login.NextResponse.rewrite(url): Vercel quietly serves another page. If a visitor from Germany opens/, the address bar stays/, but the content comes from/de/.NextResponse.next(): continue to the page that was asked for, unchanged.x-vercel-ip-countryis a header Vercel adds with the visitor's country code (DE,US,SG). The helper (a small function)geolocation(request)from the@vercel/functionspackage reads the same header.- The order matters: check the login first, then the country. In the other order, a visitor from Germany with no session (the cookie that shows they logged in) who opens
/dashboardis rewritten to/de/dashboard, and the login check never runs.
A visitor from the United States with no session cookie who opens /dashboard/settings gets a 307 redirect to /login.
The matcher: which requests go through middleware
Middleware runs for every request that matches matcher. Next.js turns the pattern "/((?!_next|static|favicon.ico).*)" into a regular expression (a pattern for matching text): every path, except those starting with _next, static or favicon.ico. These are the results in the Lab (a visitor from the United States, no session):
| Path | Through middleware? | Result |
|---|---|---|
/ |
yes | next(), the home page |
/dashboard/settings |
yes | 307 redirect to /login |
/notes/42 |
yes | next() |
/_next/static/chunks/main.js |
no | the JavaScript file is sent right away |
/favicon.ico |
no | the icon is sent right away |
So the middleware doesn't run for /_next/static/chunks/main.js. Excluding static files makes the requests for those files faster and saves money, because Vercel counts every middleware run as an invocation.
An Edge API route: export const runtime = "edge"
In the App Router (the way Next.js arranges pages and routes in the app/ folder) up to Next.js 16.2, one line in the route file picks its runtime. You will still meet it in older projects:
// app/api/hello/route.ts
export const runtime = "edge"; // deploy to the edge network, not as a Node.js function
export async function GET(request: Request) {
return Response.json({
message: "Hello from edge!",
region: process.env.VERCEL_REGION,
});
}
runtime = "edge": this route is deployed to the edge network (Vercel's servers in many cities) and runs in a V8 isolate, like Cloudflare Workers. Without that line, the default is"nodejs".process.env.VERCEL_REGIONholds the region running it. For a visitor in Singapore, an Edge function runs insin1, the closest Vercel region.- In a new project, remove the
runtimeline. The route runs on Node.js, andexport const preferredRegion = "sin1"moves it close to the visitor or to the database.
Regions: close to the user or close to the data
Example round trips from Singapore (the same as in the Cloud Computing Basics lesson): Singapore 5 ms, Virginia 230 ms.
/api/hello runs as |
Runs in | Time to a visitor in Singapore (example) |
|---|---|---|
| Edge | sin1, the closest |
5 ms |
| Node.js, default region | iad1 |
230 ms |
| Node.js, region changed | sin1 |
5 ms |
A Node.js function's region is changed in the project settings, in vercel.json ("regions": ["sin1"]), or per route with export const preferredRegion = "sin1". Pick the region close to its database. An Edge function that runs near the visitor, but asks a database in Virginia three times, waits 3 × 230 ms. Work that talks to the database a lot is faster when the function sits next to the database.
What the Edge runtime lacks
The Edge runtime brings only Web APIs (the standard APIs that browsers also have): fetch, Request, Response, URL, Headers, TextEncoder, streams and crypto.subtle. What it lacks:
fsandchild_process: it can't read files from disk or run other programs.- Native modules (modules holding code compiled from another language, not JavaScript) such as
sharporbcrypt, and database drivers that need a plain TCP connection, such aspg. evalandnew Function: code built while the program is running is refused.require: code on Edge usesimport.
An Edge route that imports fs fails at build time; it doesn't quietly move to Node.js. Its code size limit is also smaller than a Node.js function's.
📌 Next.js version notes
- Next.js 15 removed
request.geoandrequest.ipfromNextRequest. Old code likerequest.geo?.countryis nowundefined. Use thex-vercel-ip-countryheader orgeolocation(request). - Next.js 15.5 lets middleware run on the Node.js runtime (
export const config = { runtime: "nodejs" }). - Next.js 16 renames
middleware.tstoproxy.tsand its function toproxy, which runs on the Node.js runtime.middleware.tsstill works but is deprecated; keepmiddleware.tswhen you need the Edge runtime. Check your project's Next.js version before copying examples from the internet.
⚠️ Middleware that does too much, or runs in the wrong place
- A matcher that catches everything. Without excluding
_nextand static files, the middleware runs for every JavaScript file, image and icon: slower and more expensive. - Checking the country before the login. The rewrite to
/dehappens first, and the login check for visitors from Germany is skipped. - Querying a database from an Edge function far from it. Being close to the visitor is no use when every query crosses 230 ms.
- Using
request.geoin Next.js 15 and later. It is always empty, so with a fallback like?? "US"in the middleware above, every visitor looks like they come from the United States.
TL;DR: Vercel Functions (server code deployed together with a Next.js app on Vercel) have two runtimes. Node.js is full Node, running in one region (iad1 in Washington D.C. by default). Edge is a V8 isolate like Cloudflare Workers: it runs in the region closest to the visitor, but with no fs, child_process, native modules or eval. export const runtime = "edge" deploys a route to the edge network (Vercel's servers in many cities), but it is deprecated (no longer recommended). From Next.js 16.3, Vercel runs routes on Node.js, and their region is picked with preferredRegion. Middleware runs before the page, for every path that matches the matcher pattern. It answers with NextResponse.next() (continue to the page), redirect (307, the address in the browser changes) or rewrite (the address stays, the content comes from another path). In middleware, check the login before the country, so a rewrite to a country's version can't skip the login check. Edge fits auth (login checks), redirects, A/B testing and geolocation (the visitor's location). Database queries, file work and heavy computing belong in Node.js, in a region close to the database. In Next.js 15 request.geo is gone (use the x-vercel-ip-country header), and in Next.js 16 middleware is called proxy.ts.
⚡ Two runtimes
Guess first: Vercel can run a function as an Edge Function, which runs in the region closest to the visitor but has limited features. Which job fits an Edge Function best: a database query for a monthly report, or redirecting visitors to the version of the site for their country?
The redirect by country. Edge fits small work that must sit close to the visitor, such as redirects, cookie checks and geolocation (the visitor's location). Database queries and heavy work belong on the Node.js runtime.
Meaning: A function on Vercel uses one of two runtimes. Node.js is full Node, running in one region you choose. Edge is a V8 isolate (like Cloudflare Workers) running in the region closest to the visitor, with no fs (reading files from disk), child_process (running other programs) or native modules.
⚡ Middleware: a guard in front of the pages
Write:
// 1. The dashboard requires a login
const session = request.cookies.get("session");
if (!session && pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/login", request.url));
}
// 2. Visitors from Germany get the /de version of the page
const country = request.headers.get("x-vercel-ip-country") ?? "US";
if (country === "DE" && !pathname.startsWith("/de")) {
return NextResponse.rewrite(new URL(`/de${pathname}`, request.url));
}
Meaning: Middleware is code that runs before a request reaches the page, like a guard at the gate. This code checks the login first, then the country. In the other order, a visitor from Germany with no session (the cookie that shows they logged in) who opens /dashboard is rewritten to /de/dashboard, and the login check is never reached. redirect changes the address in the browser (status 307), while rewrite doesn't change it: only the content comes from another path.
⚡ An Edge route and its region
Write:
export const runtime = "edge"; // deploy to the edge network, not as a Node.js function
Meaning: This line picks the Edge runtime for one route. Without that line, the default is "nodejs". This line is deprecated (no longer recommended), and from Next.js 16.3 Vercel no longer supports it. Pick a function region close to the database. An Edge function near the visitor that asks a database in Virginia three times waits 3 × 230 ms (230 ms for each round trip).