A lightweight, self-hosted multi-tenant web publishing platform

refactor(frontend): server load functions and an (app) route group #14

Merged pre1ude.dev
targeting dev from refactor/frontend-load-functions
Merged opened by pre1ude.dev
targeting dev from refactor/frontend-load-functions

Why #

Every authenticated page fetched its data in onMount, so the server rendered a "Loading…" shell and the browser then made the real requests. In practice that meant:

  • the session was checked twice (in hooks.server.ts on full page loads, and again by each page);
  • after a client-side navigation such as /dash → /account, only the page's own fetch ran, so an expired session showed a raw error instead of redirecting to login;
  • the admin role check happened in the browser, after the admin page had already started loading;
  • the ?edit= redirect and the site-detail reload logic were $effects with hand-rolled race guards (lastLoadedKey).

What changed #

Route structure

  • account/, dash/ and admin/ moved into a (app) group (URLs unchanged).
  • (app)/+layout.server.ts checks the session once via /api/whoami and returns user, notifications and unreadCount to every page. If the backend rejects the session (401/403/404) it clears the cookie and redirects to /login?next=<current path>; a backend outage (5xx) shows an error page and keeps the cookie.
  • (app)/admin/+layout.server.ts requires role >= 2 and responds 403 otherwise, before any admin data is requested.
  • New +page.server.ts loads for dash (sites; also handles the legacy ?edit= redirect), admin, admin/users, and dash/sites/[id] (site + access check).

Pages

  • They read data.user / data.notifications / their own data, and refresh with invalidate('app:session' | 'app:site' …) or invalidateAll() after mutations. No more onMount fetches, loading states or fetchWhoAmI() calls in pages.
  • The branch as a whole is net −93 lines even after adding the loads and their tests.

API client

  • apiFetch throws an ApiError (with status), accepts SvelteKit's fetch so loads run server-side with the visitor's cookies, and in the browser redirects to /login?next=… on a 401. The lib/api helpers (fetchWhoAmI, listPages, fetchPage, fetchNotifications, getInstanceInfo, listAdminUsers, getRegistrationStatus) take an optional fetchFn.

Hook

  • hooks.server.ts no longer gates protected routes (it only proxies /api, blocks /sites on the app origin, and serves user sites on subdomains). The redundant whoami call and its tests are gone.

Login

  • After login, the redirect honours ?next= through a new safeNextPath helper (same-site paths only; //host, /\host, absolute URLs and control characters fall back to /dash).
  • The login/register page no longer fetches registration status a second time in onMount.

Tests #

  • New: layout load (redirects for 401/403/404 with the right next, 502 without clearing the cookie on outages, still loads when notifications fail), admin layout (403/allow), site-detail load (invalid id, pending approval, no access, API errors), safeNextPath.
  • Removed: the hook's session-gating tests, which are superseded by the layout-load tests.
  • bun run check 0 errors, bun run test (138), bun run build, bun run lint for changed files — all clean.

Verified against the real stack #

I ran make dev with a throwaway SECRET_KEY and checked with curl:

  • logged-out /dash, /admin, /dash?edit=5 → 303 to /login?next=…;
  • admin: /dash, /account, /admin, /admin/users → 200, with server-rendered data (no "Loading…" text); /dash?edit=5 → 307 to /dash/sites/5;
  • regular user: /admin and /admin/users → 403;
  • a password change makes the old cookie redirect to login; GET /logout doesn't end the session, POST does;
  • /sites/* and /api/sites/* → 404 on the app origin.

Not verified: behaviour in a real browser — in particular client-side navigation after the session expires, the notifications drawer refresh, and the site editor's tab and reload handling. Please click through those before merging.

Review notes #

  • Loads call await parent() first, so a bad session redirects before any page-specific API call is made.
  • invalidateAll() re-runs every active load (including whoami, which walks the user's site folders for storage totals); it's used after mutations and when opening the notifications drawer, not on every navigation.
  • AppHeader and NotificationsDrawer are still rendered per page; moving them into the (app) layout is a sensible follow-up but touches each page's title/back-link props.

Not in this PR #

  • (auth) route group and form actions for login/register/forgot/reset (still fetch-based).
  • Replacing [slug] with static login/register routes.
  • A jsdom/browser test setup for interaction tests.

Merge notes #

Independent of the other two open branches (refactor/response-schemas, refactor/models-and-migrations).

Commits 1

refactor(frontend): load data in server load functions under an (app) route group
Sign up or Sign in to add to the discussion.