This is a starter template that combines Svelte 5 (with Runes) for the frontend with Hono running on Cloudflare Workers for the backend API. The entire application is served from a single Cloudflare Worker, providing a modern, unified full-stack development experience.
Clone this repository
git clone https://github.com/dhairya137/svelte-hono-cf-workers-template.git my-app
cd my-app
Install dependencies
npm install
Start development server
npm run dev
Build for production
npm run build
Deploy to Cloudflare Workers
npm run deploy
āāā src/ # Source code
ā āāā App.svelte # Main Svelte component
ā āāā main.ts # Svelte entry point
ā āāā app.css # Global CSS
ā āāā worker.ts # Worker entry point
ā āāā workers/ # Worker code
ā āāā index.ts # Main worker (serves frontend + API)
ā āāā api.ts # API routes using Hono
āāā public/ # Static assets
āāā index.html # HTML entry point
āāā svelte.config.js # Svelte configuration
āāā vite.config.ts # Vite configuration
āāā wrangler.toml # Cloudflare Workers configuration
The Svelte 5 app starts in src/main.ts
, which mounts the main App.svelte
component. The app uses Svelte 5's Runes for state management ($state
, etc.) and modern event handling attributes (onclick
).
// Example from src/main.ts
import { mount } from 'svelte';
const app = mount(App, {
target: document.getElementById('app'),
props: {}
});
<!-- Example from App.svelte -->
<script lang="ts">
let count = $state(0);
function increment() {
count++;
}
</script>
<button onclick={increment}>Count: {count}</button>
The API is built with Hono in src/workers/api.ts
. Hono provides a modern, Express-like API for creating routes.
// Example from src/workers/api.ts
import { Hono } from 'hono';
const api = new Hono();
api.get('/hello', (c) => {
return c.json({
message: 'Hello from Hono!',
timestamp: new Date().toISOString()
});
});
export const apiRoutes = api;
Both the frontend and backend are served from a single Cloudflare Worker, configured in src/workers/index.ts
. The worker serves the static Svelte app and routes API requests to Hono.
// Example from src/workers/index.ts
import { Hono } from 'hono';
import { apiRoutes } from './api';
const app = new Hono();
// Mount API routes
app.route('/api', apiRoutes);
// Serve static files
app.get('*', async (c) => {
// ... serve static files or fallback to index.html
});
export default app;
The template includes example API endpoints:
GET /api/hello
- Returns a greeting messageGET /api/users/:id
- Returns user data for a given IDAdd new routes in src/workers/api.ts
:
api.get('/new-endpoint', (c) => {
return c.json({
message: 'This is a new endpoint',
data: {
/* your data */
}
});
});
Create new Svelte components in the src
directory and import them in App.svelte
.
Add environment variables in wrangler.toml
:
[vars]
MY_VARIABLE = "my-value"
This template is configured for deployment to Cloudflare Workers:
Configure your Cloudflare account in Wrangler
npx wrangler login
Deploy your application
npm run deploy
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.