Kurs Svelte i SvelteKit · Moduł 14: Hooki, API i wdrożenie
Deployment
W tej lekcji5
Adapters
SvelteKit używa adapterów do deploymentu. Adapter przerabia zbudowaną aplikację na format konkretnej platformy. W SvelteKit 3 ustawiasz go w vite.config.js, w opcjach wtyczki sveltekit() - plik svelte.config.js nie jest już obsługiwany:
// vite.config.js
import adapter from '@sveltejs/adapter-auto';
// lub:
// 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()
})
]
});Popularne platformy
Vercel
npm i -D @sveltejs/adapter-vercelNetlify
npm i -D @sveltejs/adapter-netlifyNode.js
npm i -D @sveltejs/adapter-nodeEnvironment variables
Najpierw deklarujesz zmienne w pliku src/env.js. Każda zmienna jest prywatna (tylko serwer), dopóki nie oznaczysz jej jako public: true:
// src/env.js
import { defineEnvVars } from '@sveltejs/kit/env';
export const variables = defineEnvVars({
DATABASE_URL: {},
API_KEY: {},
APP_NAME: { public: true }
});Wartości trzymasz w pliku .env (nie wrzucaj go do repozytorium):
# .env
DATABASE_URL=postgresql://...
API_KEY=secret
APP_NAME=Kroniki WikingówA w kodzie importujesz je z modułów $app/env/private i $app/env/public:
// W kodzie (tylko server)
import { DATABASE_URL, API_KEY } from '$app/env/private';
// Publiczne (też klient)
import { APP_NAME } from '$app/env/public';Moduły $env/static/private i pokrewne ze SvelteKit 2 są przestarzałe. Domyślnie zmienne są odczytywane w czasie działania serwera; opcja static: true wpisuje wartość do kodu podczas buildu.
Build
# Build
npm run build
# Preview lokalnie
npm run preview
# Start (dla adapter-node)
node buildSvelteKit 3 wymaga Node.js w wersji co najmniej 22.17, więc sprawdź wersję Node na serwerze, zanim wypłyniesz.
Konfiguracja produkcyjna
// vite.config.js
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter(),
csrf: {
// Ochrona CSRF jest zawsze włączona - tu dopisujesz zaufane domeny
trustedOrigins: ['https://jarl.example.com']
},
csp: {
mode: 'auto',
directives: {
'script-src': ['self']
}
}
})
]
});Opcja csrf.checkOrigin zniknęła w SvelteKit 3: ochrony przed CSRF nie da się już wyłączyć, możesz tylko wskazać domeny, którym ufasz.
SKÅL! Ukończyłeś wielką podróż przez krainę Svelte!
Kod do tej lekcji: App.svelte
<script>
// Deployment - adaptery i konfiguracja
let selectedAdapter = $state('auto');
const adapters = [
{
id: 'auto',
name: 'adapter-auto',
desc: 'Automatycznie wybiera adapter',
install: 'npm i -D @sveltejs/adapter-auto',
platforms: ['Vercel', 'Netlify', 'Cloudflare']
},
{
id: 'node',
name: 'adapter-node',
desc: 'Tradycyjny serwer Node.js',
install: 'npm i -D @sveltejs/adapter-node',
platforms: ['VPS', 'Docker', 'Railway']
},
{
id: 'static',
name: 'adapter-static',
desc: 'Pełny SSG (statyczne pliki)',
install: 'npm i -D @sveltejs/adapter-static',
platforms: ['GitHub Pages', 'Netlify', 'Any CDN']
},
{
id: 'vercel',
name: 'adapter-vercel',
desc: 'Zoptymalizowany dla 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>Wybierz 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>Instalacja</h3>
<pre>{currentAdapter.install}</pre>
<h3>Platformy</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>Komendy</h2>
<div class="command">
<code>npm run build</code>
<span>Zbuduj aplikację</span>
</div>
<div class="command">
<code>npm run preview</code>
<span>Podgląd lokalnie</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>Hasła z dokumentacji w tej lekcji
Widzisz błąd w tej lekcji?
Sprawdź się
Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.
1. Co to są adaptery w SvelteKit?