Context
The web app is server-first: tokens live in three httpOnly cookies and the browser never sees them (ADR-0007). Server components read the session and call the API directly. But some screens are genuinely interactive in the browser — filters, pagination, later a job-status poll — and need to fetch and mutate after the page has loaded, without a full navigation. Those calls cannot go to the API, because the browser has no token to send, and must not go to /api/*, because Caddy routes the whole of that prefix to NestJS (the same reason /session/* exists).
Decision
- Client screens use TanStack Query v5. A single
QueryClientis created per browser session inapp/providers.tsx(useState(() => new QueryClient(...)), never module-level, so no cache is shared between requests or users) withstaleTime: 30_000,retry: 1,refetchOnWindowFocus: false. The provider reads no cookies or headers, so it does not turn static routes dynamic — the landing page still prerenders. - **They call
/bff/*, never the API and never/api/*.** Each/bff/*route handler runs on the Next server, reads the httpOnly access cookie, forwards to the API withAuthorization: Bearer, and returns the ADR-0003 envelope. The token stays on the server.apps/web/src/lib/bff/forward.tsowns the whole contract, so a route (app/bff/me/route.ts) is one line. - The BFF runs the same three-outcome refresh rotation as
proxy.ts(rotateSession,applyRotation— the shared cookie side-effect helper). When the API answers 401 the BFF rotates once and, onrotated, sets the refreshed cookies and retries; onrefusedclears the cookies and returns 401; onunavailablereturns 503 and keeps the cookies. It reuses that logic rather than reimplementing it. - The BFF status codes the client branches on:
401— session over, the client redirects to/login;503— the API is down, show an error and keep the session;2xx— the envelope'sdata.bffFetch(client-safe, nonext/headers) turns a non-2xx envelope into a typedBffError { status, message, code? }that TanStack Query surfaces.
Alternatives considered
- Fetch the API directly from the browser. Rejected: the access token would have to leave the httpOnly cookie to travel as a bearer header, which is exactly the XSS exposure the cookie design avoids; it would also need CORS on the API and reopen CSRF questions the single-origin cookie setup closed.
- Only server components, no client fetching. Fine for read-once pages and still the default, but a genuinely interactive screen would need a full navigation for every filter change.
- **A route under
/api/*.** Rejected: Caddy hands that prefix to NestJS, so the handler never runs.
Consequences
- Default stays server-first. Reach for a client
/bffquery only when a screen is genuinely interactive after load; a page that reads its data once renders on the server and calls the API there. The worked sample is the profile summary island on/account/profile, next to (not replacing) the server-rendered edit form. - Every new client screen adds a thin
/bff/*route overforwardToApiplus auseQueryhook keyed on the resource. After a server mutation, invalidate the query key (the sample passes asyncSignalthe server derives from its revalidated data, so the island refetches without either side calling the other). - One more hop for client calls (browser → Next → API) in exchange for the token never leaving the server. Rotation is deduplicated in
rotateSession, so parallel/bff/*calls on one page do not each spend the refresh token.