Resilient Interfaces & Progressive Enhancement

SvelteKit as a Backend for the Frontend


Learning Objectives

  • You can load permitted private data through a browser-facing server.
  • You can keep identity with its request and distinguish anonymity from outage.
  • You can select safe page data while retaining the API’s access checks.

The public catalogue load needed no account identity. A private document does. A backend for the frontend, or BFF, obtains data from an API for the page it serves. SvelteKit renders the page; Hono checks permission and owns the stored state. Figure 1 follows the request through both servers.

Figure 1. The page server adapts the response while the API decides access.

Keep identity with the request

The supplied client sends only this request’s session cookie to a fixed, trusted API destination. The session hook runs before the page load and puts its result in event.locals: user, csrfToken and sessionUnavailable.

Each request needs its own locals. A module-level currentUser can be replaced while another request is waiting. Follow two overlapping requests:

EventAino’s requestBela’s request
Aino starts a session lookup.Awaiting its result.Has not started.
Bela’s later lookup finishes first.Still waiting.Bela is in Bela’s locals; her load can continue.
Aino’s lookup finishes.Aino is in Aino’s locals; her load continues.Her own locals remain unchanged.

See SvelteKit state management for request-specific state.

Load the permitted representation

Complete the page load using the supplied hook and client. First check the session outcome: an outage produces 503, an anonymous visitor 401. Reporting every lookup failure as “Sign in” would hide an unavailable service. For an authenticated visitor, read the document through the client, retaining its missing/inaccessible and outage outcomes.

Return only the page’s user, document and csrfToken fields. Select the user’s ID and display name, and the document’s ID, title, body and version. Exclude ownerId, raw cookies and extra private fields before serialization. Under the Part 5 contract, the session-bound CSRF token is intentionally readable form data; it is not an authentication credential by itself.

In the exercise, Aino’s permitted request shows “Aino document” and her own content. Bela’s concurrent request shows “Bela document” and Bela’s content. Each page must retain its request’s identity and exclude the raw session cookie and the API’s private token.

Account-specific page data

0 / 35 points

Complete routes/documents/[id]/+page.server.ts. The path is relative to src/; the application and request hook are supplied. The hook initializes this request’s locals.user, locals.csrfToken and locals.sessionUnavailable, then resolves its session. Keep those values with the request.

Supplied client

createApi(fetch, cookie) constructs the fixed API client. Pass this request’s fetch and the value selected by selectedCookie(request.headers.get("cookie")).

  • document(id) resolves to { id, title, body, version, ownerId }, possibly with extra private fields. It throws 404 for missing or inaccessible records and 503 on outage.
  • positiveId(params.id) returns the positive numeric route ID or throws 404.

Load and returned data

An unavailable session service produces error(503, "Session unavailable"). Otherwise an anonymous visitor receives error(401, "Sign in"). For an authenticated visitor, read the route’s document and return only:

type PageData = {
user: { id: number; displayName: string };
document: { id: number; title: string; body: string; version: number };
csrfToken: string | null;
};

Aino’s upstream read may finish after Bela’s. Each response must still contain its own permitted document and request identity. Keep the current user in request-local state.

Raw cookies, ownerId and extra private fields must remain out of page data. The supplied client handles permitted access and cookie forwarding; keep those checks in use.

Choose Submit solution. Permitted load: 5 points; concurrent separation: 15; safe page data: 10; unavailable/access outcomes: 5.

Optional reference: how the supplied session client forwards credentials

A gateway exposes one browser origin: /api/... reaches Hono and page routes reach SvelteKit. The browser sends matching session cookies with its requests. HttpOnly prevents JavaScript from reading a cookie; the browser can still send it.

The page server makes a separate API request. From a header such as theme=dark; session=A; analytics=x, the client selects only session=A. It disables automatic credential forwarding and rejects redirects. Its fixed destination prevents a caller from choosing where a private credential is sent; checking only that a URL uses HTTP or HTTPS would not establish trust.

An ordinary server fetch with credentials: "include" has no access to the browser’s cookie jar. SvelteKit’s request-aware event.fetch can forward credentials in some cases, so the client controls forwarding explicitly. The page server receives an upstream Set-Cookie; forwarding it to the browser requires explicit handling. Login/logout therefore use the gateway’s same-origin API path directly. The private-read exercise starts with an established session.

The hook initializes locals.user and locals.csrfToken to null and locals.sessionUnavailable to false before this request’s lookup. A rejected lookup marks the service unavailable. It then calls resolve(event) to continue to the route.

Check Your Understanding

  1. Which server chooses the permitted document, and which selects its page fields?
  2. Why does the supplied client select a cookie and a fixed destination?
  3. What distinguishes an anonymous session from an unavailable lookup?
  4. Why must overlapping requests keep their identities in separate locals?