Any way to access the server data from a client loader? #12862
-
My app does a server-side request that asks for cached data only. If there is a cache miss, I'd like the client loader to make a request instead, so the rest of the page has time to render in parallel. How could I accomplish this? I can't figure out if it's actually possible to access the serverData (see: #12851) I also don't want to always call import type { Route } from './+types/my-route';
export const loader = async({
context,
request,
params,
}: Route.LoaderArgs) => {
const cached_only = true;
return getData( cached_only );
}
export const clientLoader = async ({
request,
params,
serverLoader,
}: Route.ClientLoaderArgs) => {
if ( somehow serverData is empty) { 👈
return serverLoader();
}
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That |
Beta Was this translation helpful? Give feedback.
That's because
serverData
is usually the name you give to the return ofserverLoader
, as in:See: https://reactrouter.com/start/framework/route-module#clientloader
When you don't export a custom client loader, React Router will use a default one that works like the snippet above, calling the server loader for you.
When you export a custom client loader, you take control and it's up to you to decide whether you'll call the server loader or not. It seems you're expecting the server loader to run automatically even when you export a custom client l…