Skip to content

Commit b95dff0

Browse files
committed
Refactor callsite revalidation optout to use defaultShouldRevalidate
1 parent 62e3f8f commit b95dff0

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

packages/react-router/__tests__/router/fetchers-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2436,7 +2436,7 @@ describe("fetchers", () => {
24362436
let C = await t.fetch("/tasks", actionKey, {
24372437
formMethod: "post",
24382438
formData: createFormData({}),
2439-
shouldRevalidate: false,
2439+
defaultShouldRevalidate: false,
24402440
});
24412441

24422442
expect(t.fetchers[actionKey]).toMatchObject({ state: "submitting" });

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ interface SharedSubmitOptions {
196196
/**
197197
* Determine if revalidation should occur post-submission.
198198
*/
199-
shouldRevalidate?: boolean | (() => boolean);
199+
defaultShouldRevalidate?: boolean;
200200
}
201201

202202
/**

packages/react-router/lib/dom/lib.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ interface SharedFormProps extends React.FormHTMLAttributes<HTMLFormElement> {
17921792
/**
17931793
* Determine if revalidation should occur post-submission.
17941794
*/
1795-
shouldRevalidate?: boolean | (() => boolean);
1795+
defaultShouldRevalidate?: boolean;
17961796
}
17971797

17981798
/**
@@ -1916,7 +1916,7 @@ type HTMLFormSubmitter = HTMLButtonElement | HTMLInputElement;
19161916
* @param {FormProps.replace} replace n/a
19171917
* @param {FormProps.state} state n/a
19181918
* @param {FormProps.viewTransition} viewTransition n/a
1919-
* @param {FormProps.shouldRevalidate} shouldRevalidate n/a
1919+
* @param {FormProps.defaultShouldRevalidate} defaultShouldRevalidate n/a
19201920
* @returns A progressively enhanced [`<form>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) component
19211921
*/
19221922
export const Form = React.forwardRef<HTMLFormElement, FormProps>(
@@ -1934,7 +1934,7 @@ export const Form = React.forwardRef<HTMLFormElement, FormProps>(
19341934
relative,
19351935
preventScrollReset,
19361936
viewTransition,
1937-
shouldRevalidate,
1937+
defaultShouldRevalidate,
19381938
...props
19391939
},
19401940
forwardedRef,
@@ -1967,7 +1967,7 @@ export const Form = React.forwardRef<HTMLFormElement, FormProps>(
19671967
relative,
19681968
preventScrollReset,
19691969
viewTransition,
1970-
shouldRevalidate,
1970+
defaultShouldRevalidate,
19711971
});
19721972
};
19731973

@@ -2549,6 +2549,8 @@ export function useSubmit(): SubmitFunction {
25492549

25502550
return React.useCallback<SubmitFunction>(
25512551
async (target, options = {}) => {
2552+
debugger;
2553+
console.log("useSubmit", target, options);
25522554
let { action, method, encType, formData, body } = getFormSubmissionInfo(
25532555
target,
25542556
basename,
@@ -2557,7 +2559,7 @@ export function useSubmit(): SubmitFunction {
25572559
if (options.navigate === false) {
25582560
let key = options.fetcherKey || getUniqueFetcherId();
25592561
await router.fetch(key, currentRouteId, options.action || action, {
2560-
shouldRevalidate: options.shouldRevalidate,
2562+
defaultShouldRevalidate: options.defaultShouldRevalidate,
25612563
preventScrollReset: options.preventScrollReset,
25622564
formData,
25632565
body,
@@ -2567,7 +2569,7 @@ export function useSubmit(): SubmitFunction {
25672569
});
25682570
} else {
25692571
await router.navigate(options.action || action, {
2570-
shouldRevalidate: options.shouldRevalidate,
2572+
defaultShouldRevalidate: options.defaultShouldRevalidate,
25712573
preventScrollReset: options.preventScrollReset,
25722574
formData,
25732575
body,

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

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

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

393394
if (!shouldCall) {

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ type BaseNavigateOrFetchOptions = {
524524
preventScrollReset?: boolean;
525525
relative?: RelativeRoutingType;
526526
flushSync?: boolean;
527-
shouldRevalidate?: boolean | (() => boolean);
527+
defaultShouldRevalidate?: boolean;
528528
};
529529

530530
// Only allowed for navigations
@@ -2366,13 +2366,8 @@ export function createRouter(init: RouterInit): Router {
23662366
let preventScrollReset = (opts && opts.preventScrollReset) === true;
23672367

23682368
if (submission && isMutationMethod(submission.formMethod)) {
2369-
let shouldRevalidate =
2370-
opts && "shouldRevalidate" in opts
2371-
? typeof opts.shouldRevalidate === "function"
2372-
? opts.shouldRevalidate()
2373-
: // undefined should eval to true
2374-
opts.shouldRevalidate !== false
2375-
: true;
2369+
let callSiteDefaultShouldRevalidate =
2370+
opts?.defaultShouldRevalidate !== false;
23762371
await handleFetcherAction(
23772372
key,
23782373
routeId,
@@ -2383,7 +2378,7 @@ export function createRouter(init: RouterInit): Router {
23832378
flushSync,
23842379
preventScrollReset,
23852380
submission,
2386-
shouldRevalidate,
2381+
callSiteDefaultShouldRevalidate,
23872382
);
23882383
return;
23892384
}
@@ -2401,6 +2396,7 @@ export function createRouter(init: RouterInit): Router {
24012396
flushSync,
24022397
preventScrollReset,
24032398
submission,
2399+
// defaultShouldRevalidate, // todo
24042400
);
24052401
}
24062402

@@ -2416,7 +2412,7 @@ export function createRouter(init: RouterInit): Router {
24162412
flushSync: boolean,
24172413
preventScrollReset: boolean,
24182414
submission: Submission,
2419-
shouldRevalidate: boolean,
2415+
callSiteDefaultShouldRevalidate: boolean,
24202416
) {
24212417
interruptActiveLoads();
24222418
fetchLoadMatches.delete(key);
@@ -2592,7 +2588,7 @@ export function createRouter(init: RouterInit): Router {
25922588
basename,
25932589
init.patchRoutesOnNavigation != null,
25942590
[match.route.id, actionResult],
2595-
shouldRevalidate,
2591+
callSiteDefaultShouldRevalidate,
25962592
);
25972593

25982594
// Put all revalidating fetchers into the loading state, except for the
@@ -2624,15 +2620,6 @@ export function createRouter(init: RouterInit): Router {
26242620
abortPendingFetchRevalidations,
26252621
);
26262622

2627-
if (!shouldRevalidate) {
2628-
if (state.fetchers.has(key)) {
2629-
let doneFetcher = getDoneFetcher(actionResult.data);
2630-
state.fetchers.set(key, doneFetcher);
2631-
}
2632-
fetchControllers.delete(key);
2633-
return;
2634-
}
2635-
26362623
let { loaderResults, fetcherResults } =
26372624
await callLoadersAndMaybeResolveData(
26382625
dsMatches,
@@ -4859,7 +4846,7 @@ function getMatchesToLoad(
48594846
basename: string | undefined,
48604847
hasPatchRoutesOnNavigation: boolean,
48614848
pendingActionResult?: PendingActionResult,
4862-
shouldRevalidate?: boolean,
4849+
callSiteDefaultShouldRevalidate?: boolean,
48634850
): {
48644851
dsMatches: DataStrategyMatch[];
48654852
revalidatingFetchers: RevalidatingFetcher[];
@@ -4896,7 +4883,8 @@ function getMatchesToLoad(
48964883
? pendingActionResult[1].statusCode
48974884
: undefined;
48984885
let shouldSkipRevalidation =
4899-
(actionStatus && actionStatus >= 400) || shouldRevalidate === false;
4886+
(actionStatus && actionStatus >= 400) ||
4887+
callSiteDefaultShouldRevalidate === false;
49004888

49014889
let baseShouldRevalidateArgs = {
49024890
currentUrl,
@@ -4949,7 +4937,6 @@ function getMatchesToLoad(
49494937
forceShouldLoad,
49504938
);
49514939
}
4952-
49534940
// This is the default implementation for when we revalidate. If the route
49544941
// provides it's own implementation, then we give them full control but
49554942
// provide this value so they can leverage it if needed after they check

0 commit comments

Comments
 (0)