Skip to content
Buy the boilerplate

Svelte

The Svelte UI lives in frontend/svelte. It is a feature-for-feature port of the Vue and React UIs: same routes, screens, design tokens, storage keys and translation files, on the same API. Starter includes one web UI of your choice; Pro and Enterprise include all five.

It is a plain Svelte 5 SPA built with Vite, not SvelteKit, so it builds to static files like the other UIs. Coding conventions for this app are in frontend/svelte/CONVENTIONS.md.

ConcernLibrary
FrameworkSvelte 5 (runes), TypeScript 5.9, Vite 7
UI kitbits-ui primitives in the shadcn-svelte style, Tailwind CSS 4
RouterA small history router in src/router
StateRune stores: auth, org, theme
Data fetchingTanStack Query for Svelte (v6, runes API), TanStack Table core
ChartsChart.js
i18ni18next
Toastssvelte-sonner

The primitives in src/components/ui are source files in the repo. Edit them to restyle. npx shadcn-svelte@latest add <component> adds new ones next to them.

Start the API first (see Installation), then:

Terminal window
cd frontend/svelte
npm install
npm run dev

The dev server listens on http://localhost:4500. npm start does the same but honours a PORT variable. With Aspire, dotnet run --project src/DotnetSaas.AppHost starts the API and this app together (see .NET Aspire).

The Vite dev server proxies /api/, /health and /{en,pt-BR,es}.json to the API, so you need no CORS setup. The proxy target is VITE_BACKEND_ORIGIN when it is set in the environment (Aspire sets it), otherwise http://localhost:5113.

frontend/svelte/src/
├── components/ # shared components; components/ui holds the bits-ui primitives
├── composables/ # useServerTable, useConfirm, useMe, ... (*.svelte.ts use runes)
├── config/env.ts # runtime configuration from VITE_* vars
├── core/ # api client, auth (jwt, session refresh), i18n, services, storage
├── layouts/ # AuthLayout, MainLayout, AdminLayout, BlankLayout
├── lib/ # cn(), formatters, i18n.svelte.ts (reactive t()), query client
├── models/ # API DTO types
├── pages/ # route views; pages/admin for /admin
├── router/ # router.svelte.ts (the router) and routes.ts (the route table)
├── stores/ # auth.svelte.ts, org.svelte.ts, theme.svelte.ts
├── App.svelte
└── main.ts
  1. Create the component in src/pages/.

  2. Add it to the route table in src/router/routes.ts. meta works like the Vue app: layout, auth / admin / guest, and permissions:

    { path: '/reports', component: () => import('@/pages/ReportsPage.svelte'), meta: { layout: 'main', auth: true, permissions: ['analytics:read'] } },
  3. Add a navigation entry in src/components/MainSidebar.svelte (AdminSidebar.svelte for admin pages).

Write links as <a href={href('/reports')}>. href() adds the base path the app was built with. Fetch data with createQuery, whose options are a function and whose result is a reactive object:

const users = createQuery(() => ({
queryKey: queryKeys.users.list(params),
queryFn: () => api.get<PagedResult<UserDto>>('/users', { query: ... }),
enabled: !!org.selectedOrgId,
}));

The access token (a JWT) is kept in localStorage under accessToken. The refresh token stays in an httpOnly refresh_token cookie scoped to /api/v1/auth, which JavaScript cannot read. See JWT tokens.

src/core/auth/session-refresh.ts is the only code that refreshes. It calls POST /api/v1/auth/refresh with credentials: 'include', and concurrent callers share one in-flight request. It runs:

  • before each request, when the stored token is expired or within 30 seconds of expiry (src/core/api/client.ts);
  • after a 401, once, then the request is retried once for any HTTP method; a failed refresh signs the user out;
  • on a timer in the auth store at 80% of the token lifetime, re-checked when a background tab becomes visible;
  • on boot: main.ts awaits auth.init(), which calls restoreSession(), before mounting the app.

While a super admin is impersonating a user, the token is never refreshed; when it expires, the impersonation ends. Logout calls POST /auth/logout, then clears local state and the query cache.

The org store (src/stores/org.svelte.ts) holds the organizations and the selected one, populated from /me by MainLayout. The saved selectedOrgId in localStorage is restored, or the first organization is picked.

The API client sends X-Organization-Id on every non-anonymous request. Switching organizations clears the query cache. MainLayout enforces meta.permissions once the org context has loaded, so a deep link to a permitted page is not redirected while /me is in flight. See Permissions.

i18next loads /{lang}.json (en, pt-BR, es) from the API. These are the files in src/DotnetSaas.Api/wwwroot, shared by all web UIs. Components import t from @/lib/i18n.svelte, which re-renders them when the language changes or a bundle arrives:

<script lang="ts">
import { t } from '@/lib/i18n.svelte';
</script>
<h1>{t('Auth.Login.Title')}</h1>

Keys are flat and contain dots. A missing key renders its humanized last segment.

Settings are Vite env vars read in src/config/env.ts. All are optional.

Terminal window
cp .env.example .env.local
VariablePurpose
VITE_API_BASE_URLAPI origin. Leave empty to use the dev proxy locally and same-origin in production.
VITE_BACKEND_ORIGINAPI origin for the Hangfire dashboard link, and the dev proxy target. Falls back to VITE_API_BASE_URL, then http://localhost:5113 in dev.
VITE_DEMO_MODEShows demo login shortcuts. When empty, on for the dev server and off for production builds.
VITE_POSTHOG_KEYPostHog project key. posthog-js is only downloaded when set.
VITE_POSTHOG_HOSTPostHog host. Defaults to the API’s /ph proxy.
VITE_CRISP_WEBSITE_IDCrisp chat id. The widget only loads when set.
VITE_GOOGLE_CLIENT_IDGoogle Identity Services client id. See Google OAuth.

Values are inlined at build time, so they must be set when you run npm run build.

Terminal window
npm test # Vitest, single run (jsdom)
npm run test:watch # watch mode
npm run check # svelte-check type check

There is no lint script; .prettierrc (with prettier-plugin-svelte) is included for npx prettier --check src.

The Playwright suite in e2e/ runs against Svelte with npm run test:svelte (start the stack first with DEMO_MODE=true dotnet run --project src/DotnetSaas.AppHost).

Terminal window
npm run build

This runs svelte-check and then writes a static site to dist/.

To ship UI and API as one container, use Dockerfile.production at the template root. The API serves the UI at / on the same origin, so you need no CORS configuration and the refresh cookie works:

Terminal window
docker build -f Dockerfile.production \
--build-arg FRONTEND=svelte \
--build-arg POSTHOG_KEY=phc_... \
--build-arg GOOGLE_CLIENT_ID=....apps.googleusercontent.com \
--build-arg CRISP_WEBSITE_ID=... \
-t myapp .

The build args become VITE_POSTHOG_KEY, VITE_GOOGLE_CLIENT_ID and VITE_CRISP_WEBSITE_ID during the build. Leave one empty to turn that integration off. In a package with a single UI, FRONTEND already defaults to it.

To host dist/ on its own, proxy /api/, /health, /admin/hangfire and /{lang}.json to the API and send other unknown paths to index.html. Alternatively, set VITE_API_BASE_URL and add the UI origin to the API’s AllowedOrigins, keeping both on the same site because the refresh cookie is SameSite=Strict. See Docker.

© 2026 .NET SaaS