Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve lazy route module import errors during SPA navigations instead of replacing them with a missing `dataStrategy` result error
32 changes: 32 additions & 0 deletions packages/react-router/__tests__/router/data-strategy-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
DataStrategyMatch,
DataStrategyResult,
} from "../../lib/router/utils";
import { getSingleFetchDataStrategyImpl } from "../../lib/dom/ssr/single-fetch";
import {
createDeferred,
createAsyncStub,
Expand Down Expand Up @@ -173,6 +174,37 @@ describe("router dataStrategy", () => {
);
});

it("preserves lazy route errors in the non-SSR single-fetch strategy", async () => {
let lazyError = new Error("Unable to load lazy route module");
let [lazy, lazyDeferred] = createAsyncStub();
let t = setup({
routes: [
{
path: "/",
},
{
id: "lazy",
path: "/lazy",
lazy,
ErrorBoundary: () => null,
},
],
dataStrategy: getSingleFetchDataStrategyImpl(
() => t.router,
() => ({ hasLoader: false, hasClientLoader: false }),
async () => {
throw new Error("Unexpected single-fetch request");
},
false,
),
});

await t.navigate("/lazy");
await lazyDeferred.reject(lazyError);

expect(t.router.state.errors).toEqual({ lazy: lazyError });
});

it("should allow custom implementations to override default behavior", async () => {
let t = setup({
routes: [
Expand Down
35 changes: 15 additions & 20 deletions packages/react-router/lib/dom/ssr/single-fetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,26 +314,21 @@ async function nonSsrStrategy(
let matchesToLoad = args.matches.filter((m) => m.shouldCallHandler());
let results: Record<string, DataStrategyResult> = {};
await Promise.all(
matchesToLoad.map((m) =>
m.resolve(async (handler) => {
try {
let { hasClientLoader } = getRouteInfo(m);
// Need to pass through a `singleFetch` override handler so
// clientLoader's can still call server loaders through `.data`
// requests
let routeId = m.route.id;
let result = hasClientLoader
? await handler(async () => {
let { data } = await fetchAndDecode(args, [routeId]);
return unwrapSingleFetchResult(data, routeId);
})
: await handler();
results[m.route.id] = { type: "data", result };
} catch (e) {
results[m.route.id] = { type: "error", result: e };
}
}),
),
matchesToLoad.map(async (m) => {
let routeId = m.route.id;
results[routeId] = await m.resolve(async (handler) => {
let { hasClientLoader } = getRouteInfo(m);
// Need to pass through a `singleFetch` override handler so
// clientLoader's can still call server loaders through `.data`
// requests
return hasClientLoader
? handler(async () => {
let { data } = await fetchAndDecode(args, [routeId]);
return unwrapSingleFetchResult(data, routeId);
})
: handler();
});
}),
);
return results;
}
Expand Down