Skip to content

fix: use a push navigation on submission errors #9162

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

Merged
merged 2 commits into from
Aug 17, 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/wise-scissors-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

fix: use a push navigation on submission errors (#9162)
3 changes: 2 additions & 1 deletion docs/components/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ Instructs the form to replace the current entry in the history stack, instead of
The default behavior is conditional on the form `method`:

- `get` defaults to `false`
- every other method defaults to `true`
- every other method defaults to `true` if your `action` is successful
- if your `action` redirects or throws, then it will still push by default

We've found with `get` you often want the user to be able to click "back" to see the previous search results/filters, etc. But with the other methods the default is `true` to avoid the "are you sure you want to resubmit the form?" prompt. Note that even if `replace={false}` React Router _will not_ resubmit the form when the back button is clicked and the method is post, put, patch, or delete.

Expand Down
29 changes: 29 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,35 @@ describe("a router", () => {
expect(t.router.state.location.pathname).toEqual("/foo");
});

it("navigates correctly using POP navigations across action errors", async () => {
let t = initializeTmTest();

// Navigate to /foo
let A = await t.navigate("/foo");
await A.loaders.foo.resolve("FOO");
expect(t.router.state.location.pathname).toEqual("/foo");

// Navigate to /bar
let B = await t.navigate("/bar");
await B.loaders.bar.resolve("BAR");
expect(t.router.state.location.pathname).toEqual("/bar");

// Post to /bar (should push due to our error)
let C = await t.navigate("/bar", {
formMethod: "post",
formData: createFormData({ key: "value" }),
});
await C.actions.bar.reject("BAR ERROR");
await C.loaders.root.resolve("ROOT");
await C.loaders.bar.resolve("BAR");
expect(t.router.state.location.pathname).toEqual("/bar");

// POP to /bar
let D = await t.navigate(-1);
await D.loaders.bar.resolve("BAR");
expect(t.router.state.location.pathname).toEqual("/bar");
});

it("navigates correctly using POP navigations across loader redirects", async () => {
// Start at / (history stack: [/])
let t = initializeTmTest();
Expand Down
9 changes: 9 additions & 0 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,15 @@ export function createRouter(init: RouterInit): Router {
// Store off the pending error - we use it to determine which loaders
// to call and will commit it when we complete the navigation
let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);

// By default, all submissions are REPLACE navigations, but if the
// action threw an error that'll be rendered in an errorElement, we fall
// back to PUSH so that the user can use the back button to get back to
// the pre-submission form location to try again
if (opts?.replace !== true) {
pendingAction = HistoryAction.Push;
}

return {
pendingActionError: { [boundaryMatch.route.id]: result.error },
};
Expand Down