npm create svelte@latest edge-demo
cd edge-demo
npm install
Project Setup
Choose Skeleton project, TypeScript: No, ESLint: Yes, Prettier: Yes, Playwright: No, Vercel: Yes (for auto-adapter).
Install Vercel edge adapter and additional deps:
npm i -D @sveltejs/adapter-vercel
npm i @vercel/edge
Key Files
svelte.config.js
import adapter from '@sveltejs/adapter-vercel';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter({
// Render all pages/routes on the edge for global low-latency
runtime: 'edge'
})
},
preprocess: vitePreprocess()
};
export default config;
src/routes/+layout.svelte
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width" />
<title>Edge Demo - SvelteKit + Vercel Edge</title>
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="padding: 1rem; max-width: 1024px; margin: 0 auto;">
<slot />
</div>
%sveltekit.body%
</body>
</html>
src/routes/+page.svelte
<script>
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { onMount } from 'svelte';
let edgeData = $state({});
let loading = $state(false);
let ip = $state('');
async function fetchEdge() {
loading = true;
try {
const res = await fetch('/api/edge');
edgeData = await res.json();
} catch (e) {
console.error(e);
}
loading = false;
}
onMount(async () => {
// Get client IP from edge headers
const res = await fetch('/api/ip');
const data = await res.json();
ip = data.ip;
fetchEdge();
});
</script>
<h1>🦀 SvelteKit + Vercel Edge Functions</h1>
<p>
This app runs entirely on the <strong>edge</strong> for instant global deployments.
No cold starts, low latency everywhere.
</p>
<div style="background: #f0f9ff; padding: 1.5rem; border-radius: 0.5rem; border-left: 4px solid #0ea5e9;">
<h2>🌍 Your Edge Data</h2>
{#if loading}
<p>Loading from edge...</p>
{:else}
{#if edgeData.region}
<ul>
<li><strong>Edge Region:</strong> {edgeData.region} ({edgeData.city})</li>
<li><strong>Edge City:</strong> {edgeData.city}</li>
<li><strong>Edge Time:</strong> {new Date(edgeData.timestamp).toLocaleString()}</li>
<li><strong>Your IP:</strong> {ip}</li>
<li><strong>Headers Seen:</strong> {Object.keys(edgeData.headers || {}).length} custom headers</li>
</ul>
{:else}
<p>Click below to fetch from edge!</p>
{/if}
{/if}
</div>
<button
on:click={fetchEdge}
disabled={loading}
style="
background: #0ea5e9;
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 0.375rem;
font-size: 1rem;
cursor: pointer;
margin-top: 1rem;
"
>
{loading ? 'Fetching...' : 'Ping Edge API'}
</button>
<hr style="margin: 2rem 0;" />
<h2>🚀 Features Demo</h2>
<ul>
<li>✅ SSR on Edge (no cold starts)</li>
<li>✅ API Routes as Edge Functions</li>
<li>✅ Global CDN Deployment (Vercel Edge Network)</li>
<li>✅ Instant updates on git push</li>
</ul>
<p style="margin-top: 2rem; font-size: 0.875rem; color: #64748b;">
Test globally: Visit from different countries. Edge function runs nearest to you!
</p>
src/routes/api/edge/+server.js
// Edge-compatible API route (runs on Vercel Edge Runtime)
export async function GET({ request, getHeader, platform }) {
// Access edge-specific features
const headers = Object.fromEntries(request.headers.entries());
// Vercel Edge headers
const region = getHeader('x-vercel-ip-region') || 'Unknown';
const city = getHeader('x-vercel-ip-city') || 'Unknown';
const country = getHeader('x-vercel-ip-country') || 'Unknown';
const timestamp = new Date().toISOString();
return new Response(
JSON.stringify({
region,
city,
country,
timestamp,
headers: {
'user-agent': headers['user-agent'],
'accept-language': headers['accept-language']
}
}),
{
headers: {
'Content-Type': 'application/json',
'Cache-Control': 's-maxage=10, stale-while-revalidate' // Edge caching
}
}
);
}
src/routes/api/ip/+server.js
export async function GET({ request }) {
const headers = Object.fromEntries(request.headers.entries());
const ip = headers['x-forwarded-for'] || headers['cf-connecting-ip'] || 'Unknown';
return new Response(JSON.stringify({ ip }), {
headers: { 'Content-Type': 'application/json' }
});
}
vite.config.js (optional, for edge compat)
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()]
});
package.json (key scripts)
{
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check",
"check:watch": "svelte-kit sync && svelte-check --watch",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
}
}
Run Locally
npm run dev
Visit http://localhost:5173. Click "Ping Edge API" – it simulates edge with local headers.
Deploy for Instant Global Edge
-
Push to GitHub:
git init
git add .
git commit -m "Initial edge demo"
git branch -M main
git remote add origin <your-repo>
git push -u origin main
-
Connect to Vercel:
- Go to vercel.com, sign in.
- Import your GitHub repo.
- Framework Preset: SvelteKit.
- Root:
./ | Build: npm run build.
- Deploy! 🚀
-
Instant Global: Every git push auto-deploys to 100+ edge locations. Test from VPNs – zero cold starts, <50ms latency worldwide.
Why This Shows "Instant Global Deployments"?
- Edge Runtime: All SSR/API run on Vercel's global edge (no Node.js servers).
- Zero Config: Adapter-auto deploys to nearest edge PoP.
- Live Updates: Git push → deploy in seconds → instant everywhere.
- Demo Proof: Edge headers show your nearest region/city/time. Refresh in Tokyo, NY, London – different edges!
Scale it: Add DB (Vercel Postgres/KV), auth (Lucia), etc. – all edge-compatible.
Source: GitHub Template (fork this!) | Vercel Docs