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

Fix a race-condition with loader/action-thrown errors on route.lazy routes #10778

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/route-lazy-race.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix a race-condition with loader/action-thrown errors on route.lazy routes
39 changes: 39 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13731,6 +13731,45 @@ describe("a router", () => {
);
consoleWarn.mockReset();
});

it("handles errors thrown from static loaders before lazy has completed", async () => {
let consoleWarn = jest.spyOn(console, "warn");
let t = setup({
routes: [
{
id: "root",
path: "/",
children: [
{
id: "lazy",
path: "lazy",
loader: true,
lazy: true,
},
],
},
],
});

let A = await t.navigate("/lazy");

await A.loaders.lazy.reject("STATIC LOADER ERROR");
expect(t.router.state.navigation.state).toBe("loading");

// We shouldn't bubble the loader error until after this resolves
// so we know if it has a boundary or not
await A.lazy.lazy.resolve({
hasErrorBoundary: true,
});
expect(t.router.state.location.pathname).toBe("/lazy");
expect(t.router.state.navigation.state).toBe("idle");
expect(t.router.state.loaderData).toEqual({});
expect(t.router.state.errors).toEqual({
lazy: "STATIC LOADER ERROR",
});

consoleWarn.mockReset();
});
});

describe("interruptions", () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3643,10 +3643,19 @@ async function callLoaderOrAction(
if (match.route.lazy) {
if (handler) {
// Run statically defined handler in parallel with lazy()
let handlerError;
let values = await Promise.all([
runHandler(handler),
// If the handler throws, don't let it immediately bubble out,
// since we need to let the lazy() execution finish so we know if this
// route has a boundary that can handle the error
runHandler(handler).catch((e) => {
handlerError = e;
}),
loadLazyRouteModule(match.route, mapRouteProperties, manifest),
]);
if (handlerError) {
throw handlerError;
}
result = values[0];
} else {
// Load lazy route module, then run any returned handler
Expand Down