Deploying a Svelte App
Svelte is a modern JavaScript framework for building fast, reactive user interfaces. Unlike traditional frameworks, Svelte compiles your code to highly efficient JavaScript at build time, resulting in smaller bundles and excellent performance.
This guide explains how to deploy a Svelte application to Klutch.sh, both with and without a Dockerfile. It covers installation, sample code, and deployment steps.
Prerequisites
- Node.js & npm installed (Download)
- Git and GitHub account
- Klutch.sh account
Getting Started: Create a Svelte App
- Create a new Svelte app using Vite (recommended):
Terminal window npm create vite@latest my-svelte-app -- --template sveltecd my-svelte-appnpm install - Start the development server:
Your app should be running at http://localhost:5173.
Terminal window npm run dev
Sample Code (src/App.svelte)
Replace the contents of src/App.svelte
with:
<script> // No script needed for this example</script>
<main> <h1>Hello from Svelte on Klutch.sh!</h1></main>
<style>h1 { color: #ff3e00; }</style>
Deploying Without a Dockerfile
Svelte apps built with Vite are static sites after build. Klutch.sh can deploy static sites directly.
- Push your Svelte app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project.
- Create a new app:
- Select your Svelte GitHub repository and branch
- Choose “Static Site” as the app type
- Set build command:
npm run build
- Set output directory:
dist
- Configure region, compute, and environment variables as needed
- Click “Create” to deploy. Klutch.sh will build and serve your static Svelte app.
Deploying With a Dockerfile
If you need custom server logic or want to use a Dockerfile:
- Add a
Dockerfile
to your project root. Example:# Build stageFROM node:20-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .RUN npm run build# Production stageFROM nginx:alpineCOPY --from=builder /app/dist /usr/share/nginx/htmlEXPOSE 80CMD ["nginx", "-g", "daemon off;"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, create a new app and select the Dockerfile option when prompted.
- Set service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and serve your Svelte app via Nginx.
Resources
Deploying Svelte to Klutch.sh is fast and flexible. Use static site deployment for most cases, or Dockerfile for custom setups.