Edge Functions
Edge Functions allow you to run server-side code closer to your users than ever before. Unlike traditional serverless functions that run in a single region (like Finland-1), Edge Functions are deployed globally and execute in the region closest to the request.
Example
Here is a simple Edge Function that returns a personalized greeting based on the user's location.
terminal
// api/hello.ts
import type { NextRequest } from 'next/server';
export const config = {
runtime: 'edge',
};
export default function handler(req: NextRequest) {
const { searchParams } = new URL(req.url);
const name = searchParams.get('name') || 'World';
const country = req.geo?.country || 'the internet';
return new Response(
JSON.stringify({
message: `Hello ${name} from ${country}!`,
timestamp: Date.now(),
}),
{
status: 200,
headers: {
'content-type': 'application/json',
},
}
);
}
Runtime Environment
Nept Edge Functions run on the V8 engine, similar to Cloudflare Workers or Vercel Edge. This means:
- Fast Startup: No cold starts typical of Node.js containers.
- Web Standards: Uses standard Web APIs like
Request,Response,fetch, andStreams. - Limitations: Native Node.js APIs (like
fs,child_process) are not available. You must use polyfills or alternative strategies.
Use Cases
- Authentication & Authorization: Verify JWTs at the edge before the request hits your origin.
- A/B Testing: Bucket users into different variants using cookies without client-side flicker.
- Geolocation Routing: Redirect users to localized content based on their IP address.
- Custom Headers: Add security headers or CORS policies dynamically.
Performance Limits
| Metric | Limit | | :--- | :--- | | Execution Time (Wall Clock) | 30s | | CPU Time | 50ms (Soft limit) | | Memory | 128 MB | | Deployment Size | 1 MB (Compressed) |