SvelteKit is the fastest-growing full-stack web framework — combining Svelte’s zero-runtime-overhead components with SvelteKit’s file-based routing, server-side rendering, and API routes. Deploying on a Hong Kong VPS with CN2 GIA routing means Svelte’s already-tiny bundles arrive at mainland Chinese users in milliseconds, while server-side data fetching runs co-located with your PostgreSQL database.
Step 1: Configure for Node.js Adapter
<code"># 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, // Gzip+Brotli static assets at build time
envPrefix: 'APP_', // Only expose APP_* env vars to client
}),
csrf: { checkOrigin: true },
},
}
Step 2: Server Load Functions and API Routes
<code"># src/routes/products/+page.server.ts — server-side data loading
import type { PageServerLoad } from './$types'
import { db } from '$lib/server/db'
import { error } from '@sveltejs/kit'
export const load: PageServerLoad = async ({ url, locals }) => {
const category = url.searchParams.get('category')
const page = Number(url.searchParams.get('page') ?? 1)
const products = await db.query(
`SELECT id, name, price, category, image_url
FROM products
WHERE active = true
AND ($1::text IS NULL OR category = $1)
ORDER BY created_at DESC
LIMIT 20 OFFSET $2`,
[category, (page - 1) * 20]
)
if (!products.rows.length && page > 1) {
throw error(404, 'Page not found')
}
return { products: products.rows, page, category }
}
# src/routes/products/+page.svelte — uses the data
<script lang="ts">
import type { PageData } from './$types'
export let data: PageData
</script>
<h1>Products</h1>
{#each data.products as product}
<div>
<img src={product.image_url} alt={product.name} loading="lazy" />
<h2>{product.name}</h2>
<p>HK${product.price}</p>
</div>
{/each}
# src/routes/api/products/+server.ts — REST API endpoint
import { json } from '@sveltejs/kit'
import type { RequestHandler } from './$types'
import { db } from '$lib/server/db'
export const GET: RequestHandler = async ({ url }) => {
const category = url.searchParams.get('category')
const result = await db.query(
'SELECT * FROM products WHERE active = true AND ($1 IS NULL OR category = $1)',
[category]
)
return json({ data: result.rows })
}
export const POST: RequestHandler = async ({ request, locals }) => {
if (!locals.user?.isAdmin) return json({ error: 'Unauthorized' }, { status: 401 })
const body = await request.json()
const { rows: [product] } = await db.query(
'INSERT INTO products (name, price, category) VALUES ($1, $2, $3) RETURNING *',
[body.name, body.price, body.category]
)
return json(product, { status: 201 })
}
Step 3: Database and Session Setup
<code"># src/lib/server/db.ts
import pg from 'pg'
const { Pool } = pg
export const db = new Pool({
connectionString: process.env.DATABASE_URL,
max: 10,
idleTimeoutMillis: 30000,
})
# src/hooks.server.ts — session middleware
import type { Handle } from '@sveltejs/kit'
import { db } from '$lib/server/db'
export const handle: Handle = async ({ event, resolve }) => {
const sessionId = event.cookies.get('session')
if (sessionId) {
const { rows: [session] } = await db.query(
'SELECT user_id, expires_at FROM sessions WHERE id = $1 AND expires_at > NOW()',
[sessionId]
)
if (session) {
const { rows: [user] } = await db.query(
'SELECT id, email, is_admin FROM users WHERE id = $1',
[session.user_id]
)
event.locals.user = user
}
}
return resolve(event)
}
Step 4: Build and Deploy
<code"># Build
npm ci && npm run build
# Creates: build/ directory (Node.js server)
# Transfer to VPS
rsync -az --delete build/ root@VPS_IP:/var/www/sveltekit/build/
rsync -az package.json root@VPS_IP:/var/www/sveltekit/
# Environment on VPS
cat > /var/www/sveltekit/.env << 'EOF' DATABASE_URL=postgresql://sk_user:Pass!@localhost/sk_db ORIGIN=https://yourdomain.com PORT=3000 HOST=127.0.0.1 EOF # PM2 cat > /var/www/sveltekit/ecosystem.config.cjs << 'EOF'
module.exports = {
apps: [{
name: 'sveltekit',
script: 'build/index.js',
cwd: '/var/www/sveltekit',
instances: 'max',
exec_mode: 'cluster',
env_production: { NODE_ENV: 'production' },
max_memory_restart: '512M',
}]
}
EOF
pm2 start /var/www/sveltekit/ecosystem.config.cjs --env production
pm2 save
Step 5: Nginx
<code">cat > /etc/nginx/sites-available/sveltekit << 'EOF'
upstream sk { server 127.0.0.1:3000; keepalive 32; }
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;
# Pre-compressed static assets from SvelteKit build
location /_app/ {
root /var/www/sveltekit/build/client;
gzip_static on;
brotli_static on;
expires 1y;
add_header Cache-Control "public, immutable";
}
location / {
proxy_pass http://sk;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
EOF
ln -s /etc/nginx/sites-available/sveltekit /etc/nginx/sites-enabled/
certbot --nginx -d yourdomain.com && nginx -t && systemctl reload nginx
SvelteKit vs Next.js: HK VPS Performance
| Metric | SvelteKit | Next.js 14 | Nuxt 3 |
|---|---|---|---|
| JS bundle size (typical) | ~15–40 KB | ~80–200 KB | ~60–150 KB |
| Cold start (PM2) | None (persistent) | None | None |
| Memory/instance | ~80–150 MB | ~120–250 MB | ~100–200 MB |
| SSR throughput (req/s) | 8,000–15,000 | 5,000–10,000 | 6,000–12,000 |
| Learning curve | Low (small API) | Medium | Medium |
Conclusion
SvelteKit on a Hong Kong VPS is one of the leanest full-stack web deployments available — tiny JS bundles load fast over any Asia-Pacific connection, server-side data loading keeps sensitive logic on the VPS, and PM2 cluster mode handles concurrent SSR requests across all vCPUs. For teams building consumer web apps targeting mainland China and Southeast Asia, SvelteKit’s minimal runtime overhead combined with CN2 GIA routing from a HK VPS delivers exceptionally fast first-load experiences.
Deploy SvelteKit today: Browse Server.HK Hong Kong VPS plans — a 2 GB plan handles SvelteKit with PostgreSQL for most production Svelte applications.