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.tson 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/andadmin/moved into a(app)group (URLs unchanged).(app)/+layout.server.tschecks the session once via/api/whoamiand returnsuser,notificationsandunreadCountto 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.tsrequiresrole >= 2and responds 403 otherwise, before any admin data is requested.- New
+page.server.tsloads fordash(sites; also handles the legacy?edit=redirect),admin,admin/users, anddash/sites/[id](site + access check).
Pages
- They read
data.user/data.notifications/ their own data, and refresh withinvalidate('app:session' | 'app:site' …)orinvalidateAll()after mutations. No moreonMountfetches,loadingstates orfetchWhoAmI()calls in pages. - The branch as a whole is net −93 lines even after adding the loads and their tests.
API client
apiFetchthrows anApiError(withstatus), accepts SvelteKit'sfetchso loads run server-side with the visitor's cookies, and in the browser redirects to/login?next=…on a 401. Thelib/apihelpers (fetchWhoAmI,listPages,fetchPage,fetchNotifications,getInstanceInfo,listAdminUsers,getRegistrationStatus) take an optionalfetchFn.
Hook
hooks.server.tsno longer gates protected routes (it only proxies/api, blocks/siteson 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 newsafeNextPathhelper (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 check0 errors,bun run test(138),bun run build,bun run lintfor 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:
/adminand/admin/users→ 403; - a password change makes the old cookie redirect to login; GET
/logoutdoesn'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 (includingwhoami, 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.AppHeaderandNotificationsDrawerare 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 staticlogin/registerroutes. - A jsdom/browser test setup for interaction tests.
Merge notes #
Independent of the other two open branches (refactor/response-schemas, refactor/models-and-migrations).