Edge functions let you run code in 30+ regions worldwide, responding to requests in milliseconds. This guide will show you how to get started and explore advanced patterns.
Why Edge Functions?
Traditional serverless functions run in a single region. If your users are in Tokyo but your function runs in Virginia, every request adds 200ms of latency.
Edge functions solve this by running your code in the region closest to each user. The same function, deployed everywhere.
Getting Started
Create a new edge function:
export default function handler(request: Request) {
return new Response('Hello from the edge!', {
headers: { 'content-type': 'text/plain' },
});
}Deploy it:
platform deploy --edgeThat's it. Your function is now running globally.
Common Use Cases
A/B Testing
Run experiments without client-side flicker by making routing decisions at the edge.
Geolocation
Personalize content based on the user's country, city, or region.
Authentication
Validate tokens and redirect unauthenticated users before they hit your origin.
Performance Tips
- Keep functions small—aim for under 1MB
- Use streaming responses for large payloads
- Cache aggressively with appropriate headers
- Avoid blocking I/O when possible
Edge functions are one of the most powerful tools in modern web development. Start small, measure everything, and iterate.