Vercel Edge Function Action
Battle-tested command for generate, optimized, vercel, edge. Includes structured workflows, validation checks, and reusable patterns for nextjs vercel.
Vercel Edge Function Action
Generate production-ready Vercel Edge Functions with geolocation, authentication, data transformation, proxy/caching patterns, TypeScript types, and comprehensive test suites.
When to Use This Command
Run this command when you need to create an Edge Function that runs at the CDN edge for low-latency, globally distributed processing.
- You need a geolocation-aware API that serves different content based on the visitor's country
- You want to implement JWT authentication at the edge before requests reach your origin
- You need a data transformation proxy that converts between JSON, XML, and CSV formats
- You want a caching proxy that reduces load on upstream APIs
Use it also when:
- You need Edge Functions with proper CORS headers, error handling, and cache controls
- You want auto-generated tests using mock NextRequest objects with geo data
Quick Start
# .claude/commands/vercel-edge-function-action.md name: vercel-edge-function-action description: Generate Vercel Edge Functions arguments: name: Function name
# Generate a geo-content edge function claude vercel-edge-function-action "geo-content"
Expected output:
Edge Function generated: geo-content
api/edge/geo-content/
index.ts (main handler with GET/POST)
types.ts (TypeScript interfaces)
utils.ts (helper functions)
config.ts (edge configuration)
__tests__/
geo-content.test.ts (unit tests)
Runtime: edge
Methods: GET, POST
Features: geolocation, caching, CORS
Core Concepts
| Concept | Description |
|---|---|
| Edge Runtime | V8-based runtime running at CDN nodes worldwide |
| Geolocation | Access to request.geo with country, city, and region data |
| Cache Control | s-maxage and stale-while-revalidate headers for CDN caching |
| Edge Middleware | Logic that runs before requests reach serverless functions |
| CORS Helper | Utility for consistent cross-origin response headers |
Edge Function Architecture:
Client āā> Nearest Edge Node āā> Edge Function
ā
āāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāā
v v v
Geo Lookup Auth Check Cache Check
ā ā ā
v v v
Localized Validated Cached or
Response Request Fresh Data
Configuration
| Parameter | Default | Description |
|---|---|---|
name | required | Function name (used for directory and file naming) |
type | generic | Template: geo, auth, transform, proxy, generic |
methods | GET,POST | HTTP methods to implement |
cache-time | 300 | Default s-maxage in seconds |
cors-origins | ["*"] | Allowed CORS origins |
Best Practices
-
Keep edge functions lightweight -- The Edge Runtime has a 128KB code size limit. Avoid importing large libraries; use lightweight alternatives or implement logic inline.
-
Set appropriate cache headers -- Use
s-maxagefor CDN caching andstale-while-revalidatefor background refresh. This dramatically reduces edge function invocations. -
Handle missing geo data gracefully -- Not all requests include geolocation data. Always provide fallback values:
const country = request.geo?.country || 'US'. -
Use jose instead of jsonwebtoken at the edge -- The
jsonwebtokenlibrary is not compatible with the Edge Runtime. Usejosefor JWT operations. -
Test with mocked geo data -- Create mock NextRequest objects with custom
geoproperties to test geolocation logic without deploying to the edge.
Common Issues
-
Module not supported in Edge Runtime -- Node.js built-in modules (
fs,path,crypto) are not available at the edge. Use Web APIs or edge-compatible alternatives. -
Function exceeds size limit -- The Edge Runtime enforces strict code size limits. Tree-shake imports and remove unused dependencies to stay within bounds.
-
Geo data undefined in local development --
request.geois only populated in production on Vercel. Use environment checks and mock data for local testing.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Git Commit Message Generator
Generates well-structured conventional commit messages by analyzing staged changes. Follows Conventional Commits spec with scope detection.
React Component Scaffolder
Scaffolds a complete React component with TypeScript types, Tailwind styles, Storybook stories, and unit tests. Follows project conventions automatically.
CI/CD Pipeline Generator
Generates GitHub Actions workflows for CI/CD including linting, testing, building, and deploying. Detects project stack automatically.