"Step-by-step guide to deploying your SvelteKit frontend to Vercel. Free SSL, automatic deployments, and global CDN included."
Deploying SvelteKit to Vercel
Vercel is the optimal platform for deploying SvelteKit applications. Created by the same team behind Svelte, Vercel offers first-class support with automatic deployments, global CDN, and serverless functions.
Why Vercel?
- Native SvelteKit support: Zero configuration required
- Automatic deployments: Deploy on every git push
- Preview deployments: Test changes with unique URLs
- Global edge network: Fast loading worldwide
- Serverless functions: API routes work out of the box
- Free SSL: Automatic HTTPS certificates
Prerequisites
Before starting:
- Git repository with your SvelteKit code
- Vercel account (free tier available)
- Node.js 20+ installed locally
Install Development Tools
We recommend using mise (version manager) and pnpm (package manager) instead of npm:
Install mise (version manager for Node.js):
# macOS/Linux
curl https://mise.run | sh
# Add to your shell (follow the post-install instructions)
echo 'eval "$(mise activate)"' >> ~/.bashrc
# or for zsh
echo 'eval "$(mise activate zsh)"' >> ~/.zshrcInstall Node.js via mise:
mise use node@20Install pnpm:
curl -fsSL https://get.pnpm.io/install.sh | sh -Why pnpm instead of npm?
- Faster: Uses content-addressable storage, shared across projects
- Disk efficient: Packages are stored once, hard-linked into projects
- Strict: Prevents accidental access to undeclared dependencies
- Reliable: Lockfile format is more deterministic than npm
Step 1: Prepare Your Project
Configure Adapter
Ensure your svelte.config.js uses the Vercel adapter:
import adapter from '@sveltejs/adapter-vercel';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter({
// Optional: specify runtime
runtime: 'nodejs18.x'
})
}
};
export default config;Install the adapter if needed:
cd frontend
pnpm install -D @sveltejs/adapter-vercelEnvironment Variables
Create frontend/.env.production:
PUBLIC_API_URL=https://tcs-svc-9k2m.yourdomain.com
PUBLIC_GRAPHQL_ENDPOINT=https://tcs-svc-9k2m.yourdomain.com/graphql
PUBLIC_APP_URL=https://your-app.vercel.appImportant: Do not commit secrets. Use Vercel's dashboard for sensitive values.
Security Note: The backend URL uses an obscure subdomain (tcs-svc-9k2m) instead of predictable names like api or backend. This hides your API from automated subdomain scanners and reduces attack surface. Always choose unguessable subdomain names for your backend services.
Build Configuration
Ensure your vite.config.ts is compatible:
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()],
build: {
// Ensure source maps for debugging
sourcemap: true
}
});Step 2: Install Vercel CLI
pnpm add -g vercelStep 3: Deploy
First Deployment
- Login to Vercel:
vercel login- Deploy from your project directory:
cd frontend
vercelAnswer the prompts:
- "Set up and deploy?" → Yes
- "Which scope?" → Your account
- "Link to existing project?" → No (first time)
- "What's your project name?" → your-app-name
Wait for deployment to complete:
🔍 Inspect: https://vercel.com/yourname/your-app/abc123
✅ Production: https://your-app.vercel.appSubsequent Deployments
After initial setup, just push to git:
git push origin mainVercel automatically deploys from your connected git repository.
Step 4: Connect Git Repository
For automatic deployments:
- Go to vercel.com/dashboard
- Select your project
- Click "Settings" → "Git"
- Ensure your repository is connected
- Configure production branch (usually
main)
Step 5: Configure Environment Variables
Via Dashboard
- Go to Project Settings → Environment Variables
- Add variables:
| Name | Value | Environment |
|---|---|---|
PUBLIC_API_URL |
https://tcs-svc-9k2m.yourapp.com |
Production |
PUBLIC_API_URL |
https://tcs-stg-4m7k.yourapp.com |
Preview |
Via CLI
vercel env add PUBLIC_API_URL
# Enter value when prompted
# Select environment (Production, Preview, Development)Step 6: Custom Domain
Add Domain
- Project Settings → Domains
- Enter your domain:
yourapp.com - Follow DNS configuration instructions
Configure DNS
Vercel provides DNS records to add:
Option A: A Record
Type: A
Name: @
Value: 76.76.21.21Option B: CNAME (for www)
Type: CNAME
Name: www
Value: cname.vercel-dns.comVerify DNS
DNS propagation takes time. Check status in Vercel dashboard.
Step 7: Configure Build Settings
Build Command
Usually auto-detected, but can be customized:
Project Settings → Build & Development Settings:
- Framework Preset: SvelteKit
- Build Command:
pnpm run build - Output Directory:
.svelte-kit/output(auto-detected) - Install Command:
pnpm install
Root Directory
If your frontend is in a subdirectory:
Project Settings → General → Root Directory:
frontendAdvanced Configuration
vercel.json
Create frontend/vercel.json for advanced options:
{
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-Frame-Options",
"value": "DENY"
}
]
}
],
"redirects": [
{
"source": "/old-path",
"destination": "/new-path",
"permanent": true
}
]
}Serverless Function Configuration
For API routes, configure function size:
{
"functions": {
"src/routes/api/**/*.ts": {
"maxDuration": 30
}
}
}Troubleshooting
Build Failures
Error: "Cannot find module"
Solution:
# Delete node_modules and lock file
rm -rf node_modules pnpm-lock.yaml
pnpm install
# Commit lock file
git add pnpm-lock.yaml
git commit -m "fix: regenerate lock file"
git pushEnvironment Variables Not Available
Ensure variables:
- Are prefixed with
PUBLIC_if used in browser - Are added to correct environment (Production/Preview)
- Are redeployed after adding
API Routes Not Working
Check adapter configuration:
// svelte.config.js
import adapter from '@sveltejs/adapter-vercel';
export default {
kit: {
adapter: adapter({
// Ensure this is set
runtime: 'nodejs18.x'
})
}
};404 on Dynamic Routes
Add vercel.json:
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/"
}
]
}Production Checklist
Before announcing your deployment:
- Environment variables set for production
- Custom domain configured (if applicable)
- SSL certificate active
- Build succeeds without warnings
- API endpoints connect to backend
- Authentication flows work
- Error tracking configured
- Analytics configured (optional)
Next Steps
Your frontend is deployed. Now deploy your backend:
Or learn about: