Edge Functions Development for Websites (Vercel Edge)
Vercel Edge Functions run on the Vercel Edge Network — on 100+ nodes worldwide, milliseconds from the user. Cold start less than 1 ms. This distinguishes them from regular Vercel Functions, which run in a single AWS region.
When Edge, When Regular Functions
Edge Functions are optimal for: personalization (A/B testing, geolocation), redirects and rewrites based on conditions, middleware authentication, header and response transformation, cache validation by cookie.
Regular Node.js Functions are needed when: Node.js API is required (fs, crypto, native modules), TCP connection to PostgreSQL, execution time > 30 s, memory > 128 MB.
Edge Runtime uses Web API (like in browsers), not Node.js API.
Middleware for A/B Testing
File middleware.ts in the root of Next.js project:
import { NextRequest, NextResponse } from "next/server";
export function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
// A/B test for homepage
if (url.pathname === "/") {
const bucket = request.cookies.get("ab-bucket")?.value;
if (!bucket) {
const newBucket = Math.random() < 0.5 ? "a" : "b";
const response = NextResponse.rewrite(
new URL(newBucket === "b" ? "/home-variant" : "/", request.url)
);
response.cookies.set("ab-bucket", newBucket, { maxAge: 86400 * 30 });
return response;
}
if (bucket === "b") {
url.pathname = "/home-variant";
return NextResponse.rewrite(url);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/", "/pricing", "/features"],
};
Geolocation and Personalization
import { NextRequest, NextResponse } from "next/server";
import { geolocation } from "@vercel/functions";
export function middleware(request: NextRequest) {
const { country, city } = geolocation(request);
// Redirect to localized version
if (country === "RU" && !request.nextUrl.pathname.startsWith("/ru")) {
return NextResponse.redirect(
new URL(`/ru${request.nextUrl.pathname}`, request.url)
);
}
// Add geo data to headers for components
const response = NextResponse.next();
response.headers.set("x-user-country", country || "unknown");
response.headers.set("x-user-city", city || "unknown");
return response;
}
Edge API Route
// app/api/edge-data/route.ts
import { NextRequest, NextResponse } from "next/server";
export const runtime = "edge";
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
// Fetch works natively in Edge Runtime
const data = await fetch(`https://api.external.com/data/${id}`, {
headers: { Authorization: `Bearer ${process.env.API_KEY}` },
// next.js cache: cache for 60 seconds
next: { revalidate: 60 },
}).then(r => r.json());
return NextResponse.json(data, {
headers: { "Cache-Control": "s-maxage=60, stale-while-revalidate=120" }
});
}
Route Protection via JWT
import { NextRequest, NextResponse } from "next/server";
import { jwtVerify } from "jose";
const JWT_SECRET = new TextEncoder().encode(process.env.JWT_SECRET);
export async function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith("/dashboard")) {
const token = request.cookies.get("auth-token")?.value;
if (!token) {
return NextResponse.redirect(new URL("/login", request.url));
}
try {
await jwtVerify(token, JWT_SECRET);
return NextResponse.next();
} catch {
const response = NextResponse.redirect(new URL("/login", request.url));
response.cookies.delete("auth-token");
return response;
}
}
}
jose is the only JWT library compatible with Edge Runtime (uses Web Crypto API instead of node:crypto).
Edge Runtime Limitations
- No
node:fs,node:path,node:crypto(Web Crypto available) - No native npm packages
- Memory: 128 MB
- CPU time: 30 ms (Hobby), unlimited on Pro
- No direct connections to PostgreSQL (Neon and PlanetScale support HTTP API)
Timeframe
Middleware with geolocation and A/B testing — 1–2 days. Edge API Routes with caching and JWT protection — 2–3 days.







