react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Add support for fetcher.abort()

Open brophdawg11 opened this issue 1 year ago • 1 comments

This has primarily 2 use cases:

  • When using v7_fetcherPersist, we lost the ability to actually cancel/abort fetches if a fetcher was unmounted, so this gives the users a programmatic API to achieve the previous behavior if necessary
  • When "streaming" data via the user of promises, once the fetcher has returned to "idle" there's no good way to abort/cancel the ongoing streaming data since we only tracked the controller in the router while the fetcher was in a non-idle state. This keeps those controllers around inside the router until the fetcher is deleted so that they can be programatically aborted after returning to idle

When calling abortFetcher, we simply abort the controller which will proxy through to the request.signal in the loader so you can use that to reject the promises and trigger the <Await errorElement>:

function loader({ request }) {
  return {
    critical: "CRITICAL",
    lazy: new Promise((resolve, reject) => {
      request.signal.addEventListener("abort", () =>
        reject(request.signal.reason)
      );
      setTimeout(() => resolve("LAZY"), 3000);
    }),
  };
}

brophdawg11 avatar Sep 11 '24 20:09 brophdawg11

🦋 Changeset detected

Latest commit: 22da2428aa23cfe3a0d925e754cc2246ee9d977c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
react-router-dom Minor
@remix-run/router Minor
react-router Minor
react-router-dom-v5-compat Minor
react-router-native Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Sep 11 '24 20:09 changeset-bot[bot]

  • Get updated to latest dev
  • Remove abort
  • Make reset abort if in flight
  • Add unstable_ for an alpha release

brophdawg11 avatar Aug 19 '25 20:08 brophdawg11

Closed in favor of https://github.com/remix-run/react-router/pull/14206

brophdawg11 avatar Aug 20 '25 19:08 brophdawg11