Svelte and SvelteKit course ยท Module 14: Hooks, API and Deployment
Deployment
In this lesson5
Adapters
SvelteKit uses adapters for deployment. An adapter turns the built app into the format of a specific platform. In SvelteKit 3 you set it in vite.config.js, in the options of the sveltekit() plugin - the svelte.config.js file is no longer supported:
// vite.config.js
import adapter from '@sveltejs/adapter-auto';
// or:
// import adapter from '@sveltejs/adapter-node';
// import adapter from '@sveltejs/adapter-vercel';
// import adapter from '@sveltejs/adapter-cloudflare';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter()
})
]
});Popular platforms
Vercel
npm i -D @sveltejs/adapter-vercelNetlify
npm i -D @sveltejs/adapter-netlifyNode.js
npm i -D @sveltejs/adapter-nodeEnvironment variables
First you declare the variables in the src/env.js file. Every variable is private (server only) until you mark it as public: true:
// src/env.js
import { defineEnvVars } from '@sveltejs/kit/env';
export const variables = defineEnvVars({
DATABASE_URL: {},
API_KEY: {},
APP_NAME: { public: true }
});You keep the values in the .env file (do not commit it to the repository):
# .env
DATABASE_URL=postgresql://...
API_KEY=secret
APP_NAME=Viking ChroniclesAnd in code you import them from the $app/env/private and $app/env/public modules:
// In code (server only)
import { DATABASE_URL, API_KEY } from '$app/env/private';
// Public (client too)
import { APP_NAME } from '$app/env/public';The $env/static/private module and its siblings from SvelteKit 2 are deprecated. By default variables are read while the server runs; the static: true option writes the value into the code at build time.
Build
# Build
npm run build
# Preview locally
npm run preview
# Start (for adapter-node)
node buildSvelteKit 3 needs Node.js 22.17 or newer, so check the Node version on your server before you set sail.
Production configuration
// vite.config.js
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter(),
csrf: {
// CSRF protection is always on - here you list trusted domains
trustedOrigins: ['https://jarl.example.com']
},
csp: {
mode: 'auto',
directives: {
'script-src': ['self']
}
}
})
]
});The csrf.checkOrigin option is gone in SvelteKit 3: CSRF protection can no longer be turned off, you can only name the domains you trust.
SKAL! You have completed the great journey through the land of Svelte!
Code for this lesson: App.svelte
<script>
// Deployment - adapters and configuration
let selectedAdapter = $state('auto');
const adapters = [
{
id: 'auto',
name: 'adapter-auto',
desc: 'Automatically selects adapter',
install: 'npm i -D @sveltejs/adapter-auto',
platforms: ['Vercel', 'Netlify', 'Cloudflare']
},
{
id: 'node',
name: 'adapter-node',
desc: 'Traditional Node.js server',
install: 'npm i -D @sveltejs/adapter-node',
platforms: ['VPS', 'Docker', 'Railway']
},
{
id: 'static',
name: 'adapter-static',
desc: 'Full SSG (static files)',
install: 'npm i -D @sveltejs/adapter-static',
platforms: ['GitHub Pages', 'Netlify', 'Any CDN']
},
{
id: 'vercel',
name: 'adapter-vercel',
desc: 'Optimized for Vercel',
install: 'npm i -D @sveltejs/adapter-vercel',
platforms: ['Vercel']
}
];
let currentAdapter = $derived(adapters.find(a => a.id === selectedAdapter));
</script>
<main class="deploy-demo">
<h1>Deployment</h1>
<section>
<h2>Choose adapter</h2>
<div class="adapters">
{#each adapters as adapter}
<button
class:active={selectedAdapter === adapter.id}
onclick={() => selectedAdapter = adapter.id}
>
{adapter.name}
</button>
{/each}
</div>
</section>
{#if currentAdapter}
<section class="adapter-info">
<h2>{currentAdapter.name}</h2>
<p>{currentAdapter.desc}</p>
<h3>Installation</h3>
<pre>{currentAdapter.install}</pre>
<h3>Platforms</h3>
<div class="platforms">
{#each currentAdapter.platforms as platform}
<span class="platform">{platform}</span>
{/each}
</div>
</section>
{/if}
<section class="config">
<h2>vite.config.js</h2>
<pre>
import adapter from '@sveltejs/adapter-{selectedAdapter}';
import {"{"} sveltekit {"}"} from '@sveltejs/kit/vite';
import {"{"} defineConfig {"}"} from 'vite';
export default defineConfig({"{"}
plugins: [
sveltekit({"{"}
adapter: adapter()
{"}"})
]
{"}"});
</pre>
</section>
<section class="commands">
<h2>Commands</h2>
<div class="command">
<code>npm run build</code>
<span>Build the application</span>
</div>
<div class="command">
<code>npm run preview</code>
<span>Preview locally</span>
</div>
<div class="command">
<code>node build</code>
<span>Start (adapter-node)</span>
</div>
</section>
</main>
<style>
.deploy-demo {
font-family: 'Georgia', serif;
max-width: 600px;
margin: 0 auto;
padding: 2rem;
background: #0a1628;
color: #b8d4f7;
min-height: 100vh;
}
h1 {
color: #4a90d9;
text-align: center;
}
section {
background: rgba(74, 144, 217, 0.1);
border: 1px solid #4a90d9;
border-radius: 8px;
padding: 1rem;
margin: 1rem 0;
}
h2 {
color: #4a90d9;
font-size: 1rem;
margin-top: 0;
}
h3 {
color: #7cb3f0;
font-size: 0.9rem;
margin: 1rem 0 0.5rem;
}
.adapters {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
button {
padding: 0.5rem 1rem;
background: #1e3a5f;
color: #b8d4f7;
border: 1px solid #4a90d9;
border-radius: 4px;
cursor: pointer;
}
button.active {
background: #4a90d9;
color: #0a1628;
}
pre {
background: rgba(0,0,0,0.5);
padding: 1rem;
border-radius: 4px;
font-size: 0.85rem;
overflow-x: auto;
}
.platforms {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.platform {
padding: 0.25rem 0.75rem;
background: rgba(144, 238, 144, 0.2);
border-radius: 20px;
font-size: 0.85rem;
color: #90ee90;
}
.command {
display: flex;
justify-content: space-between;
padding: 0.5rem;
background: rgba(0,0,0,0.3);
border-radius: 4px;
margin-bottom: 0.5rem;
}
code {
color: #ffd700;
font-family: monospace;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What are adapters in SvelteKit?