loader vs beforeLoad
What is the difference between them? Which circumstances should I use one for another?
11 Replies
other-emerald•17mo ago
beforeLoad - should be for performing any preflight checks.
like - is this user authenticated, does the user have permissions to access this page.
Try to do synchronous work here, i'd say.
loader - should be for actually kicking off the requests needed for the page content to be shown at all.
like - data from /posts/1 to display on the page
Async work is what I'd throw in here, as its meant to hold up the user till it has data available to show
absent-sapphire•17mo ago
Other differences I've found worth noting.. The
beforeLoad
functions in a route chain run in series while the loader
functions all run simultaneously (once beforeLoad
s are completed). A promise returned from beforeLoad
will block everything else until it resolves.
throwing
from beforeLoad
will prevent the rest of the route from running, eg throe redirect()
.
You can also add to the router context by returning an object from beforeLoad
which will be merged into existing context and be accessible by route functions down the line.extended-salmonOP•17mo ago
thanks guys
optimistic-gold•16mo ago
FYI, the docs have this documented in the Data Loading page. When I was looking for this info it took me a bit of digging to find it (I was expecting the routing lifecycle to be documented in the overview sections or in a page dedicated to the lifecycle).
other-emerald•16mo ago
What'd be a better place for it?.
The overview section needs to be kept really short. The topic itself is too sparse to warrant its own dedicated page.
optimistic-gold•16mo ago
@Sean Cassiere I feel like a dedicated page under the "Getting Started" heading that describes the entire "Routing Lifecycle" would be very helpful. There are bits of the lifecycle spread out over several pages. I am still getting a hang of tanstack (only been using it for about 3 weeks now, coming from react router and swr). I didn't meant for my comment to sound like a criticism (technical documentation is super tricky to write), just that it wasn't super clear when I was looking for it 🙂. Last night I was thinking about how I might be able to contribute to the documentation to help make it a little more clear.
Here is a (very old) poster which describes ASP.NET WebAPI 2's request lifecycle. Something along these lines would help so much. And here is another for ASP.NET MVC's request lifecycle.
Sorry for being a bit off topic on this thread 🙂
other-emerald•16mo ago
Feel free!
We are definitely open to any feedback on our docs.
Ours can feel abit expansive.
We do try to keep the initial pages a bit short, so people don't find it immediately too daunting.
optimistic-gold•16mo ago
Totally understand that. I enjoy writing docs so I might give it a go and create a PR to get feedback. Thanks for working on something awesome and giving it out for free! 🙂
other-emerald•16mo ago
We follow in Sir Tan's footsteps 😂
optimistic-gold•16mo ago
foreign-sapphire•16mo ago
Is there a difference between
throw
ing in a loader vs beforeLoad. Specifically throw redirect
? Does it produce the same effects in either?