SvelteKit is a full-stack web framework built on Svelte — compiling components to vanilla JavaScript with no virtual DOM overhead, delivering some of the smallest client bundles in the ecosystem. Self-hosting SvelteKit on a Hong Kong VPS with CN2 GIA routing gives mainland Chinese users server-rendered pages at 30–60ms TTFB with no Vercel or Netlify cold-start latency — just a persistent Node.js process behind Nginx.
Why Self-Host SvelteKit Instead of Vercel?
- China performance — Vercel’s US/EU edge has 200–400ms TTFB from China; your HK VPS via CN2 GIA delivers 35–70ms
- No cold starts — serverless platforms have function cold-start delays; a persistent Node.js process on your VPS responds instantly
- Flat cost — Vercel’s Pro plan + function invocations scales with traffic; VPS cost is fixed
- Database colocation — PostgreSQL on the same VPS means sub-1ms query latency from SvelteKit load functions
Step 1: Configure SvelteKit for Node.js
<code"># Install the Node.js adapter
npm install -D @sveltejs/adapter-node
# svelte.config.js
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
export default {
preprocess: vitePreprocess(),
kit: {
adapter: adapter({
out: 'build', // Output directory
precompress: true, // Pre-compress assets with gzip/brotli
}),
csrf: {
checkOrigin: true, // CSRF protection for form actions
},
},
};
Step 2: Environment and Build
<code"># .env.production (on VPS — never commit to git) DATABASE_URL=postgresql://svelte_user:Pass!@localhost/svelte_db SECRET_KEY=RANDOM_32_CHAR_STRING_HERE ORIGIN=https://yourdomain.com PORT=3000 HOST=127.0.0.1 # Build npm ci npm run build # The build/ directory contains: # build/index.js ← entry point # build/client/ ← static assets # build/server/ ← server-side code
Step 3: Deploy to VPS
<code">useradd -m -s /bin/bash svelte mkdir -p /var/www/svelte-app # Transfer build artefact + package files rsync -az build/ root@VPS_IP:/var/www/svelte-app/build/ rsync -az package.json package-lock.json root@VPS_IP:/var/www/svelte-app/ ssh root@VPS_IP "cd /var/www/svelte-app && npm ci --production" # Environment file cat > /var/www/svelte-app/.env << 'EOF' DATABASE_URL=postgresql://svelte_user:Pass!@localhost/svelte_db SECRET_KEY=YOUR_SECRET ORIGIN=https://yourdomain.com PORT=3000 HOST=127.0.0.1 EOF chown -R svelte:svelte /var/www/svelte-app
PM2 Service
<code">cat > /var/www/svelte-app/ecosystem.config.cjs << 'EOF'
module.exports = {
apps: [{
name: 'sveltekit',
script: 'build/index.js',
cwd: '/var/www/svelte-app',
instances: 'max',
exec_mode: 'cluster',
env_production: {
NODE_ENV: 'production',
PORT: 3000,
HOST: '127.0.0.1',
},
max_memory_restart: '512M',
error_file: '/var/log/sveltekit/error.log',
out_file: '/var/log/sveltekit/out.log',
}]
};
EOF
mkdir -p /var/log/sveltekit
chown svelte:svelte /var/log/sveltekit
pm2 start /var/www/svelte-app/ecosystem.config.cjs --env production
pm2 save
pm2 startup systemd -u svelte --hp /home/svelte
Step 4: Nginx Reverse Proxy
<code">cat > /etc/nginx/sites-available/sveltekit << 'EOF'
upstream sveltekit {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Serve pre-compressed SvelteKit static assets
location /_app/immutable/ {
alias /var/www/svelte-app/build/client/_app/immutable/;
expires 1y;
add_header Cache-Control "public, immutable";
gzip_static on;
brotli_static on;
access_log off;
}
# All other requests to SvelteKit
location / {
proxy_pass http://sveltekit;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
EOF
ln -s /etc/nginx/sites-available/sveltekit /etc/nginx/sites-enabled/
certbot --nginx -d yourdomain.com
nginx -t && systemctl reload nginx
Step 5: SvelteKit Load Functions with PostgreSQL
<code"># src/lib/server/db.ts — database connection
import postgres from 'postgres';
const sql = postgres(process.env.DATABASE_URL!, {
max: 10, // Connection pool size
idle_timeout: 30,
connect_timeout: 10,
});
export { sql };
# src/routes/products/+page.server.ts — server-side data loading
import type { PageServerLoad } from './$types';
import { sql } from '$lib/server/db';
export const load: PageServerLoad = async ({ url }) => {
const category = url.searchParams.get('category') ?? 'all';
// Runs on the VPS — direct DB query, no API overhead
const products = await sql`
SELECT id, name, price, stock, category
FROM products
WHERE (${ category } = 'all' OR category = ${ category })
AND active = true
ORDER BY created_at DESC
LIMIT 24
`;
return { products, category };
};
# src/routes/products/+page.svelte — client component
-
- {#each data.products as product (product.id)}
- {product.name} HK${product.price}
{/each}
Step 6: Form Actions (Server-Side Mutations)
<code"># src/routes/cart/+page.server.ts
import type { Actions } from './$types';
import { fail, redirect } from '@sveltejs/kit';
import { sql } from '$lib/server/db';
export const actions: Actions = {
addToCart: async ({ request, locals }) => {
const data = await request.formData();
const productId = data.get('productId');
if (!productId) return fail(400, { error: 'Missing product ID' });
if (!locals.user) throw redirect(303, '/login');
await sql`
INSERT INTO cart_items (user_id, product_id, quantity)
VALUES (${locals.user.id}, ${productId}, 1)
ON CONFLICT (user_id, product_id)
DO UPDATE SET quantity = cart_items.quantity + 1
`;
return { success: true };
}
};
# src/routes/cart/+page.svelte
Zero-Downtime Deployment
<code"># deploy.sh — run from local machine or CI/CD #!/bin/bash set -e VPS=root@YOUR_VPS_IP APP=/var/www/svelte-app npm run build rsync -az --delete build/ $VPS:$APP/build/ rsync -az package.json package-lock.json $VPS:$APP/ ssh $VPS "cd $APP && npm ci --production" ssh $VPS "pm2 reload sveltekit --update-env" ssh $VPS "sleep 3 && curl -sf http://127.0.0.1:3000/ && echo 'OK'" echo "Deployed"
Performance: SvelteKit on HK VPS vs Vercel
| Metric | Vercel (US edge) | HK VPS CN2 GIA |
|---|---|---|
| TTFB from Shanghai (SSR) | 200–380ms | 35–70ms |
| Bundle size (typical app) | ~40 KB JS | ~40 KB JS (same) |
| Cold start | 150–500ms | None (persistent process) |
| DB query latency | External DB: 20–100ms | Local DB: 0.5–2ms |
Conclusion
SvelteKit on a Hong Kong VPS delivers some of the best full-stack performance available to Asia-Pacific users — Svelte’s compiler produces minimal JavaScript bundles, the Node.js adapter serves SSR pages from a persistent process, and CN2 GIA routing brings mainland Chinese users to sub-70ms TTFB. Combined with a collocated PostgreSQL database and PM2 cluster mode, the setup handles high-concurrency workloads at a predictable flat cost.
Deploy your SvelteKit app: Browse Server.HK Hong Kong VPS plans — a 2 GB plan handles SvelteKit with PM2 cluster and PostgreSQL for most production applications.