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.
| Concern | Library |
|---|---|
| Framework | Svelte 5 (runes), TypeScript 5.9, Vite 7 |
| UI kit | bits-ui primitives in the shadcn-svelte style, Tailwind CSS 4 |
| Router | A small history router in src/router |
| State | Rune stores: auth, org, theme |
| Data fetching | TanStack Query for Svelte (v6, runes API), TanStack Table core |
| Charts | Chart.js |
| i18n | i18next |
| Toasts | svelte-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.
Run it locally
Section titled “Run it locally”Start the API first (see Installation), then:
cd frontend/sveltenpm installnpm run devThe 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.
Folder layout
Section titled “Folder layout”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.tsAdding a page
Section titled “Adding a page”-
Create the component in
src/pages/. -
Add it to the route table in
src/router/routes.ts.metaworks like the Vue app:layout,auth/admin/guest, andpermissions:{ path: '/reports', component: () => import('@/pages/ReportsPage.svelte'), meta: { layout: 'main', auth: true, permissions: ['analytics:read'] } }, -
Add a navigation entry in
src/components/MainSidebar.svelte(AdminSidebar.sveltefor 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,}));Auth and session
Section titled “Auth and session”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
authstore at 80% of the token lifetime, re-checked when a background tab becomes visible; - on boot:
main.tsawaitsauth.init(), which callsrestoreSession(), 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.
Tenant selection
Section titled “Tenant selection”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.
Translations
Section titled “Translations”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.
Configuration
Section titled “Configuration”Settings are Vite env vars read in src/config/env.ts. All are optional.
cp .env.example .env.local| Variable | Purpose |
|---|---|
VITE_API_BASE_URL | API origin. Leave empty to use the dev proxy locally and same-origin in production. |
VITE_BACKEND_ORIGIN | API 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_MODE | Shows demo login shortcuts. When empty, on for the dev server and off for production builds. |
VITE_POSTHOG_KEY | PostHog project key. posthog-js is only downloaded when set. |
VITE_POSTHOG_HOST | PostHog host. Defaults to the API’s /ph proxy. |
VITE_CRISP_WEBSITE_ID | Crisp chat id. The widget only loads when set. |
VITE_GOOGLE_CLIENT_ID | Google Identity Services client id. See Google OAuth. |
Values are inlined at build time, so they must be set when you run npm run build.
Tests and checks
Section titled “Tests and checks”npm test # Vitest, single run (jsdom)npm run test:watch # watch modenpm run check # svelte-check type checkThere 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).
Production build
Section titled “Production build”npm run buildThis 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:
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.