Skip to content

What is ktor-svelte? ​

ktor-svelte turns a Ktor API and a SvelteKit frontend into one application. In production it is a single JAR on a single port. In development you open Ktor's port and get Vite's hot reload.

Ktor plugin Gradle Generated client

It comes in parts that work together:

PartArtifactWhat it does
Ktor pluginktor-svelte-serverinstall(Svelte) serves the frontend next to your API, adds the security and caching defaults, and writes the TypeScript client
Gradle pluginio.github.nijasc.ktor-svelteBuilds the frontend into the JAR, runs the dev loop, sets up frontend libraries, and checks that generated files are current
Generated clientfrontend/src/lib/apiTypes, services and validation helpers for every Ktor route, imported as #api, with no npm dependencies
Test kitktor-svelte-testKotlin tests that drive a real browser against the packaged app

In 30 seconds ​

Install the plugin in Ktor:

kotlin
install(Svelte) {
    generateTs = true
    transport {
        json()
    }
}

Write a route in Kotlin, save, and call it from Svelte with full types:

ts
import { api } from '#api';

const note = await api.notes.create({ title: 'Buy milk' });

Then build one JAR that holds both:

bash
./gradlew buildFatJar

Made with AI

This project was made heavily with AI, and nothing is guaranteed. It has real tests, but read the code before you trust it with anything important. Since 1.0, releases follow semantic versioning, so only a major release can break your build.

How the pieces fit ​

text
frontend/              SvelteKit with adapter-static
src/main/kotlin/       your Ktor app with install(Svelte)
build.gradle.kts       the ktor-svelte Gradle plugin
  1. At build time, the Gradle plugin installs your frontend dependencies, runs the SvelteKit build and copies the static output into the JAR, together with a manifest that lists every file and its hash.
  2. At run time, install(Svelte) serves those files on the same port as your API. Your Ktor routes always win, missing pages get the SPA fallback, and hashed assets are cached forever.
  3. In development, the plugin proxies page requests to Vite, so you get hot reload on Ktor's port with the same origin as production, and the TypeScript client is regenerated from your routes whenever the app starts.

The frontend is plain files, so there is no Node process in production. SvelteKit runs in the browser, and Ktor is the only backend. The FAQ explains why ktor-svelte uses adapter-static and what that means for server-side rendering, and How it works follows a request through the app.

What you get ​

  • One port, one JAR. Works with jar, Shadow and Ktor's buildFatJar.
  • Your routes win. Anything you define in Ktor beats the frontend, whatever order you register it in.
  • Generated TypeScript. Types, services, validation rules and typed errors from your Ktor routes, with no npm dependencies. See Generated TypeScript.
  • Real hot reload. One command runs Vite, the Kotlin compiler and your app. See Dev workflow.
  • Secure defaults. CSP, security headers, origin check, body and rate limits, hidden error details. See Security.
  • Fast defaults. Immutable caching, ETags, precompressed files, compressed API responses, real 404s for stale chunks. See Transport and compression.
  • Frontend libraries from Kotlin. Tailwind, DaisyUI, Lucide, i18n, image optimization and error toasts, configured in Gradle. See Frontend libraries.
  • Any package manager. Bun, pnpm, npm or Yarn, picked from your lockfile.
  • IntelliJ run configurations. One click starts everything.

The full list, grouped by what you are doing, is on Everything ktor-svelte does.

Is it for you? ​

It fits well when:

  • the backend is, or should be, on the JVM, and you want a SvelteKit frontend with it,
  • you want to deploy one artifact and run one process,
  • you want the frontend to break at compile time, not in production, when an API changes.

Look elsewhere when you need server-side rendering on every request, or when the frontend and backend are deployed by different teams. Compared with other setups goes through the alternatives.

Where to go next ​

Documentation for ktor-svelte 1.4.0. Released under the Apache 2.0 license.