Skip to content

Commit 77c881c

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

2 files changed

Lines changed: 21 additions & 42 deletions

File tree

‎src/useObservable.ts‎

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,20 @@ 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.
107+
// The third argument is required for server-rendered and hydrating trees; without it
108+
// React throws "Missing getServerSnapshot" and the subtree falls back to client rendering.
111109
//
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.
110+
// It deliberately does not return `observable.immutableStatus` the way `getSnapshot` does:
111+
// `preloadedObservables` is a `globalThis` cache keyed only by `observableId`, so a server
112+
// shares it across concurrent requests, and seeding from it would render one request's data
113+
// into another's HTML. Every field below comes from `config`, the caller's input on this
114+
// render. `firstValuePromise` is the one exception, and it resolves without a value.
115115
//
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.
116+
// Note the `as ObservableStatus<T>` cast hides a missing `firstValuePromise` from both
117+
// `tsc` and the tests; a server-side `.then(...)` would throw.
120118
//
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.
119+
// Memoized per instance because React compares the returned value across renders, and a
120+
// fresh object each time triggers "The result of getServerSnapshot should be cached".
132121
const serverSnapshotRef = React.useRef<ObservableStatus<T> | undefined>(undefined);
133122
const getServerSnapshot = React.useCallback<() => ObservableStatus<T>>(() => {
134123
if (serverSnapshotRef.current === undefined) {
@@ -145,10 +134,8 @@ export function useObservable<T = unknown>(observableId: string, source: Observa
145134
}
146135

147136
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.
137+
// `config` fields are read above but kept out of the deps: callers routinely pass a
138+
// fresh literal each render, and the ref computes the value once per instance anyway.
152139
// eslint-disable-next-line react-hooks/exhaustive-deps
153140
}, [observable, hasInitialData]);
154141

‎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)