Skip to content

Commit 2352aaa

Browse files
Fix RSC fetcher.load (#13709)
1 parent 17b9b60 commit 2352aaa

File tree

6 files changed

+51
-28
lines changed

6 files changed

+51
-28
lines changed

packages/react-router/lib/router/router.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,7 +2258,6 @@ export function createRouter(init: RouterInit): Router {
22582258
return;
22592259
}
22602260

2261-
let match = getTargetMatch(matches, path);
22622261
// Create a new context per fetch
22632262
let scopedContext = new unstable_RouterContextProvider(
22642263
init.unstable_getContext ? await init.unstable_getContext() : undefined
@@ -2270,7 +2269,6 @@ export function createRouter(init: RouterInit): Router {
22702269
key,
22712270
routeId,
22722271
path,
2273-
match,
22742272
matches,
22752273
scopedContext,
22762274
fogOfWar.active,
@@ -2288,7 +2286,6 @@ export function createRouter(init: RouterInit): Router {
22882286
key,
22892287
routeId,
22902288
path,
2291-
match,
22922289
matches,
22932290
scopedContext,
22942291
fogOfWar.active,
@@ -2304,7 +2301,6 @@ export function createRouter(init: RouterInit): Router {
23042301
key: string,
23052302
routeId: string,
23062303
path: string,
2307-
match: AgnosticDataRouteMatch,
23082304
requestMatches: AgnosticDataRouteMatch[],
23092305
scopedContext: unstable_RouterContextProvider,
23102306
isFogOfWar: boolean,
@@ -2315,23 +2311,6 @@ export function createRouter(init: RouterInit): Router {
23152311
interruptActiveLoads();
23162312
fetchLoadMatches.delete(key);
23172313

2318-
function detectAndHandle405Error(m: AgnosticDataRouteMatch) {
2319-
if (!m.route.action && !m.route.lazy) {
2320-
let error = getInternalRouterError(405, {
2321-
method: submission.formMethod,
2322-
pathname: path,
2323-
routeId: routeId,
2324-
});
2325-
setFetcherError(key, routeId, error, { flushSync });
2326-
return true;
2327-
}
2328-
return false;
2329-
}
2330-
2331-
if (!isFogOfWar && detectAndHandle405Error(match)) {
2332-
return;
2333-
}
2334-
23352314
// Put this fetcher into it's submitting state
23362315
let existingFetcher = state.fetchers.get(key);
23372316
updateFetcherState(key, getSubmittingFetcher(submission, existingFetcher), {
@@ -2369,14 +2348,21 @@ export function createRouter(init: RouterInit): Router {
23692348
return;
23702349
} else {
23712350
requestMatches = discoverResult.matches;
2372-
match = getTargetMatch(requestMatches, path);
2373-
2374-
if (detectAndHandle405Error(match)) {
2375-
return;
2376-
}
23772351
}
23782352
}
23792353

2354+
let match = getTargetMatch(requestMatches, path);
2355+
2356+
if (!match.route.action && !match.route.lazy) {
2357+
let error = getInternalRouterError(405, {
2358+
method: submission.formMethod,
2359+
pathname: path,
2360+
routeId: routeId,
2361+
});
2362+
setFetcherError(key, routeId, error, { flushSync });
2363+
return;
2364+
}
2365+
23802366
// Call the action for the fetcher
23812367
fetchControllers.set(key, abortController);
23822368

@@ -2619,7 +2605,6 @@ export function createRouter(init: RouterInit): Router {
26192605
key: string,
26202606
routeId: string,
26212607
path: string,
2622-
match: AgnosticDataRouteMatch,
26232608
matches: AgnosticDataRouteMatch[],
26242609
scopedContext: unstable_RouterContextProvider,
26252610
isFogOfWar: boolean,
@@ -2667,10 +2652,11 @@ export function createRouter(init: RouterInit): Router {
26672652
return;
26682653
} else {
26692654
matches = discoverResult.matches;
2670-
match = getTargetMatch(matches, path);
26712655
}
26722656
}
26732657

2658+
let match = getTargetMatch(matches, path);
2659+
26742660
// Call the loader for this fetcher route match
26752661
fetchControllers.set(key, abortController);
26762662

playground/rsc-parcel/src/routes.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ export const routes = [
1717
// @ts-expect-error
1818
lazy: () => import("./routes/about/about"),
1919
},
20+
{
21+
id: "fetcher",
22+
path: "fetcher",
23+
lazy: () => import("./routes/fetcher/fetcher"),
24+
},
2025
],
2126
},
27+
{
28+
id: "resource",
29+
path: "resource",
30+
lazy: () => import("./routes/resource/resource"),
31+
},
2232
] satisfies ServerRouteObject[];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use client";
2+
3+
import { useFetcher } from "react-router";
4+
import type { loader } from "../resource/resource";
5+
6+
export default function Fetcher() {
7+
const fetcher = useFetcher<typeof loader>();
8+
9+
return (
10+
<main>
11+
<button onClick={() => fetcher.load("/resource")}>
12+
Load fetcher data
13+
</button>
14+
<pre>{JSON.stringify(fetcher.data, null, 2)}</pre>
15+
</main>
16+
);
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./fetcher.client";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function loader() {
2+
return {
3+
timestamp: Date.now(),
4+
message: "Hello from resource route!",
5+
};
6+
}

playground/rsc-parcel/src/routes/root/root.client.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export default function Root() {
3939
<li>
4040
<Link to="/about">About</Link>
4141
</li>
42+
<li>
43+
<Link to="/fetcher">Fetcher</Link>
44+
</li>
4245
</ul>
4346
{counter}
4447
<Outlet />

0 commit comments

Comments
 (0)