U

Ultimate Edge Functions Engine

Enterprise-ready skill that automates low-latency edge middleware and geolocation logic. Built for Claude Code with best practices and real-world patterns.

SkillCommunitydevopsv1.0.0MIT
0 views0 copies

Edge Functions Engine

Complete edge function development guide covering Vercel Edge Functions, Supabase Edge Functions, Netlify Edge, and Deno Deploy for building globally distributed, low-latency serverless endpoints.

When to Use This Skill

Choose Edge Functions when:

  • Need sub-100ms response times globally without cold starts
  • Implementing authentication, authorization, or request transformation at the edge
  • Building personalization (geolocation, A/B testing) without origin round-trips
  • Creating lightweight API endpoints that don't need full Node.js runtime
  • Implementing feature flags, redirects, or content transformation

Consider alternatives when:

  • Need Node.js APIs (fs, child_process) — use standard serverless functions
  • Need long-running computation — use Lambda or containers
  • Need database connections — use standard functions with connection pooling

Quick Start

# Activate edge functions claude skill activate ultimate-edge-functions-engine # Build edge API claude "Create edge functions for authentication and geolocation-based routing"

Example: Vercel Edge Function

// app/api/hello/route.ts (Next.js App Router) export const runtime = 'edge'; export async function GET(request: Request) { const { geo, ip } = request as any; return Response.json({ message: 'Hello from the edge!', region: geo?.region || 'unknown', country: geo?.country || 'unknown', ip: ip || 'unknown', timestamp: Date.now(), }, { headers: { 'Cache-Control': 'public, s-maxage=60, stale-while-revalidate=30', }, }); } // Middleware (runs on every request at the edge) // middleware.ts import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; export function middleware(request: NextRequest) { const country = request.geo?.country || 'US'; const response = NextResponse.next(); // Add geolocation headers for downstream use response.headers.set('x-country', country); // Block restricted regions if (['CN', 'RU'].includes(country)) { return NextResponse.redirect(new URL('/blocked', request.url)); } // A/B testing via cookie if (!request.cookies.get('ab-variant')) { const variant = Math.random() > 0.5 ? 'A' : 'B'; response.cookies.set('ab-variant', variant, { maxAge: 86400 }); } return response; } export const config = { matcher: ['/api/:path*', '/dashboard/:path*'], };

Core Concepts

Edge Runtime Limitations

FeatureSupportedNot Supported
Web APIs (fetch, URL, crypto)Yes
Streaming responsesYes
WebSocket upgradePlatform-dependent
Node.js fs moduleNot available
Node.js child_processNot available
Native addonsNot available
Long execution (>30s)Limited CPU time
Large memory (>128MB)Constrained

Platform Comparison

PlatformRuntimeCold StartMax DurationRegions
Vercel EdgeV8<1ms30s30+
Cloudflare WorkersV8<1ms30s CPU300+
Supabase EdgeDeno~50ms150sLimited
Netlify EdgeDeno~10ms50s20+
Deno DeployDeno~10ms35+

Configuration

ParameterDescriptionDefault
runtimeEdge runtime selectionedge
regionsDeploy regions (or 'all')all
max_durationMaximum execution timePlatform default
env_varsEnvironment variables{}
matcherURL patterns to handle['/*']

Best Practices

  1. Keep edge functions lightweight and focused — Edge runtimes have limited CPU and memory. Handle request routing, auth checks, and header manipulation at the edge, then proxy heavy computation to origin servers.

  2. Use streaming responses for large payloads — Instead of buffering entire responses, use ReadableStream to stream data to clients. This reduces memory usage and improves time-to-first-byte.

  3. Leverage edge caching with proper Cache-Control headers — Set s-maxage for CDN caching and stale-while-revalidate for background revalidation. Edge-cached responses serve in <10ms globally.

  4. Use Web Crypto API instead of Node.js crypto — Edge runtimes support Web Crypto API natively. Use crypto.subtle for JWT verification, HMAC signing, and encryption operations.

  5. Test with platform-specific local development tools — Use vercel dev, wrangler dev, or supabase functions serve for accurate local testing that matches the edge runtime constraints.

Common Issues

Edge function can't connect to database directly. Most databases require persistent TCP connections unsupported at the edge. Use HTTP-based database proxies (Neon, PlanetScale, Supabase REST), edge-compatible ORMs (Drizzle with HTTP driver), or fetch data from your origin API.

Module import fails with "module not found" at the edge. Edge runtimes don't support all npm packages — only those compatible with Web API standards. Check package compatibility with the edge runtime and use edge-specific alternatives (e.g., jose instead of jsonwebtoken for JWT).

Response times increase under high concurrency. Edge functions share compute resources at each location. Reduce CPU usage per request, cache responses aggressively, and ensure functions complete quickly. If latency increases, check if your edge location is overloaded and consider spreading traffic across regions.

Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates