Skip to content

Commit d879db7

Browse files
committed
Move revalidation logic out of single-fetch strategy
1 parent 9c211ae commit d879db7

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

packages/react-router/lib/dom/ssr/single-fetch.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,9 @@ async function singleFetchLoaderNavigationStrategy(
385385
getRouteInfo(m);
386386

387387
let defaultShouldRevalidate =
388-
m.unstable_shouldRevalidateArgs?.defaultShouldRevalidate !== false &&
389-
(!m.unstable_shouldRevalidateArgs ||
390-
m.unstable_shouldRevalidateArgs.actionStatus == null ||
391-
m.unstable_shouldRevalidateArgs.actionStatus < 400);
388+
!m.unstable_shouldRevalidateArgs ||
389+
m.unstable_shouldRevalidateArgs.actionStatus == null ||
390+
m.unstable_shouldRevalidateArgs.actionStatus < 400;
392391
let shouldCall = m.unstable_shouldCallHandler(defaultShouldRevalidate);
393392

394393
if (!shouldCall) {

packages/react-router/lib/router/router.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4869,9 +4869,7 @@ function getMatchesToLoad(
48694869
let actionStatus = pendingActionResult
48704870
? pendingActionResult[1].statusCode
48714871
: undefined;
4872-
let shouldSkipRevalidation =
4873-
(actionStatus && actionStatus >= 400) ||
4874-
callSiteDefaultShouldRevalidate === false;
4872+
let shouldSkipRevalidation = actionStatus && actionStatus >= 400;
48754873

48764874
let baseShouldRevalidateArgs = {
48774875
currentUrl,
@@ -4925,19 +4923,28 @@ function getMatchesToLoad(
49254923
);
49264924
}
49274925

4928-
// This is the default implementation for when we revalidate. If the route
4929-
// provides it's own implementation, then we give them full control but
4930-
// provide this value so they can leverage it if needed after they check
4931-
// their own specific use cases
4932-
let defaultShouldRevalidate = shouldSkipRevalidation
4933-
? false
4934-
: // Forced revalidation due to submission, useRevalidator, or X-Remix-Revalidate
4935-
isRevalidationRequired ||
4936-
currentUrl.pathname + currentUrl.search ===
4937-
nextUrl.pathname + nextUrl.search ||
4938-
// Search params affect all loaders
4939-
currentUrl.search !== nextUrl.search ||
4940-
isNewRouteInstance(state.matches[index], match);
4926+
let defaultShouldRevalidate = false;
4927+
if (callSiteDefaultShouldRevalidate != null) {
4928+
// Use callsite value verbatim if provided
4929+
defaultShouldRevalidate = callSiteDefaultShouldRevalidate;
4930+
} else if (shouldSkipRevalidation) {
4931+
// Skip due to 4xx/5xx action result
4932+
defaultShouldRevalidate = false;
4933+
} else if (isRevalidationRequired) {
4934+
// Forced revalidation due to submission, useRevalidator, or X-Remix-Revalidate
4935+
defaultShouldRevalidate = true;
4936+
} else if (
4937+
currentUrl.pathname + currentUrl.search ===
4938+
nextUrl.pathname + nextUrl.search
4939+
) {
4940+
// Same URL - mimic a hard reload
4941+
defaultShouldRevalidate = true;
4942+
} else if (currentUrl.search !== nextUrl.search) {
4943+
// Search params affect all loaders
4944+
defaultShouldRevalidate = true;
4945+
} else if (isNewRouteInstance(state.matches[index], match)) {
4946+
defaultShouldRevalidate = true;
4947+
}
49414948
let shouldRevalidateArgs = {
49424949
...baseShouldRevalidateArgs,
49434950
defaultShouldRevalidate,
@@ -4953,6 +4960,7 @@ function getMatchesToLoad(
49534960
scopedContext,
49544961
shouldLoad,
49554962
shouldRevalidateArgs,
4963+
callSiteDefaultShouldRevalidate,
49564964
);
49574965
});
49584966

@@ -5848,6 +5856,7 @@ function getDataStrategyMatch(
58485856
scopedContext: unknown,
58495857
shouldLoad: boolean,
58505858
unstable_shouldRevalidateArgs: DataStrategyMatch["unstable_shouldRevalidateArgs"] = null,
5859+
callSiteDefaultShouldRevalidate?: boolean,
58515860
): DataStrategyMatch {
58525861
// The hope here is to avoid a breaking change to the resolve behavior.
58535862
// Opt-ing into the `unstable_shouldCallHandler` API changes some nuanced behavior
@@ -5873,6 +5882,13 @@ function getDataStrategyMatch(
58735882
return shouldLoad;
58745883
}
58755884

5885+
if (typeof callSiteDefaultShouldRevalidate === "boolean") {
5886+
return shouldRevalidateLoader(match, {
5887+
...unstable_shouldRevalidateArgs,
5888+
defaultShouldRevalidate: callSiteDefaultShouldRevalidate,
5889+
});
5890+
}
5891+
58765892
if (typeof defaultShouldRevalidate === "boolean") {
58775893
return shouldRevalidateLoader(match, {
58785894
...unstable_shouldRevalidateArgs,

0 commit comments

Comments
 (0)