Sergio Xalambrí

react-router@7.0.0
View as Markdown

Tutorial

Prevent the React Router Loader to Run after Document Request

Let's say you're building a route that needs a loader, but you only want this loader to really run once on the document request, for a client-side navigation from another route you most likely already have the loader data for any reason and want to avoid React Router to fetch the loader

You can export a clientLoader function from your route to control when the server loader runs, which can be never.

// app/routes/something.ts
export async function loader(args: LoaderFunctionArgs) { // some code that probably take a lot to run } export function clientLoader() { return null }

With this simple clientLoader function, we can ensure that the server loader will run on a document request only.

Do you like my content?

Your sponsorship helps me create more tutorials, articles, and open-source tools.

Sponsor me on GitHub