Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide fetcher submission to shouldRevalidate if fetcher action redirects #10208

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Provide fetcher submission to shouldRevalidate if fetcher action redi…
…rects
  • Loading branch information
brophdawg11 committed Mar 15, 2023
commit 72d8e0759a74758e9ee1c1f4f4498e59a98f8f63
5 changes: 5 additions & 0 deletions .changeset/fetcher-submission-revalidation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Provide fetcher submission to `shouldRevalidate` if the fetcher action redirects
36 changes: 24 additions & 12 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function findRouteById(
}

interface CustomMatchers<R = jest.Expect> {
URL(url: string);
trackedPromise(data?: any, error?: any, aborted?: boolean): R;
deferredData(
done: boolean,
Expand All @@ -204,6 +205,13 @@ declare global {
}

expect.extend({
// Custom matcher for asserting against URLs
URL(received, url) {
return {
message: () => `expected URL ${received.toString()} to equal URL ${url}`,
pass: received instanceof URL && received.toString() === url,
};
},
// Custom matcher for asserting deferred promise results for static handler
// - expect(val).deferredData(false) => Unresolved promise
// - expect(val).deferredData(false) => Resolved promise
Expand Down Expand Up @@ -1825,12 +1833,12 @@ describe("a router", () => {
expect(rootLoader.mock.calls.length).toBe(1);
expect(shouldRevalidate.mock.calls[0][0]).toMatchObject({
currentParams: {},
currentUrl: new URL("http://localhost/child"),
currentUrl: expect.URL("http://localhost/child"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found out that new URL inside toMatchObject doesn't actually validate the internal URL 🤦 , so added the custom matcher and changed all existing instances. Thankfully no false positives.

nextParams: {
a: "aValue",
b: "bValue",
},
nextUrl: new URL("http://localhost/params/aValue/bValue"),
nextUrl: expect.URL("http://localhost/params/aValue/bValue"),
defaultShouldRevalidate: false,
actionResult: undefined,
});
Expand Down Expand Up @@ -1889,9 +1897,9 @@ describe("a router", () => {
let arg = shouldRevalidate.mock.calls[0][0];
expect(arg).toMatchObject({
currentParams: {},
currentUrl: new URL("http://localhost/child"),
currentUrl: expect.URL("http://localhost/child"),
nextParams: {},
nextUrl: new URL("http://localhost/child"),
nextUrl: expect.URL("http://localhost/child"),
defaultShouldRevalidate: true,
formMethod: "post",
formAction: "/child",
Expand Down Expand Up @@ -1942,9 +1950,9 @@ describe("a router", () => {
let arg = shouldRevalidate.mock.calls[0][0];
expect(arg).toMatchObject({
currentParams: {},
currentUrl: new URL("http://localhost/child"),
currentUrl: expect.URL("http://localhost/child"),
nextParams: {},
nextUrl: new URL("http://localhost/"),
nextUrl: expect.URL("http://localhost/"),
defaultShouldRevalidate: true,
formMethod: "post",
formAction: "/child",
Expand Down Expand Up @@ -2108,9 +2116,9 @@ describe("a router", () => {
expect(shouldRevalidate.mock.calls.length).toBe(1);
expect(shouldRevalidate.mock.calls[0][0]).toMatchObject({
currentParams: {},
currentUrl: new URL("http://localhost/"),
currentUrl: expect.URL("http://localhost/"),
nextParams: {},
nextUrl: new URL("http://localhost/child"),
nextUrl: expect.URL("http://localhost/child"),
defaultShouldRevalidate: false,
});
expect(router.state.fetchers.get(key)).toMatchObject({
Expand All @@ -2123,9 +2131,9 @@ describe("a router", () => {
expect(shouldRevalidate.mock.calls.length).toBe(2);
expect(shouldRevalidate.mock.calls[1][0]).toMatchObject({
currentParams: {},
currentUrl: new URL("http://localhost/child"),
currentUrl: expect.URL("http://localhost/child"),
nextParams: {},
nextUrl: new URL("http://localhost/"),
nextUrl: expect.URL("http://localhost/"),
defaultShouldRevalidate: false,
});
expect(router.state.fetchers.get(key)).toMatchObject({
Expand All @@ -2147,9 +2155,9 @@ describe("a router", () => {
expect(shouldRevalidate.mock.calls.length).toBe(3);
expect(shouldRevalidate.mock.calls[2][0]).toMatchObject({
currentParams: {},
currentUrl: new URL("http://localhost/"),
currentUrl: expect.URL("http://localhost/"),
nextParams: {},
nextUrl: new URL("http://localhost/child"),
nextUrl: expect.URL("http://localhost/child"),
formAction: "/child",
formData: createFormData({}),
formEncType: "application/x-www-form-urlencoded",
Expand Down Expand Up @@ -2269,6 +2277,10 @@ describe("a router", () => {
"currentParams": {},
"currentUrl": "http://localhost/",
"defaultShouldRevalidate": true,
"formAction": "/fetch",
"formData": FormData {},
"formEncType": "application/x-www-form-urlencoded",
"formMethod": "post",
Comment on lines +2280 to +2283
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fetcher submission should be included to shouldRevalidate if the fetcher action redirected

"nextParams": {},
"nextUrl": "http://localhost/",
}
Expand Down
1 change: 1 addition & 0 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,7 @@ export function createRouter(init: RouterInit): Router {
updateState({ fetchers: new Map(state.fetchers) });

return startRedirectNavigation(state, actionResult, {
submission,
isFetchActionRedirect: true,
});
}
Expand Down