Skip to content

Commit e1d27bc

Browse files
committed
Fix form URL navigations
1 parent ff1afa1 commit e1d27bc

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

examples/tasks/src/components/TaskItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function TaskItem(this: Remix.Handle) {
1313
const redirectId = routes.tasks.show.match(router.location.href)?.params.id ?? "";
1414

1515
return (
16-
<>
16+
<task-item>
1717
<span>{task.name}</span>
1818
&nbsp;
1919
<a href={routes.tasks.show.href({ id: task.id })}>Open</a>
@@ -39,7 +39,7 @@ export function TaskItem(this: Remix.Handle) {
3939
{isDeleting ? "Deleting..." : "❌"}
4040
</button>
4141
</form>
42-
</>
42+
</task-item>
4343
);
4444
};
4545
}

packages/router/src/router.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ export class Router<Renderable> extends EventTarget {
278278

279279
// Update outlet with the result
280280
this.#outlet = result;
281-
this.#location = location;
281+
// Only update location for GET requests (navigation)
282+
// For non-GET requests (mutations), keep the current location
283+
if (!submission) {
284+
this.#location = location;
285+
}
282286

283287
// Set navigating to idle
284288
this.#navigating = {
@@ -541,12 +545,15 @@ export class Router<Renderable> extends EventTarget {
541545
*/
542546
async navigate(to: To, options: NavigateOptions = {}): Promise<void> {
543547
const pathname = this.#resolveTo(to);
548+
const currentPath = window.location.pathname + window.location.search + window.location.hash;
544549

545-
// Update history
546-
if (options.replace) {
547-
window.history.replaceState({}, "", pathname);
548-
} else {
549-
window.history.pushState({}, "", pathname);
550+
// Update history only if the path is different
551+
if (pathname !== currentPath) {
552+
if (options.replace) {
553+
window.history.replaceState({}, "", pathname);
554+
} else {
555+
window.history.pushState({}, "", pathname);
556+
}
550557
}
551558

552559
// Perform navigation
@@ -622,8 +629,9 @@ export class Router<Renderable> extends EventTarget {
622629
formData = undefined;
623630
}
624631

625-
// Update history if this is a navigation
626-
if (options.navigate !== false) {
632+
// Update history only for GET requests
633+
// Non-GET requests (POST, PUT, DELETE, etc.) should not change the URL or add history entries
634+
if (options.navigate !== false && formMethod === "GET") {
627635
if (options.replace) {
628636
window.history.replaceState({}, "", formAction);
629637
} else {

0 commit comments

Comments
 (0)