Skip to content

Commit

Permalink
Fix: reset actionData on action redirect to current location (#9772)
Browse files Browse the repository at this point in the history
* Clear actionData on redirect to current location

* Add changeset
  • Loading branch information
brophdawg11 authored Dec 22, 2022
1 parent 554aa84 commit 159b988
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-news-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Reset `actionData` on action redirect to current location
51 changes: 50 additions & 1 deletion packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3013,13 +3013,17 @@ describe("a router", () => {

await A.actions.foo.resolve("FOO ACTION");
expect(A.loaders.root.stub.mock.calls.length).toBe(1);
expect(t.router.state.actionData).toEqual({
foo: "FOO ACTION",
});

let B = await A.loaders.foo.redirect("/bar");
await A.loaders.root.reject("ROOT ERROR");
await B.loaders.root.resolve("ROOT LOADER 2");
await B.loaders.bar.resolve("BAR LOADER");
expect(B.loaders.root.stub.mock.calls.length).toBe(1);
expect(t.router.state).toMatchObject({
actionData: null,
loaderData: {
root: "ROOT LOADER 2",
bar: "BAR LOADER",
Expand Down Expand Up @@ -3051,8 +3055,11 @@ describe("a router", () => {
});
expect(A.loaders.root.stub.mock.calls.length).toBe(0);

await A.actions.foo.resolve(null);
await A.actions.foo.resolve("FOO ACTION");
expect(A.loaders.root.stub.mock.calls.length).toBe(1);
expect(t.router.state.actionData).toEqual({
foo: "FOO ACTION",
});

await A.loaders.foo.resolve("A LOADER");
expect(t.router.state.navigation.state).toBe("loading");
Expand All @@ -3063,6 +3070,9 @@ describe("a router", () => {

await A.loaders.root.resolve("ROOT LOADER");
expect(t.router.state.navigation.state).toBe("idle");
expect(t.router.state.actionData).toEqual({
foo: "FOO ACTION", // kept around on action reload
});
expect(t.router.state.loaderData).toEqual({
foo: "A LOADER",
root: "ROOT LOADER",
Expand Down Expand Up @@ -3238,6 +3248,45 @@ describe("a router", () => {
});
});

it("removes action data after action redirect to current location", async () => {
let t = setup({
routes: [
{
path: "/",
id: "index",
action: true,
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("/");
expect(t.router.state.actionData).toEqual({
index: { error: "invalid" },
});
expect(t.router.state.loaderData).toEqual({});

await C.loaders.index.resolve("NEW");

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

it("uses the proper action for index routes", async () => {
let t = setup({
routes: [
Expand Down
10 changes: 4 additions & 6 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,17 +735,15 @@ export function createRouter(init: RouterInit): Router {
): void {
// Deduce if we're in a loading/actionReload state:
// - We have committed actionData in the store
// - The current navigation was a submission
// - The current navigation was a mutation submission
// - We're past the submitting state and into the loading state
// - 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)
// - The location being loaded is not the result of a redirect
let isActionReload =
state.actionData != null &&
state.navigation.formMethod != null &&
isMutationMethod(state.navigation.formMethod) &&
state.navigation.state === "loading" &&
state.navigation.formAction?.split("?")[0] === location.pathname;
location.state?._isRedirect !== true;

let actionData: RouteData | null;
if (newState.actionData) {
Expand Down

0 comments on commit 159b988

Please sign in to comment.