This example demonstrates how to deploy a srvx server as a Vercel Edge Function using vite-plugin-srvx.
- ⚡ Vite for lightning-fast development
- 🌐 srvx Universal Server with Web Standard APIs
- ▲ Vercel Edge Functions for global edge deployment
- 🎨 Interactive frontend with API testing
- 🔧 Zero-config deployment setup
pnpm installpnpm devVisit http://localhost:5173 to see the app in development mode.
pnpm buildThis creates:
dist/
├── api/
│ └── index.js # Vercel Edge Function
└── public/ # Static assets
├── index.html
└── assets/
pnpm previewOr manually:
vercel dev# Install Vercel CLI if you haven't
npm i -g vercel
# Deploy
vercel- Push this example to a GitHub repository
- Import the repository in Vercel Dashboard
- Vercel will automatically detect the configuration and deploy!
The vite.config.ts enables Vercel mode:
import { defineConfig } from "vite";
import srvx from "vite-plugin-srvx";
export default defineConfig({
plugins: [
...srvx({
entry: "./src/server.ts",
framework: "vercel", // Enable Vercel Edge Functions
}),
],
});The vercel.json tells Vercel where to find the Edge Function:
{
"buildCommand": "pnpm build",
"outputDirectory": "dist/public",
"functions": {
"dist/api/**": {
"runtime": "edge"
}
}
}Your srvx server (src/server.ts) uses Web Standard APIs:
export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/hello") {
return Response.json({ message: "Hello!" });
}
return new Response("Not Found", { status: 404 });
},
};The example includes several API endpoints:
GET /api/hello- Returns a greeting messageGET /api/time- Returns current timeGET /api/user-agent- Returns user agent and headers
Deploying to Vercel Edge Functions provides:
- ⚡ Ultra-low latency - Runs on Vercel's global edge network
- 🌍 Global reach - Automatically deployed to 100+ edge locations
- 📈 Auto-scaling - Scales automatically based on demand
- 💰 Cost-effective - Pay only for what you use
- 🔒 Secure - Runs in isolated V8 environments
examples/vercel/
├── frontend/
│ ├── main.ts # Frontend TypeScript
│ └── style.css # Styles
├── src/
│ └── server.ts # srvx server (Edge Function)
├── index.html # Entry HTML
├── package.json # Dependencies
├── tsconfig.json # TypeScript config
├── vite.config.ts # Vite + srvx config
└── vercel.json # Vercel deployment config
To use environment variables in your Edge Function:
- Add them in Vercel Dashboard → Project Settings → Environment Variables
- Access them in your server code:
export default {
async fetch(request: Request) {
const apiKey = process.env.API_KEY;
// Use the API key...
},
};Vercel Edge Functions have some constraints:
- Max Duration: 25 seconds (must begin sending response)
- Streaming: Up to 300 seconds
- Code Size: 1 MB (Hobby), 2 MB (Pro), 4 MB (Enterprise) after gzip
- No Node.js APIs: Only Web Standard APIs (which srvx uses!)
Make sure all dependencies are installed:
pnpm installVerify your vercel.json has the correct function configuration:
{
"functions": {
"dist/api/**": {
"runtime": "edge"
}
}
}Check that your outputDirectory points to dist/public in vercel.json.
- Vercel Edge Functions Documentation
- srvx Documentation
- vite-plugin-srvx Documentation
- Vercel Deployment Guide
MIT