The browser never talks to Go with the internal secret. Next is the BFF.
Official pattern#
Steps in code#
1. Auth and section gate#
2. Go transport#
3. Errors#
Propagate structured errors so the client can use isStructuredApiErr and upsell.
4. Cache (Cache Components)#
The shared fetch does not decide cache. Each cacheable read declares it with 'use cache: remote'.
For reads shareable across members of the same profile:
Rules:
- Auth (
cookies,headers, Better Auth) and membership/section outside the'use cache: remote'scope. - Never pass the session token as an argument of a shareable cached function: it becomes part of the cache key and blocks sharing + fragments Runtime Cache.
- Inside the cached scope: only
baseFetchInternal/baseFetchInternalMeta(server-to-server). Writes and user-scoped reads still usebaseFetch/baseFetchMeta+ session Bearer. - Tags per surface in every domain (list / trash / detail / resource). Helpers in
services/shared/server/cache-tags.ts. - Do not use a single profile-wide cache tag in product queries: that purges trash and unrelated surfaces on every write.
- Do not put filters/cursors in tags — the function args form the cache key.
- Do not put in Data Cache: session/identity, permission checks, usage/audit, editor documents, streaming, writes, exports,
assignedToMe/createdByMefilters (they use session or a per-userIdkey in phase 2). - Writes:
invalidateCache.*→updateTag(immediate, read-your-own-writes) + a singlerefresh(). Prefer domain helpers ortags([...]). Use{ refresh: false }only in autosave/editor/blob. Do not userevalidateTagin product mutations. - Materialization with side effects (e.g. finance ensure-month) goes in a Server Action fired from the client, not in the RSC render.
4b. App init and dynamic reads (react.cache)#
Three distinct layers:
| Layer | What for | Where |
| --- | --- |
| Upstash Redis (Go) | Hot payload of GET /users/me/session (me:session:v2:*), identity and access | Go API |
| react.cache (request) | Per-request dedupe of identity, session, access, usage, canView | Next BFF / RSC |
| 'use cache: remote' (Data Cache) | Cacheable product reads with cacheTag | Domain queries |
Init warm path (one authenticated document):
- Do not call
GET /users/meproactively on every request: the Redis session already brings user + profiles + permissions (+canViewAuditLogs,settings). - Do not preload usage/invitations/library pickers in the global profile layout.
- Shell/gate access:
profileAccessFromSession/resolveProfileAccess(session first; Go/accessonly as fallback). Audit:canViewAuditLogsFromSession— no hop to/audit-logs/accesson the cold path. - Client bootstrap (
GoUserBootstrapBoundary) only if BA says signed-in and there is no SSRinitialSession.
Helpers: shared/lib/server/app-session.ts, platform-bootstrap.ts, require-go-domain-auth.ts.
5. Invalidation#
Security checklist#
- Action marked
'use server' - Do not import secrets in client components
-
studioIdvalidated on the server, not only from a blind client - Correct app section (
work,daily, finance…)
Where to look#
| Need | Path |
|---|---|
| Better Auth identity | shared/lib/server/better-auth-session.ts |
| App session (Redis init) | shared/lib/server/app-session.ts, platform-bootstrap.ts |
| Domain access gates | shared/lib/server/require-go-domain-auth.ts |
| Domain helpers | shared/lib/server/go-domain-action-helpers.ts |
| Tags / invalidation | services/shared/server/cache-tags.ts, cache-invalidation.ts |
| Go fetch | services/shared/server/base-fetch.ts |
| Cached query example | services/tasks/queries/list-tasks.ts |