Skip to content

Commit dc9884e

Browse files
committed
docs(ssr): prune comments on the getServerSnapshot change
1 parent 6e42acf commit dc9884e

2 files changed

Lines changed: 15 additions & 45 deletions

File tree

‎src/useObservable.ts‎

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,11 @@ export function useObservable<T = unknown>(observableId: string, source: Observa
104104
return observable.immutableStatus;
105105
}, [observable]);
106106

107-
// `useSyncExternalStore` requires a third argument when the tree is rendered on a
108-
// server or hydrated; without it React throws "Missing getServerSnapshot, which is
109-
// required for server-rendered content" and the surrounding subtree falls back to
110-
// client rendering.
111-
//
112-
// This applies on React 18 and up. Below that, `use-sync-external-store/shim` ignores the
113-
// third argument on the server as well as the client, so both shapes render identically
114-
// and nothing here regresses. It also does nothing for it.
115-
//
116-
// This deliberately does NOT return `observable.immutableStatus` the way `getSnapshot`
117-
// does. `preloadedObservables` is a `globalThis` cache keyed only by `observableId`, so
118-
// on a server it is shared by every concurrent request. Seeding the server snapshot from
119-
// it would let one request render data another request fetched for the same path.
120-
//
121-
// So no field below carries data across requests: `status`, `hasEmitted` and `data` come
122-
// from `config`, which is the caller's own input on this render. `firstValuePromise` does
123-
// read the shared `observable`, but it is a `Promise<void>` that resolves without a value,
124-
// so it discloses nothing. It is not optional on `ObservableStatus`, and the
125-
// `as ObservableStatus<T>` cast below means neither `tsc` nor the tests would notice if it
126-
// were dropped: a caller doing `status.firstValuePromise.then(...)` on the server would
127-
// just throw.
128-
//
129-
// The result is memoized per component instance because React compares the value it
130-
// returns across renders, and a fresh object each time is what triggers the
131-
// "The result of getServerSnapshot should be cached" error.
107+
// Reads only `config`, never `observable.immutableStatus`: `preloadedObservables` is a
108+
// `globalThis` cache keyed only by `observableId`, so a server shares it across concurrent
109+
// requests, and seeding from it would render one request's data into another's HTML.
110+
// The `as` cast below hides a missing `firstValuePromise` from `tsc` and the tests.
111+
// Held in a ref because React requires a stable value across renders.
132112
const serverSnapshotRef = React.useRef<ObservableStatus<T> | undefined>(undefined);
133113
const getServerSnapshot = React.useCallback<() => ObservableStatus<T>>(() => {
134114
if (serverSnapshotRef.current === undefined) {
@@ -145,10 +125,8 @@ export function useObservable<T = unknown>(observableId: string, source: Observa
145125
}
146126

147127
return serverSnapshotRef.current;
148-
// `config.initialData` and `config.startWithValue` are read above but deliberately left
149-
// out of the dependency array. Callers routinely pass a fresh `config` literal on every
150-
// render, so including them would rebuild this callback constantly, and the ref means
151-
// the value is computed once per component instance regardless.
128+
// Callers pass a fresh `config` literal each render, so the fields read above are kept
129+
// out of the deps; the ref computes the value once per instance anyway.
152130
// eslint-disable-next-line react-hooks/exhaustive-deps
153131
}, [observable, hasInitialData]);
154132

‎test/useObservable.test.tsx‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,18 @@ describe('useObservable', () => {
332332
});
333333

334334
describe('Server rendering', () => {
335-
// Renders `status` and `data` so the assertions read the snapshot React actually used,
336-
// rather than a value the test computed for itself.
335+
// Renders `status` and `data` so assertions read the snapshot React actually used.
337336
const Probe = ({ observableId, observable$, config }: { observableId: string; observable$: Subject<any>; config?: ReactFireOptions }) => {
338337
const { status, data } = useObservable(observableId, observable$, { suspense: false, ...config });
339-
// A single interpolated child, because adjacent JSX text nodes render with `<!-- -->`
340-
// separators between them and the assertions below match on the plain string.
338+
// One interpolated child: adjacent JSX text nodes render with `<!-- -->` between them.
341339
return <div>{`${status}:${String(data)}`}</div>;
342340
};
343341

344342
it('renders on the server instead of throwing', () => {
345343
const observable$: Subject<any> = new Subject();
346344

347-
// Without a getServerSnapshot, React throws "Missing getServerSnapshot, which is
348-
// required for server-rendered content" and the whole subtree falls back to client
349-
// rendering. This is the #748 regression test: delete the third argument to
350-
// useSyncExternalStore and this assertion fails.
345+
// The #748 regression test: delete the third argument to useSyncExternalStore and
346+
// this fails with "Missing getServerSnapshot".
351347
expect(() => renderToString(<Probe observableId="ssr-renders" observable$={observable$} />)).not.toThrow();
352348
});
353349

@@ -368,10 +364,8 @@ describe('useObservable', () => {
368364
});
369365

370366
it('does not leak a cached value from another request into the server snapshot', async () => {
371-
// `preloadedObservables` lives on `globalThis` and is keyed only by observableId, so on
372-
// a server every concurrent request shares it. A getServerSnapshot that read
373-
// `observable.immutableStatus` would render whatever the previous request left behind.
374-
// Here the first render stands in for that earlier request.
367+
// `preloadedObservables` is on `globalThis`, keyed only by observableId, so concurrent
368+
// server requests share it. The first render below stands in for an earlier request.
375369
const observable$: Subject<any> = new Subject();
376370
const observableId = 'ssr-no-cross-request-leak';
377371

@@ -386,10 +380,8 @@ describe('useObservable', () => {
386380
});
387381

388382
it('prefers the callers initialData over a value already in the shared cache', async () => {
389-
// The branch above that reads `initialData` is only reachable when the cache ALREADY
390-
// holds a value for this id: otherwise the overlay in `useObservable` sets status,
391-
// data and hasEmitted itself and the server snapshot never decides anything. So seed
392-
// the cache first, exactly as the leak test does, and only then pass `initialData`.
383+
// The `initialData` branch is only reachable when the cache already holds a value for
384+
// this id; otherwise `useObservable`'s overlay decides and the snapshot never does.
393385
const observable$: Subject<any> = new Subject();
394386
const observableId = 'ssr-initial-data-beats-cache';
395387

0 commit comments

Comments
 (0)