Skip to content

Fix RSC fetcher.load #13709

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

Merged
merged 4 commits into from
May 30, 2025
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
42 changes: 14 additions & 28 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,6 @@ export function createRouter(init: RouterInit): Router {
return;
}

let match = getTargetMatch(matches, path);
Copy link
Member Author

@markdalgleish markdalgleish May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the fog of war case, this match is completely ignored and is reassigned once route discovery is done. In an RSC world, if the the root route doesn't have a path, this line throws an error even though its result would never have been used.

To fix this, the creation of this match variable is delayed until after route discovery has completed and we know for a fact we have valid matches. This also means we no longer pass it as an argument to handleFetcherLoader and handleFetcherAction.

// Create a new context per fetch
let scopedContext = new unstable_RouterContextProvider(
init.unstable_getContext ? await init.unstable_getContext() : undefined
Expand All @@ -2270,7 +2269,6 @@ export function createRouter(init: RouterInit): Router {
key,
routeId,
path,
match,
matches,
scopedContext,
fogOfWar.active,
Expand All @@ -2288,7 +2286,6 @@ export function createRouter(init: RouterInit): Router {
key,
routeId,
path,
match,
matches,
scopedContext,
fogOfWar.active,
Expand All @@ -2304,7 +2301,6 @@ export function createRouter(init: RouterInit): Router {
key: string,
routeId: string,
path: string,
match: AgnosticDataRouteMatch,
requestMatches: AgnosticDataRouteMatch[],
scopedContext: unstable_RouterContextProvider,
isFogOfWar: boolean,
Expand All @@ -2315,23 +2311,6 @@ export function createRouter(init: RouterInit): Router {
interruptActiveLoads();
fetchLoadMatches.delete(key);

function detectAndHandle405Error(m: AgnosticDataRouteMatch) {
if (!m.route.action && !m.route.lazy) {
let error = getInternalRouterError(405, {
method: submission.formMethod,
pathname: path,
routeId: routeId,
});
setFetcherError(key, routeId, error, { flushSync });
return true;
}
return false;
}

if (!isFogOfWar && detectAndHandle405Error(match)) {
return;
}

// Put this fetcher into it's submitting state
let existingFetcher = state.fetchers.get(key);
updateFetcherState(key, getSubmittingFetcher(submission, existingFetcher), {
Expand Down Expand Up @@ -2369,14 +2348,21 @@ export function createRouter(init: RouterInit): Router {
return;
} else {
requestMatches = discoverResult.matches;
match = getTargetMatch(requestMatches, path);

if (detectAndHandle405Error(match)) {
return;
}
}
}

let match = getTargetMatch(requestMatches, path);

if (!match.route.action && !match.route.lazy) {
let error = getInternalRouterError(405, {
method: submission.formMethod,
pathname: path,
routeId: routeId,
});
setFetcherError(key, routeId, error, { flushSync });
return;
}
Comment on lines +2356 to +2364
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The detectAndHandle405Error function has been inlined now that this logic is only needed in a single place.


// Call the action for the fetcher
fetchControllers.set(key, abortController);

Expand Down Expand Up @@ -2619,7 +2605,6 @@ export function createRouter(init: RouterInit): Router {
key: string,
routeId: string,
path: string,
match: AgnosticDataRouteMatch,
matches: AgnosticDataRouteMatch[],
scopedContext: unstable_RouterContextProvider,
isFogOfWar: boolean,
Expand Down Expand Up @@ -2667,10 +2652,11 @@ export function createRouter(init: RouterInit): Router {
return;
} else {
matches = discoverResult.matches;
match = getTargetMatch(matches, path);
}
}

let match = getTargetMatch(matches, path);

// Call the loader for this fetcher route match
fetchControllers.set(key, abortController);

Expand Down
10 changes: 10 additions & 0 deletions playground/rsc-parcel/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export const routes = [
// @ts-expect-error
lazy: () => import("./routes/about/about"),
},
{
id: "fetcher",
path: "fetcher",
lazy: () => import("./routes/fetcher/fetcher"),
},
],
},
{
id: "resource",
path: "resource",
lazy: () => import("./routes/resource/resource"),
},
] satisfies ServerRouteObject[];
17 changes: 17 additions & 0 deletions playground/rsc-parcel/src/routes/fetcher/fetcher.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use client";

import { useFetcher } from "react-router";
import type { loader } from "../resource/resource";

export default function Fetcher() {
const fetcher = useFetcher<typeof loader>();

return (
<main>
<button onClick={() => fetcher.load("/resource")}>
Load fetcher data
</button>
<pre>{JSON.stringify(fetcher.data, null, 2)}</pre>
</main>
);
}
1 change: 1 addition & 0 deletions playground/rsc-parcel/src/routes/fetcher/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./fetcher.client";
6 changes: 6 additions & 0 deletions playground/rsc-parcel/src/routes/resource/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function loader() {
return {
timestamp: Date.now(),
message: "Hello from resource route!",
};
}
3 changes: 3 additions & 0 deletions playground/rsc-parcel/src/routes/root/root.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export default function Root() {
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/fetcher">Fetcher</Link>
</li>
</ul>
{counter}
<Outlet />
Expand Down