export async function action({ request }: Route.ActionArgs) { const formData = await request.formData(); const success = await updateProfile(formData); // Include a flag in the result return { success, shouldRevalidate: success };}export function shouldRevalidate({ actionResult, defaultShouldRevalidate,}: ShouldRevalidateFunctionArgs) { // Only revalidate if action was successful if (actionResult && "shouldRevalidate" in actionResult) { return actionResult.shouldRevalidate; } return defaultShouldRevalidate;}
Use the default behavior as a fallback to avoid missing important revalidations:
export function shouldRevalidate({ currentParams, nextParams, defaultShouldRevalidate,}: ShouldRevalidateFunctionArgs) { // Your custom logic if (currentParams.id === nextParams.id) { return false; } // ✅ Fall back to default return defaultShouldRevalidate;}
Be conservative with skipping revalidation
It’s better to revalidate unnecessarily than to show stale data:
// ❌ Too aggressive - might show stale dataexport function shouldRevalidate() { return false; // Never revalidate}// ✅ Conservative - only skip when safeexport function shouldRevalidate({ currentParams, nextParams, defaultShouldRevalidate,}) { // Only skip when we're certain data hasn't changed if (currentParams.id === nextParams.id && !formMethod) { return false; } return defaultShouldRevalidate;}
Use for expensive loaders
Only add shouldRevalidate when loader is computationally expensive:
Ensure your revalidation logic doesn’t break data updates:
// Test cases to verify:// 1. Navigation between different items// 2. Form submissions// 3. Search param changes// 4. Same URL clicks (refresh)// 5. Action success/failure
// Parent doesn't need to reload when child params changeexport function shouldRevalidate({ currentParams, nextParams,}: ShouldRevalidateFunctionArgs) { // Only revalidate if parent params changed return currentParams.parentId !== nextParams.parentId;}
// List route - don't revalidate when viewing detailsexport function shouldRevalidate({ currentUrl, nextUrl, formMethod,}: ShouldRevalidateFunctionArgs) { // Only revalidate if list was mutated if (formMethod && formMethod !== "GET") { return true; } // Don't revalidate when navigating to/from detail return false;}