ข้ามไปยังเนื้อหา

Build & Deploy ด้วย SvelteKit Adapters

ใน Next.js คุณควบคุมรูปแบบ output ด้วย output ใน next.config.js ('export' สำหรับ static, ค่าเริ่มต้นสำหรับ Node server) และเลือกแพลตฟอร์ม deployment โดยทำตามเอกสารของตัวเอง ส่วน SvelteKit ใช้แนวทางที่ชัดเจนกว่า: คุณติดตั้งแพ็กเกจ adapter แล้วอ้างอิง adapter นั้นใน svelte.config.js adapter เป็นตัวกำหนดว่า npm run build จะผลิตอะไรออกมา — เว็บไซต์ static, Node.js server, bundle ของ Vercel edge function และอื่น ๆ

next.config.js
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export', // static export
};
module.exports = nextConfig;
svelte.config.js
// svelte.config.js
import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
/** @type {import('@sveltejs/kit').Config} */
const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter({
fallback: '404.html',
}),
},
};
export default config;
Adapterตัวเทียบเท่ากรณีการใช้งาน
@sveltejs/adapter-staticNext.js output: 'export'static เต็มรูปแบบ — deploy ไปยัง CDN ใดก็ได้ (Netlify, GitHub Pages, S3)
@sveltejs/adapter-autoตรวจจับ Vercel, Netlify, Cloudflare โดยอัตโนมัติ
@sveltejs/adapter-nodeNext.js standalone serverNode.js server (Dockerfile, VPS, Railway)
@sveltejs/adapter-vercelการ deploy Next.js แบบเฉพาะ Vercelการ deploy ไปยัง Vercel แบบชัดเจนพร้อม config edge/serverless
Terminal window
# Build for production (output depends on adapter)
npm run build
# Preview the production build locally
npm run preview

คำสั่งเหล่านี้แมปตรง ๆ กับ next build และ next start / vite preview

โปรเจกต์ React/Vite:

name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run build
- run: npm test -- --watchAll=false

โปรเจกต์ SvelteKit:

name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run check
- run: npm run build
- run: npm run test

สิ่งที่เพิ่มเข้ามาที่สำคัญใน workflow ของ SvelteKit คือ npm run check (รัน svelte-check เพื่อจับ type error ก่อน build) และ npm run test (รัน Vitest)

Terminal window
# Static
npm install --save-dev @sveltejs/adapter-static
# Auto (recommended for most deployments)
npm install --save-dev @sveltejs/adapter-auto
# Node server
npm install --save-dev @sveltejs/adapter-node
# Vercel
npm install --save-dev @sveltejs/adapter-vercel
คุณตั้งค่า SvelteKit adapter ที่ไหน?
SvelteKit adapter ใดที่เทียบเท่า Next.js output: "export"?
workflow ของ SvelteKit เพิ่มขั้นตอน CI พิเศษอะไรเมื่อเทียบกับ workflow ของ React Vite?