Additional parameters for shouldRevalidate to refetch stale data #10429
Replies: 2 comments 4 replies
-
Instead of adding "cache-like" ability to async function loader(request) {
if (cache.has(request.url)) {
return cache.get(request.url);
}
let data = await getExpensiveData();
return data;
} |
Beta Was this translation helpful? Give feedback.
-
Hi, I also think that Imagine a loader returning data that can be either static or dynamic depending on params for example import { ShouldRevalidateFunctionArgs } from 'react-router';
import { Route } from './+types/conditionallyRevalidatedRoute';
export async function loader({ params: { id } }: Route.LoaderArgs) {
const n = Number(id);
return n % 2 === 0
? {
shouldRevalidate: true,
value: Math.floor(Math.random() * 100) + n
}
: { shouldRevalidate: false, value: 42 + n };
}
export default function ConditionallyRevalidatedRoute({
loaderData
}: Route.ComponentProps) {
return <div>{JSON.stringify(loaderData)}</div>;
} It would be very suitable to define the export function shouldRevalidate({
currentParams,
nextParams,
loaderData,
defaultShouldRevalidate
}: Route.ShouldRevalidateArgs) {
if (currentParams.id !== nextParams.id) {
return true;
}
return loaderData.shouldRevalidate && defaultShouldRevalidate;
} The proposed workaround (using a clientLoader and a cache) is not really suitable since RR7 in framework mode and singleFetched server loader functions. |
Beta Was this translation helpful? Give feedback.
-
hi. I'm running a service that needs to cover a large amount of traffic, and I'm going through a technical review to apply react-router (I want to switch from universal-router to react-router).
I'm looking for ways to reduce page load time, and I'm proposing an additional feature. Before I say something, I'm not sure if this is in line with the direction I want to go, so I'll start with some context.
The idea is that when a page is navigated to a CSR, the goal is to reuse data already received from the loader if it's not out of date.
To refetch a loader holding stale data,
I expected shouldRevalidate, which is provided by the loader, to provide this functionality, but it doesn't.
So I came up with a few ideas for what data we could provide to shouldRevalidate.
Providing additional data to shouldRevalidate would be a major change in the current flow of library development, but I think it's a necessary feature for production use in high traffic environments.
Not only for the backend API traffic that comes from moving every page to the CSR, but also to improve usability for site visitors by reusing slightly heavier APIs that are already called.
If you have any suggestions, even if they're small, I'd love to hear them.
Thank you :)
Beta Was this translation helpful? Give feedback.
All reactions