ReviewOS

stacks/stx

feat(data): hydrate server-fetched data into client fetch state without a duplicate request

#1963
Closed glennmichael123 opened this 10 days ago ·
10 days ago

Problem

stx has useFetch/useAsyncData, server scripts, and a server-to-client data bridge, but these do not provide an automatic request-scoped fetch-to-hydration lifecycle. Applications must manually connect server-loaded data to client fetching state.

The goal is to fetch once for SSR and reuse that result during browser hydration, while retaining explicit refresh and navigation behavior.

Existing foundation

Reviewed at 7d93c4c: use-fetch.ts and signals.ts. #1869 already implemented concurrent useQuery deduplication. This request builds on that work; it does not propose rebuilding it.

Scope and acceptance criteria

  • Provide a keyed server data-loading API and integrate its results into the client data layer.
  • Preserve the rule that only script server runs server-side; do not implicitly execute client scripts on the server.
  • A server render followed by hydration makes one underlying data request for a hydrated key.
  • Concurrent SSR requests cannot share private data accidentally.
  • Define refresh, invalidation, navigation, failed-load, and missing-payload behavior.
  • Preserve existing deduplication, superseding-request, and unmount-cancellation guarantees.
  • Add end-to-end request-count and request-isolation tests and document usage.

Prior art: Nuxt data fetching.

10 days ago

Implemented and shipped on main in ab902b6, with browser-fixture routing corrected in 450e61a. useServerData(key, loader) deduplicates within a request-isolated server-script scope and emits escaped JSON for keyed useFetch/useAsyncData hydration. Initial hydration skips the duplicate request; explicit refresh, invalidation, missing/failed payloads, reactive URL changes, and SPA snapshot replacement are documented in docs/features/server-data.md. Client scripts are not executed server-side. Real-browser tests cover request counts, concurrent private SSR responses, full-document/fragment/layout navigation, and stale responses after teardown; module/runtime parity and existing dedupe/supersession regressions also pass. Final local full suite: 13,126 passed. CI typecheck, lint, tests and publish-commit are all green: https://github.com/stacksjs/stx/actions/runs/36003247008.

Sign in to comment on this issue.