Parallel Server Function Throws #3832
-
Hey! I was wondering if someone could help me understand how I should be handling parallel server function throws (like I have created a very simple route (in the Basic example installed this way) to demonstrate a situation I'm handling poorly in my current TanStack Start application: // src/routes/throw-test.tsx
import { createFileRoute, redirect } from "@tanstack/react-router"
import { createServerFn } from "@tanstack/react-start"
const serverFnRedirect = createServerFn().handler(async () => {
throw redirect({
to: "/posts", // in the case of an auth middleware, would be a redirect to a login page or something
})
})
export const Route = createFileRoute("/throw-test")({
component: RouteComponent,
loader: async () => {
// Simulating parallel server function calls
;(async () => {
await new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, 2000)
})
await serverFnRedirect()
})()
await serverFnRedirect()
},
})
function RouteComponent() {
return <div>Hello "/throw-test"!</div>
} The idea is that in a route loader I want to fetch multiple resources in parallel (a'la TanStack Query’s In the current example, the user is navigated to "/posts", then the delayed redirect is treated as an error (logged to the console and in the case of TanStack Query with Suspense, as an uncaught error during hydration). While this example is clear and easy to fix, in real applications these throws can lead to race conditions and irregular errors—especially in cases where auth middleware only redirects when tokens expire, making it easy for issues to be overlooked in development and then surface in production. I want to:
Note: I've seen this Handling Errors section in the docs, but based on the wording it doesn't seem like I should be doing this for I've been using Start for about a month and have loved it so far. I really think this project is headed in the right direction and can't say enough good things about all the TanStack tooling. Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After some digging around I found something that works for me. The Still new to router + start and I think I tried to do too much at once and confused myself. |
Beta Was this translation helpful? Give feedback.
After some digging around I found something that works for me. The
@tanstack/react-router-with-query
package ended up being the solution I needed: a relatively new addition to the start docs + examples, but provides a very useful query client that automatically handles thethrow redirect
stuff without needing to douseSeverFn
everywhere. Probably just missed this somewhere in the docs. The example I provided still works to throw an error to the browser, but I think that makes sense?Still new to router + start and I think I tried to do too much at once and confused myself.