Skip to content

Commit e11af30

Browse files
markdalgleishbrophdawg11
andauthoredAug 11, 2023
Fix actionResult type on shouldRevalidate args (#10779)
* Fix actionResult type on shouldRevalidate args * rename type for consistency * use args type in shouldRevalidateLoader * Move type declaration into details/summary --------- Co-authored-by: Matt Brophy <matt@brophy.org>
1 parent 6a08757 commit e11af30

File tree

5 files changed

+68
-47
lines changed

5 files changed

+68
-47
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Fix type for `actionResult` on the arguments object passed to `shouldRevalidate`

‎docs/route/should-revalidate.md

+26-21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ new: true
55

66
# `shouldRevalidate`
77

8+
<details>
9+
<summary>Type declaration</summary>
10+
11+
```ts
12+
interface ShouldRevalidateFunction {
13+
(args: ShouldRevalidateFunctionArgs): boolean;
14+
}
15+
16+
interface ShouldRevalidateFunctionArgs {
17+
currentUrl: URL;
18+
currentParams: AgnosticDataRouteMatch["params"];
19+
nextUrl: URL;
20+
nextParams: AgnosticDataRouteMatch["params"];
21+
formMethod?: Submission["formMethod"];
22+
formAction?: Submission["formAction"];
23+
formEncType?: Submission["formEncType"];
24+
text?: Submission["text"];
25+
formData?: Submission["formData"];
26+
json?: Submission["json"];
27+
actionResult?: any;
28+
defaultShouldRevalidate: boolean;
29+
}
30+
```
31+
32+
</details>
33+
834
This function allows you opt-out of revalidation for a route's loader as an optimization.
935

1036
<docs-warning>This feature only works if using a data router, see [Picking a Router][pickingarouter]</docs-warning>
@@ -53,27 +79,6 @@ Note that this is only for data that has already been loaded, is currently rende
5379

5480
<docs-warning>Using this API risks your UI getting out of sync with your data, use with caution!</docs-warning>
5581

56-
## Type Declaration
57-
58-
```ts
59-
interface ShouldRevalidateFunction {
60-
(args: {
61-
currentUrl: URL;
62-
currentParams: AgnosticDataRouteMatch["params"];
63-
nextUrl: URL;
64-
nextParams: AgnosticDataRouteMatch["params"];
65-
formMethod?: Submission["formMethod"];
66-
formAction?: Submission["formAction"];
67-
formEncType?: Submission["formEncType"];
68-
formData?: Submission["formData"];
69-
json?: Submission["json"];
70-
text?: Submission["text"];
71-
actionResult?: DataResult;
72-
defaultShouldRevalidate: boolean;
73-
}): boolean;
74-
}
75-
```
76-
7782
[action]: ./action
7883
[form]: ../components/form
7984
[fetcher]: ../hooks/use-fetcher

‎packages/router/__tests__/router-test.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import type {
3434
AgnosticNonIndexRouteObject,
3535
AgnosticRouteObject,
3636
DeferredData,
37+
ShouldRevalidateFunctionArgs,
3738
TrackedPromise,
3839
} from "../utils";
3940
import {
@@ -1860,7 +1861,7 @@ describe("a router", () => {
18601861
router.navigate("/params/aValue/bValue");
18611862
await tick();
18621863
expect(rootLoader.mock.calls.length).toBe(1);
1863-
expect(shouldRevalidate.mock.calls[0][0]).toMatchObject({
1864+
let expectedArg: ShouldRevalidateFunctionArgs = {
18641865
currentParams: {},
18651866
currentUrl: expect.URL("http://localhost/child"),
18661867
nextParams: {
@@ -1870,7 +1871,8 @@ describe("a router", () => {
18701871
nextUrl: expect.URL("http://localhost/params/aValue/bValue"),
18711872
defaultShouldRevalidate: false,
18721873
actionResult: undefined,
1873-
});
1874+
};
1875+
expect(shouldRevalidate.mock.calls[0][0]).toMatchObject(expectedArg);
18741876
rootLoader.mockClear();
18751877
shouldRevalidate.mockClear();
18761878

@@ -1924,7 +1926,7 @@ describe("a router", () => {
19241926
expect(shouldRevalidate.mock.calls.length).toBe(1);
19251927
// @ts-expect-error
19261928
let arg = shouldRevalidate.mock.calls[0][0];
1927-
expect(arg).toMatchObject({
1929+
let expectedArg: ShouldRevalidateFunctionArgs = {
19281930
currentParams: {},
19291931
currentUrl: expect.URL("http://localhost/child"),
19301932
nextParams: {},
@@ -1934,7 +1936,8 @@ describe("a router", () => {
19341936
formAction: "/child",
19351937
formEncType: "application/x-www-form-urlencoded",
19361938
actionResult: "ACTION",
1937-
});
1939+
};
1940+
expect(arg).toMatchObject(expectedArg);
19381941
// @ts-expect-error
19391942
expect(Object.fromEntries(arg.formData)).toEqual({ key: "value" });
19401943

@@ -1977,7 +1980,7 @@ describe("a router", () => {
19771980
expect(shouldRevalidate.mock.calls.length).toBe(1);
19781981
// @ts-expect-error
19791982
let arg = shouldRevalidate.mock.calls[0][0];
1980-
expect(arg).toMatchObject({
1983+
let expectedArg: ShouldRevalidateFunctionArgs = {
19811984
currentParams: {},
19821985
currentUrl: expect.URL("http://localhost/child"),
19831986
nextParams: {},
@@ -1987,7 +1990,8 @@ describe("a router", () => {
19871990
formAction: "/child",
19881991
formEncType: "application/x-www-form-urlencoded",
19891992
actionResult: undefined,
1990-
});
1993+
};
1994+
expect(arg).toMatchObject(expectedArg);
19911995
// @ts-expect-error
19921996
expect(Object.fromEntries(arg.formData)).toEqual({ key: "value" });
19931997

@@ -2022,15 +2026,16 @@ describe("a router", () => {
20222026
expect(shouldRevalidate.mock.calls.length).toBe(1);
20232027
// @ts-expect-error
20242028
let arg = shouldRevalidate.mock.calls[0][0];
2025-
expect(arg).toMatchObject({
2029+
let expectedArg: Partial<ShouldRevalidateFunctionArgs> = {
20262030
formMethod: "post",
20272031
formAction: "/",
20282032
formEncType: "application/json",
20292033
text: undefined,
20302034
formData: undefined,
20312035
json: { key: "value" },
20322036
actionResult: "ACTION",
2033-
});
2037+
};
2038+
expect(arg).toMatchObject(expectedArg);
20342039

20352040
router.dispose();
20362041
});
@@ -2063,15 +2068,16 @@ describe("a router", () => {
20632068
expect(shouldRevalidate.mock.calls.length).toBe(1);
20642069
// @ts-expect-error
20652070
let arg = shouldRevalidate.mock.calls[0][0];
2066-
expect(arg).toMatchObject({
2071+
let expectedArg: Partial<ShouldRevalidateFunctionArgs> = {
20672072
formMethod: "post",
20682073
formAction: "/",
20692074
formEncType: "text/plain",
20702075
text: "hello world",
20712076
formData: undefined,
20722077
json: undefined,
20732078
actionResult: "ACTION",
2074-
});
2079+
};
2080+
expect(arg).toMatchObject(expectedArg);
20752081

20762082
router.dispose();
20772083
});

‎packages/router/router.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type {
2828
RedirectResult,
2929
RouteData,
3030
RouteManifest,
31-
ShouldRevalidateFunction,
31+
ShouldRevalidateFunctionArgs,
3232
Submission,
3333
SuccessResult,
3434
V7_FormMethod,
@@ -3513,7 +3513,7 @@ function isNewRouteInstance(
35133513

35143514
function shouldRevalidateLoader(
35153515
loaderMatch: AgnosticDataRouteMatch,
3516-
arg: Parameters<ShouldRevalidateFunction>[0]
3516+
arg: ShouldRevalidateFunctionArgs
35173517
) {
35183518
if (loaderMatch.route.shouldRevalidate) {
35193519
let routeChoice = loaderMatch.route.shouldRevalidate(arg);

‎packages/router/utils.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@ export interface ActionFunction {
174174
(args: ActionFunctionArgs): Promise<DataFunctionValue> | DataFunctionValue;
175175
}
176176

177+
/**
178+
* Arguments passed to shouldRevalidate function
179+
*/
180+
export interface ShouldRevalidateFunctionArgs {
181+
currentUrl: URL;
182+
currentParams: AgnosticDataRouteMatch["params"];
183+
nextUrl: URL;
184+
nextParams: AgnosticDataRouteMatch["params"];
185+
formMethod?: Submission["formMethod"];
186+
formAction?: Submission["formAction"];
187+
formEncType?: Submission["formEncType"];
188+
text?: Submission["text"];
189+
formData?: Submission["formData"];
190+
json?: Submission["json"];
191+
actionResult?: any;
192+
defaultShouldRevalidate: boolean;
193+
}
194+
177195
/**
178196
* Route shouldRevalidate function signature. This runs after any submission
179197
* (navigation or fetcher), so we flatten the navigation/fetcher submission
@@ -182,20 +200,7 @@ export interface ActionFunction {
182200
* have to re-run based on the data models that were potentially mutated.
183201
*/
184202
export interface ShouldRevalidateFunction {
185-
(args: {
186-
currentUrl: URL;
187-
currentParams: AgnosticDataRouteMatch["params"];
188-
nextUrl: URL;
189-
nextParams: AgnosticDataRouteMatch["params"];
190-
formMethod?: Submission["formMethod"];
191-
formAction?: Submission["formAction"];
192-
formEncType?: Submission["formEncType"];
193-
text?: Submission["text"];
194-
formData?: Submission["formData"];
195-
json?: Submission["json"];
196-
actionResult?: DataResult;
197-
defaultShouldRevalidate: boolean;
198-
}): boolean;
203+
(args: ShouldRevalidateFunctionArgs): boolean;
199204
}
200205

201206
/**

0 commit comments

Comments
 (0)
Please sign in to comment.