Documentation Frameworks and migration

Deploy a SvelteKit app

adapter-node from source with no Dockerfile, runtime env vars, secrets and PostgreSQL.

~2 min read
On this page

Gregale deploys a SvelteKit app from its source as a Node app: it installs dependencies with the package manager your lockfile names, runs your build script (vite build), and starts the server that @sveltejs/adapter-node builds into build/. No Dockerfile is needed. What every framework shares is on Deploy a web framework.

The app runs as a server, so server load functions, form actions, +server.ts endpoints and server-side rendering all work.

Prepare the app

Use adapter-node. adapter-auto doesn't know Gregale and won't produce a server:

bash
npm uninstall @sveltejs/adapter-auto
npm install -D @sveltejs/adapter-node

Import it where your project configures SvelteKit (vite.config.ts in SvelteKit 3, svelte.config.js before that):

ts
import adapter from '@sveltejs/adapter-node';
// ...
sveltekit({ adapter: adapter() })

Add a start script that runs the built server:

json
{
  "scripts": {
    "build": "vite build",
    "start": "node build"
  }
}

The server reads PORT and listens on every interface (HOST defaults to 0.0.0.0), so it needs no flags. vite dev and vite preview are development servers; Gregale warns about a start script that runs them.

Add a health endpoint that doesn't touch the database, src/routes/healthz/+server.ts:

ts
import { json } from '@sveltejs/kit';

export const GET = () => json({ ok: true });
yaml
# gregale.yaml
hosting:
  health: /healthz

Deploy

bash
gregale deploy --dry-run
gregale deploy --name my-app

If the deployment fails:

bash
gregale app health my-app --errors
gregale logs my-app

Configuration and secrets

bash
gregale env set --app my-app API_KEY="$API_KEY"

Values reach the server when it starts. Read them with SvelteKit's runtime (dynamic) private module: in SvelteKit 3, declare them in src/env.ts with defineEnvVars and import them from $app/env/private; before SvelteKit 3, import from $env/dynamic/private. Static imports ($env/static/private) are fixed at build time, and Gregale builds without your secrets. See Secrets.

Add PostgreSQL

bash
gregale add postgres --app my-app

The connection URL arrives as DATABASE_URL. With postgres.js, open the pool on first use so the server starts and answers /healthz without the database:

ts
import postgres from 'postgres';
import { DATABASE_URL } from '$app/env/private';

let sql: postgres.Sql | undefined;

export function db() {
	sql ??= postgres(DATABASE_URL, { max: 5, prepare: false });
	return sql;
}

Keep this in a server-only module (src/lib/server/). DATABASE_URL goes through a connection pooler in transaction mode; see Managed PostgreSQL.

Run migrations as a release command with their own credential:

bash
gregale postgres attach my-app-postgres my-app --access migration --env MIGRATION_DATABASE_URL
yaml
# gregale.yaml
release:
  command: npm run migrate

The release command receives MIGRATION_DATABASE_URL; serving instances never do. If it fails, the previous version keeps serving.

Things to know

  • Prerendered pages are written into build/ and served by the same server. A fully static site can also deploy this way.
  • Timers and background work stop while the app is parked. Run scheduled work as a cron or a job.
  • Files. The filesystem is ephemeral. Keep uploads in object storage.

Example repository

gregale-dev/sveltekit-postgres is a SvelteKit 3 notes app on this page's setup: a server-rendered page with a form action, a JSON API, postgres.js, and migrations as the release command. Its gregale.yaml creates the database, so one command deploys both:

bash
git clone https://github.com/gregale-dev/sveltekit-postgres
cd sveltekit-postgres
gregale deploy --name my-sveltekit-app

Spotted a problem on this page? Tell us