Skip to content

Web (SvelteKit)

apps/svelte-web is SvelteKit 2 + Svelte 5 (runes), Tailwind 4, bits-ui, Paraglide JS. The repo-root CLAUDE.md and apps/svelte-web/CLAUDE.md are the agent context; this page is the human-readable summary.

Web mutations are form actions — permanent

Section titled “Web mutations are form actions — permanent”

SvelteKit +page.server.ts form actions are locked in for web mutations. Do not migrate web mutations to client-side tRPC mutations.

Reasons:

  • Progressive enhancement — forms work with JS disabled.
  • No-JS support — important for some users + crawlers.
  • superforms validation — server-side Zod validation with typed form.errors flowing back to the UI.
  • CSRF for free — same-origin form-action POSTs are covered by SvelteKit’s built-in csrf.checkOrigin (Origin validation) plus the SameSite=Lax session cookie; no token needed.

tRPC on web is for:

  • Read queries (typed, cached server-side via createCaller(locals)).
  • Tools that need typed access (admin scripts, etc.).
  • Mobile (the main tRPC consumer — see Conventions → Mobile (Native)).
+page.server.ts
import { fail } from '@sveltejs/kit';
import { superValidate } from 'sveltekit-superforms';
import { zod4 } from 'sveltekit-superforms/adapters';
import { localizedRedirect } from '$lib/server/redirect';
import { schema } from './schema';
export const load = async ({ locals }) => {
if (!locals.user) localizedRedirect(303, '/');
return { user: locals.user };
};
export const actions = {
default: async ({ request, locals }) => {
if (!locals.user) return fail(401);
const form = await superValidate(request, zod4(schema));
if (!form.valid) return fail(400, { form });
// ...db op via shared service in @repo/api/services
return { form };
}
};

Shared business logic lives in packages/api/src/services/ — call it from both the form action and the tRPC procedure.

Redirects keep the locale. A hook, load or form action redirects with localizedRedirect(status, '/path') from $lib/server/redirect, which runs the path through Paraglide’s localizeHref so a visitor on /en/... stays in English. Write the path without a locale prefix. A URL on another origin (Stripe Checkout, the Customer Portal) goes through externalRedirect(status, url) from the same module. ESLint rejects importing redirect from @sveltejs/kit anywhere else in src/. Client-side navigation follows the same rule: a hand-written path passed to goto, pushState or replaceState is wrapped in localizeHref(...) from $lib/paraglide/runtime, and ESLint rejects a bare goto('/path'). A target built from page.url already carries the prefix.

+server.ts
import { json } from '@sveltejs/kit';
import { rateLimitByUser } from '$lib/server/rate-limit';
export const POST: RequestHandler = async ({ request, locals }) => {
if (!locals.user) return json({ error: 'Unauthorized' }, { status: 401 });
const { allowed } = await rateLimitByUser(locals.user.id, 'action', 20, 60);
if (!allowed) return json({ error: 'Rate limited' }, { status: 429 });
// ...op
};

rateLimitByUser(userId, scope, limit, windowSeconds) is the canonical entry point. Backed by Valkey with in-memory fallback. Source: apps/svelte-web/src/lib/server/rate-limit.ts.

src/routes/
├── (public)/ # browse, /properties, /agents
├── portal/ # personal user area (auth required)
│ └── become-agent/ # action-only — Dialog is mounted at root layout
├── dashboard/ # agent workspace (auth + agent role)
├── api/ # REST endpoints — uploads, places, chat
├── auth/callback/ # OAuth callback
└── sitemap.xml/ # dynamic

Files under src/lib/server/ are server-only — never imported from .svelte components or anywhere a client bundle reaches. Vite enforces this with $lib/server/* import restrictions.

Common patterns:

ModulePurpose
lib/server/auth.tsBetter Auth configuration
lib/server/s3.ts, imgproxy.ts, meilisearch.tsOne-line re-exports of @repo/integrations (S3 client, HMAC image URLs, Meilisearch client)
lib/server/email.tsResend email service
lib/server/rate-limit.tsThin shim: resolves the client IP from the SvelteKit event, then calls the Valkey-backed limiter in @repo/integrations
lib/server/sanitize.tsInput sanitization (text, URL, UUID, CUID)
  • Validation — Zod schemas in @repo/validation (shared with mobile + tRPC).
  • Wrapper — sveltekit-superforms with zod4 adapter.
  • Display — formsnap + bits-ui primitives.
  • i18n errors — makeValidationErrorMap adapter pipes Zod issue codes through Paraglide.

The auth modal is mounted globally at the root layout. Three trigger sources route to it via lib/auth-guard.svelte.ts:

  • Anonymous user clicks an auth-required affordance (favorite, save, request tour).
  • Header / mobile menu sign-in.
  • 401 response from a protected action.

Email-OTP signup captures firstName / lastName in the modal form, then calls authClient.updateUser({ name, firstName, lastName }) immediately after signIn.emailOtp succeeds — the OTP signIn body only carries { email, otp }, so we persist the name fields in a follow-up call. Errors from updateUser are logged but do not block the redirect.

Use the shared translateAuthError(error) helper in src/lib/auth-client.ts:

import { translateAuthError } from '$lib/auth-client';
import { m } from '$lib/paraglide/messages';
if (error) {
toast.error(translateAuthError(error)); // ← localized
// NOT: toast.error(error.message); // ← Better Auth's raw English
}

Maps Better Auth error.code (INVALID_OTP, OTP_EXPIRED, INVALID_EMAIL, USER_NOT_FOUND, TOO_MANY_REQUESTS, EMAIL_NOT_VERIFIED) to Paraglide message functions, with a status-429 fallback to auth_error_rateLimited and a catch-all auth_error_unknown.

Colour tokens are generated into apps/svelte-web/src/routes/tokens.generated.css from design-tokens/tokens.cjs (shared with iOS and Android — see Design → Overview); layout.css imports them and owns the web-only scales. The docs site reuses the same primary indigo + Source Serif 4 — see apps/docs/src/styles/brand.css.

Use Tailwind utility classes that reference the tokens:

<div class="bg-card text-card-foreground border-border">
<button class="bg-primary text-primary-foreground hover:bg-primary-hover">…</button>
</div>

No hex literals, no arbitrary sizes, no dark: colour overrides in components. The token system covers light + dark mode (so a dark:bg-… override is a defect — the dark token set already swaps the colour), status badges (status-active, status-pending, …), and the Meridian scales:

ScaleUtilities
Typetext-micro 10/600 (uppercase eyebrows only) · text-badge 11/600 · text-xs 12 · text-body-sm 13 · text-ui 13/600 (buttons, tabs, chips) · text-sm 14 · text-base 16 · text-h3 15 · text-xl 20 · text-h1-sm 22 · text-2xl 24 · text-h1 28 · text-display-sm 32 · text-display 46
Radiusrounded-sm 6 (badges, checkboxes) · rounded-item 8 (rows, thumbnails) · rounded-md 10 (inputs, buttons, chips) · rounded-panel 12 (menus, popovers, toasts) · rounded-lg 16 (cards) · rounded-xl 20 (sheets, dialogs) · rounded-full
Shadowshadow-xs (buttons at rest) · shadow-chip (chips over photos, map pins) · shadow-pill (the active segmented pill) · shadow-raised (menus, hovered cards, floating bars) · shadow-overlay (dialogs, sheets) · shadow-card-hover
Motionduration-fast 120 (hover) · duration-base 200 · duration-slow 320 (sheets) · ease-out / ease-in-out
Field statesfocus-visible:border-primary focus-visible:shadow-ring-focus · aria-invalid:border-destructive aria-invalid:shadow-ring-error; keyboard focus on buttons/links stays focus-visible:ring-[3px] focus-visible:ring-ring/55

Control heights follow the Meridian contract: 44px on touch, 40px from md (h-11 md:h-10), sm 32 only in dense toolbars.

Both Sentry inits (server + client) install a beforeSend that redacts:

  • cookie / authorization / csrf headers
  • body fields matching password|token|otp|secret

Don’t remove the scrubber — Sentry defaults capture the full Cookie header on every error.