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

fix: reset actionData state on redirection from an action #9334

Merged
merged 4 commits into from
Sep 27, 2022
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
5 changes: 5 additions & 0 deletions .changeset/short-hats-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

fix: reset `actionData` after successful action redirect (#9334)
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- jmargeta
- johnpangalos
- jonkoops
- jrakotoharisoa
- kantuni
- kddnewton
- kentcdodds
Expand Down
75 changes: 75 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2706,6 +2706,81 @@ describe("a router", () => {
expect(t.router.state.actionData).toBeNull();
});

it("removes action data after action redirect (w/o loaders to run)", async () => {
let t = setup({
routes: [
{
index: true,
id: "index",
action: true,
},
{
path: "/other",
id: "other",
},
],
});
let A = await t.navigate("/", {
formMethod: "post",
formData: createFormData({ gosh: "" }),
});
await A.actions.index.resolve({ error: "invalid" });
expect(t.router.state.actionData).toEqual({
index: { error: "invalid" },
});

let B = await t.navigate("/", {
formMethod: "post",
formData: createFormData({ gosh: "dang" }),
});
await B.actions.index.redirectReturn("/other");

expect(t.router.state.actionData).toBeNull();
});

it("removes action data after action redirect (w/ loaders to run)", async () => {
let t = setup({
routes: [
{
index: true,
id: "index",
action: true,
},
{
path: "/other",
id: "other",
loader: true,
},
],
});
let A = await t.navigate("/", {
formMethod: "post",
formData: createFormData({ gosh: "" }),
});
await A.actions.index.resolve({ error: "invalid" });
expect(t.router.state.actionData).toEqual({
index: { error: "invalid" },
});

let B = await t.navigate("/", {
formMethod: "post",
formData: createFormData({ gosh: "dang" }),
});

let C = await B.actions.index.redirectReturn("/other");
expect(t.router.state.actionData).toEqual({
index: { error: "invalid" },
});
expect(t.router.state.loaderData).toEqual({});

await C.loaders.other.resolve("OTHER");

expect(t.router.state.actionData).toBeNull();
expect(t.router.state.loaderData).toEqual({
other: "OTHER",
});
});

it("uses the proper action for index routes", async () => {
let t = setup({
routes: [
Expand Down
11 changes: 6 additions & 5 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,15 @@ export function createRouter(init: RouterInit): Router {
// - We have committed actionData in the store
// - The current navigation was a submission
// - We're past the submitting state and into the loading state
// - This should not be susceptible to false positives for
// loading/submissionRedirect since there would not be actionData in the
// state since the prior action would have returned a redirect response
// and short circuited
// - The location we've finished loading is different from the submission
// location, indicating we redirected from the action (avoids false
// positives for loading/submissionRedirect when actionData returned
// on a prior submission)
let isActionReload =
state.actionData != null &&
state.navigation.formMethod != null &&
state.navigation.state === "loading";
state.navigation.state === "loading" &&
state.navigation.formAction?.split("?")[0] === location.pathname;
jrakotoharisoa marked this conversation as resolved.
Show resolved Hide resolved

// Always preserve any existing loaderData from re-used routes
let newLoaderData = newState.loaderData
Expand Down