Loading destination details under React Suspense, with cancellation of an abandoned load. Domain:
travel search. This is the Suspense sibling of app-react, which uses the non-Suspense
usePromiseState path instead.
Pick a destination and its details load under a Suspense fallback. Pick another before the first
finishes and the abandoned request should stop, not run to completion in the background. The -canc
flavor does exactly that: the abandoned request aborts (an aborted marker in the mock call log).
The -vanilla flavor cannot, so the abandoned request finishes anyway.
The example consumes the built dist of each @cancjs/* package through a npm file:. Build the
monorepo first, then install this workspace:
cd ../../ # monorepo root (canc)
npm run build
cd examples
npm install
Two flavors, each its own Vite dev server and HTML entry:
npm run dev:canc --workspace=app-react-suspense # canc.html + src/main-canc.tsx
npm run dev:vanilla --workspace=app-react-suspense # vanilla.html + src/main-vanilla.tsx
npm run test --workspace=app-react-suspense
npm run typecheck --workspace=app-react-suspense
start:canc / start:vanilla are aliases of the dev:* scripts. Open the browser console to watch
the mock API log which detail requests start, complete, and abort.
React.use(promise) suspends the tree until the promise settles, which is what shows the fallback.
But a component that suspends on first render never commits, so its own effects never run. That
means the intuitive fix (create the resource in the reader and cancel it in the reader's cleanup
effect) does not work: when the reader is abandoned during the fallback, the cleanup that would
cancel it is never scheduled, and the request runs to completion.
Cancellation has to live in a component that commits. This example shows three panels:
DetailPanel-canc.tsx(correct). The resource is created above the boundary, inuseCancelableResource, and handed to aCancelableSuspenseboundary. The boundary commits and stays mounted while the reader inside it suspends, so its cleanup effect runs when the resource is superseded or the boundary unmounts, and cancels the in-flight request.NaiveDetailPanel-canc.tsx(the leak we teach). The reader creates aCancelablePromiseand tries to cancel it in its own cleanup effect (useCancelableSuspense). Because it suspends before it ever commits, that effect never runs, and the abandoned load completes anyway.DetailPanel-vanilla.tsx. A plainuse(api.destinationDetails(id)). A plain Promise has no cancel, so the abandoned load always completes.
src/DetailPanel-vanilla.tsxvssrc/DetailPanel-canc.tsx: the plain-Suspense panel vs the cancel-aware boundary.src/NaiveDetailPanel-canc.tsx: the in-child-cancel attempt that leaks. Read it next toDetailPanel-canc.tsxto see why the boundary, not the reader, has to own the cancel.
src/lib/ holds the Suspense helpers this example prototypes. They carry no example-specific code
and are the seed of a future @cancjs/react package. Copy them freely.
useCancelableResource(factory, deps): render-as-you-fetch. Starts the request and returns a stableCancelablePromise(memoized perdeps, whichReact.userequires) to hand to a boundary.CancelableSuspense: a Suspense boundary that cancels its resource on unmount or when a new resource supersedes it. Pass a resource fromuseCancelableResource; the render prop receives the resolved value.useCancelableSuspense(factory, deps): the in-child hook kept only as the counter-example. It leaks; useCancelableSuspenseinstead.
- What cancellation stops here: the details request is simulated by the shared mock API, which
honors an
AbortSignal. Canceling the resource aborts the in-flight request at that boundary (anabortedmarker in the call log). A real backend needs its own request to be abortable (fetch with a signal) for the cancel to reach the wire. - React.use needs a stable promise. A fresh promise on every render would suspend forever, so
useCancelableResourcememoizes it perdeps. Cancel-on-abandon needs a committing owner (the boundary), not the suspending child. src/mock/is scaffolding. Pretend it is your API. It is not meant to be copied.