-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Single Fetch throwing response does not work #9793
Comments
If you scroll, up in the console you will see this error after submitting:
This is standard behavior in all Remix routes. When an In your case, you have a This is a common enough occurrence that there's a section in the docs, see the "A note on If you need to display root loader data in |
@brophdawg11 Thanks for your explanation, however, I am throwing the response stub which per the docs is a way to the short circuit actions/loaders to immediately respond to the incoming request. So what is happening here is that an error is incorrectly thrown by Remix, while it should have just sent a normal response |
How so? Your code is throwing the response stub - and any time you Can you point to the section of the docs that is causing the confusion? |
That is not the behavior in non-single-fetch mode when throwing You can also throw these response stubs to short circuit the flow of your loaders and actions: export const loader = defineLoader(
({ request, response }) => {
if (shouldRedirectToHome(request)) {
response.status = 302;
response.headers.set("Location", "/");
throw response;
}
// ...
}
); |
Redirects are different than errors since you're telling the router to navigate to a new route, as opposed to informing it of an error on the current route. You're not throwing a |
You're right, I've only ever thrown with redirect before so didn't realize it worked this way. I notice though that single fetch's types override remix/packages/remix-react/future/single-fetch.d.ts Lines 27 to 29 in ac747ff
Edit: after testing this out it seems like useRouteLoaderData is now always set, so while useLoaderData becomes undefined on error useRouteLoaderData retains its value from before the error |
Ah thanks for pointing this out - this is a bug. In your case |
How exactly is the behavior different between the two, and for what reason? It’s not clear from the docs. I only seitched to it for the | ubdefined and was surprised it never became undefined during errors unlike useLoaderData |
|
Reproduction
https://stackblitz.com/edit/remix-run-remix-ndbnrb?file=app%2Froutes%2F_index.tsx,app%2Froot.tsx,tsconfig.json
System Info
Used Package Manager
npm
Expected Behavior
Loader states should always stay consistent. E.g.
useLoaderData<typeof loader>
should always contain the latest valid loader dataActual Behavior
Loader state isnt consistent when using
throw response
with single fetch. It becomes undefined unexpectedly leading to destructuring errors (see stackblitz)The text was updated successfully, but these errors were encountered: