-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
Fix RSC fetcher.load
#13709
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2258,7 +2258,6 @@ export function createRouter(init: RouterInit): Router { | |
return; | ||
} | ||
|
||
let match = getTargetMatch(matches, path); | ||
// Create a new context per fetch | ||
let scopedContext = new unstable_RouterContextProvider( | ||
init.unstable_getContext ? await init.unstable_getContext() : undefined | ||
|
@@ -2270,7 +2269,6 @@ export function createRouter(init: RouterInit): Router { | |
key, | ||
routeId, | ||
path, | ||
match, | ||
matches, | ||
scopedContext, | ||
fogOfWar.active, | ||
|
@@ -2288,7 +2286,6 @@ export function createRouter(init: RouterInit): Router { | |
key, | ||
routeId, | ||
path, | ||
match, | ||
matches, | ||
scopedContext, | ||
fogOfWar.active, | ||
|
@@ -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, | ||
|
@@ -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), { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
// Call the action for the fetcher | ||
fetchControllers.set(key, abortController); | ||
|
||
|
@@ -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, | ||
|
@@ -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); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
playground/rsc-parcel/src/routes/fetcher/fetcher.client.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./fetcher.client"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!", | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 tohandleFetcherLoader
andhandleFetcherAction
.