Web Server State
Purpose
Plan how the app fetches, caches, invalidates, and mutates server data. Selects the query layer with justification and defines its conventions (keys, staleness, retries, optimistic updates). On Next.js, also draws the line between server-component data fetching and client-side queries.
When to Use
- After
web-state-management assigns server data to this layer.
- When an existing app hand-rolls fetching in effects or stores API data in Redux.
- Not for transport mechanics (
web-api-integration) or form submission UX (web-forms).
Inputs
- The state map (
web-state-management) and API surface (web-api-integration).
- Framework foundation — server components change the read path on Next.js.
- Freshness/consistency requirements per data type (dashboards often need tighter invalidation).
Discovery Questions
- Which data is read-mostly (long staleness) vs live (polling/short staleness/websocket)?
- (Next.js) Which reads belong in server components with fetch caching/revalidation vs interactive client queries?
- Where do mutations need optimistic updates vs invalidate-and-refetch?
- Which lists need pagination/infinite scroll, and how large can they get (
dashboard-tables)?
Responsibilities
- Select the query library (TanStack Query, RTK Query, SWR) with justification — RTK Query only pairs naturally where Redux is already justified.
- Define query-key conventions mirroring the API's resource structure.
- Set caching/staleness defaults per data class; define invalidation rules per mutation.
- Plan error/retry policy (bounded retries, no retry on 4xx) and loading UX expectations.
- Plan optimistic updates only where UX payoff justifies rollback complexity.
- (Next.js) Assign each read path: server component fetch (with revalidation tags) vs client query — not both for the same view without a reason.
Required Workflow
- Take the server-data inventory from the state map.
- Classify data by freshness/consistency needs.
- Choose the library (and the server/client read split on Next.js) with justification.
- Define keys, staleness, invalidation-per-mutation, retry policy.
- Record conventions; hand transport details to
web-api-integration.
Decision Rules
- Every mutation lists the queries it invalidates — unlisted invalidation is a bug waiting.
- Default staleness > 0 for read-mostly data; "always refetch everything" is not a strategy.
- Optimistic updates require a defined rollback path; otherwise invalidate-and-refetch.
- Don't duplicate a server-component-fetched view as a client query; pick the owner per view.
- Polling/websockets only for data whose staleness genuinely hurts users.
Rules
- Server data never mirrors into a global client store (
web-state-management).
- Query functions throw typed errors mapped by
web-api-integration; the layer surfaces them to web-error-handling.
- Conventions are written once and followed; per-feature ad-hoc caching is a review flag.
Anti-Patterns
useEffect + fetch + local state re-implementing a query cache badly.
- Copying query results into Redux/Zustand "for access elsewhere."
- Infinite retry loops hammering a failing endpoint.
- Optimistic updates with no rollback, leaving phantom UI state on failure.
Validation Checklist
Definition of Done
A recorded server-state plan — library choice, key conventions, caching/invalidation/retry rules, and (on Next.js) the server/client read split — that features can implement uniformly without inventing per-screen data logic.
Related Skills
web-state-management, web-api-integration, web-forms, web-error-handling, dashboard-tables, dashboard-reporting, nextjs-foundation.
Related Knowledge
../../../knowledge/ (API resource model, freshness requirements).
Related References
../../../references/web/state/ (query conventions — when populated).
Context Loading Guidance
- Requires: state map, API surface summary, freshness needs.
- Does not require: full backend source, UI component detail.
- May load:
web-api-integration for transport; dashboard-tables for heavy-list needs.
- Stop when: the server-state conventions are recorded.
Token Efficiency Guidance
Define conventions once (keys, staleness classes, invalidation table) rather than per-endpoint prose. A mutation→invalidates table beats paragraphs.
1---2name: web-server-state3description: Use to plan the server-data layer — TanStack Query / RTK Query / SWR selection, query keys, caching, invalidation, retries, optimistic updates, and pagination; on Next.js, how server components and fetch caching split read paths from client queries. Server data lives here, not in a global store.4---56# Web Server State78## Purpose910Plan how the app fetches, caches, invalidates, and mutates server data. Selects the query layer with justification and defines its conventions (keys, staleness, retries, optimistic updates). On Next.js, also draws the line between server-component data fetching and client-side queries.1112## When to Use1314- After `web-state-management` assigns server data to this layer.15- When an existing app hand-rolls fetching in effects or stores API data in Redux.16- **Not** for transport mechanics (`web-api-integration`) or form submission UX (`web-forms`).1718## Inputs1920- The state map (`web-state-management`) and API surface (`web-api-integration`).21- Framework foundation — server components change the read path on Next.js.22- Freshness/consistency requirements per data type (dashboards often need tighter invalidation).2324## Discovery Questions2526- Which data is read-mostly (long staleness) vs live (polling/short staleness/websocket)?27- (Next.js) Which reads belong in server components with fetch caching/revalidation vs interactive client queries?28- Where do mutations need optimistic updates vs invalidate-and-refetch?29- Which lists need pagination/infinite scroll, and how large can they get (`dashboard-tables`)?3031## Responsibilities3233- **Select the query library** (TanStack Query, RTK Query, SWR) with justification — RTK Query only pairs naturally where Redux is already justified.34- Define **query-key conventions** mirroring the API's resource structure.35- Set **caching/staleness defaults** per data class; define **invalidation** rules per mutation.36- Plan **error/retry** policy (bounded retries, no retry on 4xx) and loading UX expectations.37- Plan **optimistic updates** only where UX payoff justifies rollback complexity.38- (Next.js) Assign each read path: server component fetch (with revalidation tags) vs client query — not both for the same view without a reason.3940## Required Workflow41421. Take the server-data inventory from the state map.432. Classify data by freshness/consistency needs.443. Choose the library (and the server/client read split on Next.js) with justification.454. Define keys, staleness, invalidation-per-mutation, retry policy.465. Record conventions; hand transport details to `web-api-integration`.4748## Decision Rules4950- Every mutation lists the queries it invalidates — unlisted invalidation is a bug waiting.51- Default staleness > 0 for read-mostly data; "always refetch everything" is not a strategy.52- Optimistic updates require a defined rollback path; otherwise invalidate-and-refetch.53- Don't duplicate a server-component-fetched view as a client query; pick the owner per view.54- Polling/websockets only for data whose staleness genuinely hurts users.5556## Rules5758- Server data never mirrors into a global client store (`web-state-management`).59- Query functions throw typed errors mapped by `web-api-integration`; the layer surfaces them to `web-error-handling`.60- Conventions are written once and followed; per-feature ad-hoc caching is a review flag.6162## Anti-Patterns6364- `useEffect` + `fetch` + local state re-implementing a query cache badly.65- Copying query results into Redux/Zustand "for access elsewhere."66- Infinite retry loops hammering a failing endpoint.67- Optimistic updates with no rollback, leaving phantom UI state on failure.6869## Validation Checklist7071- [ ] Library selected with justification (and server/client read split on Next.js).72- [ ] Query-key conventions defined.73- [ ] Staleness defaults per data class recorded.74- [ ] Invalidation rules mapped per mutation.75- [ ] Retry/error policy defined; optimistic updates limited to justified cases.7677## Definition of Done7879A recorded server-state plan — library choice, key conventions, caching/invalidation/retry rules, and (on Next.js) the server/client read split — that features can implement uniformly without inventing per-screen data logic.8081## Related Skills8283`web-state-management`, `web-api-integration`, `web-forms`, `web-error-handling`, `dashboard-tables`, `dashboard-reporting`, `nextjs-foundation`.8485## Related Knowledge8687`../../../knowledge/` (API resource model, freshness requirements).8889## Related References9091`../../../references/web/state/` (query conventions — when populated).9293## Context Loading Guidance9495- **Requires:** state map, API surface summary, freshness needs.96- **Does not require:** full backend source, UI component detail.97- **May load:** `web-api-integration` for transport; `dashboard-tables` for heavy-list needs.98- **Stop when:** the server-state conventions are recorded.99100## Token Efficiency Guidance101102Define conventions once (keys, staleness classes, invalidation table) rather than per-endpoint prose. A mutation→invalidates table beats paragraphs.