Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add future flag to throw request.signal.reason for aborted requests #11104

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Note about DOMException not existing in Node 16
  • Loading branch information
brophdawg11 committed Dec 8, 2023
commit 9463fb5eb1772b9ba0fc2958e18f11f1cfd40308
4 changes: 3 additions & 1 deletion .changeset/throw-abort-reason.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
"@remix-run/router": minor
---

Add `createStaticHandler` `future.v7_throwAbortReason` flag to instead throw `request.signal.reason` when a request is aborted instead of our own custom `new Error()` with a message such as `query() call aborted`/`queryRoute() call aborted`.
Add `createStaticHandler` `future.v7_throwAbortReason` flag to instead throw `request.signal.reason` (defaults to a `DOMException`) when a request is aborted instead of our own custom `new Error()` with a message such as `query() call aborted`/`queryRoute() call aborted`

- Please note that `DOMException` was added in Node v17 so you will not get a `DOMException` on Node 16 and below.
4 changes: 2 additions & 2 deletions docs/routers/create-static-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ These are the same `routes`/`basename` you would pass to [`createBrowserRouter`]

The `handler.query()` method takes in a Fetch request, performs route matching, and executes all relevant route action/loader methods depending on the request. The return `context` value contains all of the information required to render the HTML document for the request (route-level `actionData`, `loaderData`, `errors`, etc.). If any of the matched routes return or throw a redirect response, then `query()` will return that redirect in the form of Fetch `Response`.

If a request is aborted, `query` will throw an error such as `Error("query() call aborted")`. If you want to throw the native `AbortSignal.reason` (by default a `DOMException`) you can opt-in into the `future.v7_throwAbortReason` future flag.
If a request is aborted, `query` will throw an error such as `Error("query() call aborted")`. If you want to throw the native `AbortSignal.reason` (by default a `DOMException`) you can opt-in into the `future.v7_throwAbortReason` future flag. `DOMException` was added in Node 17 so you must be on Node 17 or higher for this to work properly.

### `opts.requestContext`

Expand Down Expand Up @@ -132,7 +132,7 @@ export async function render(req: express.Request) {

The `handler.queryRoute` is a more-targeted version that queries a singular route and runs it's loader or action based on the request. By default, it will match the target route based on the request URL. The return value is the values returned from the loader or action, which is usually a `Response` object.

If a request is aborted, `query` will throw an error such as `Error("query() call aborted")`. If you want to throw the native `AbortSignal.reason` (by default a `DOMException`) you can opt-in into the `future.v7_throwAbortReason` future flag.
If a request is aborted, `query` will throw an error such as `Error("query() call aborted")`. If you want to throw the native `AbortSignal.reason` (by default a `DOMException`) you can opt-in into the `future.v7_throwAbortReason` future flag. `DOMException` was added in Node 17 so you must be on Node 17 or higher for this to work properly.

### `opts.routeId`

Expand Down
24 changes: 20 additions & 4 deletions packages/router/__tests__/ssr-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,11 @@ describe("ssr", () => {
} catch (_e) {
e = _e;
}
expect(e).toBeInstanceOf(DOMException);
// DOMException added in node 17
if (process.versions.node.split(".").map(Number)[0] >= 17) {
// eslint-disable-next-line jest/no-conditional-expect
expect(e).toBeInstanceOf(DOMException);
}
expect(e.name).toBe("AbortError");
expect(e.message).toBe("This operation was aborted");
});
Expand Down Expand Up @@ -711,7 +715,11 @@ describe("ssr", () => {
} catch (_e) {
e = _e;
}
expect(e).toBeInstanceOf(DOMException);
// DOMException added in node 17
if (process.versions.node.split(".").map(Number)[0] >= 17) {
// eslint-disable-next-line jest/no-conditional-expect
expect(e).toBeInstanceOf(DOMException);
}
expect(e.name).toBe("AbortError");
expect(e.message).toBe("This operation was aborted");
});
Expand Down Expand Up @@ -2072,7 +2080,11 @@ describe("ssr", () => {
} catch (_e) {
e = _e;
}
expect(e).toBeInstanceOf(DOMException);
// DOMException added in node 17
if (process.versions.node.split(".").map(Number)[0] >= 17) {
// eslint-disable-next-line jest/no-conditional-expect
expect(e).toBeInstanceOf(DOMException);
}
expect(e.name).toBe("AbortError");
expect(e.message).toBe("This operation was aborted");
});
Expand Down Expand Up @@ -2102,7 +2114,11 @@ describe("ssr", () => {
} catch (_e) {
e = _e;
}
expect(e).toBeInstanceOf(DOMException);
// DOMException added in node 17
if (process.versions.node.split(".").map(Number)[0] >= 17) {
// eslint-disable-next-line jest/no-conditional-expect
expect(e).toBeInstanceOf(DOMException);
}
expect(e.name).toBe("AbortError");
expect(e.message).toBe("This operation was aborted");
});
Expand Down