history: push/replace stamp __TSR_index before blockers are awaited, so a second navigation gets the wrong index (canGoBack() returns false)

Đang mở
#8,844 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@orkirson đang làm issue này rồi.

Từ ngày 2/11/2026.

  • #8847 của @orkirson — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
42/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
react, typescript
Lĩnh vực
frontend, web-dev

Hướng nghiên cứu

The eager index stamp is in createHistory's push and replace, using assignKeyAndIndex with currentIndex + 1 before tryNavigation awaits the blockers; location is only updated in the task's notify. Start by reproducing with the minimal script in the issue against memory history, then move the __TSR_index computation into the task so it reads location.state at write time. Done when push then replace, and push then push, leave the correct indices and canGoBack() returns true, with blockers still receiving the eager nextLocation.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Which project does this relate to?

Router

Describe the bug

createHistory's push and replace compute the new entry's __TSR_index from location at the moment they are called (assignKeyAndIndex(currentIndex + 1, state)), and only afterwards await the registered blockers in tryNavigation. location itself is only updated inside the task's notify.

So with any blocker registered (even one that always returns false), a second navigation made while the first is still waiting on its blockers is stamped from the entry before the first one:

  • push then replace in one click lands a single entry carrying the previous entry's index, so history.canGoBack() returns false on a page you can go back from (a "Back" control reading it shows disabled).
  • push then push stamps both entries with the same index.

Any app with an always-on useBlocker (for example an unsaved-changes guard) hits this on every click that makes two navigations.

Complete minimal reproducer
// @tanstack/history 1.162.4 (react-router 1.170.40). Blockers only run where a `document` exists.
globalThis.document ??= {};
const { createMemoryHistory } = await import("@tanstack/history");

const history = createMemoryHistory({ initialEntries: ["/list"] });
history.block({ blockerFn: async () => false, enableBeforeUnload: false });

history.push("/record");
history.replace("/record/letters");
await new Promise((r) => setTimeout(r, 0));

console.log(history.location.pathname, history.location.state.__TSR_index, history.canGoBack());
// actual:   /record/letters 0 false
// expected: /record/letters 1 true
Steps to Reproduce the Bug
  1. Register any blocker on the history (history.block({ blockerFn: async () => false }), or useBlocker in an app).
  2. In one synchronous handler, call push(a) and then replace(b) (or push(b)).
  3. Read history.location.state.__TSR_index / history.canGoBack() after both land.
Expected behavior

The index is stamped inside the task, from location at write time (push: location.state.__TSR_index + 1; replace: location.state.__TSR_index), so each entry is numbered from the entry it actually follows. The eagerly stamped state can still be used for the blockers' nextLocation.

Platform
  • @tanstack/history 1.162.4, @tanstack/react-router 1.170.40
  • Reproduces in Node (memory history) and in the browser (browser history)
Additional context

Related but not the same: #3193. Without a blocker the bug doesn't show, because the navigations land synchronously and location is current when the second one is stamped.

Ngôn ngữ chính
TypeScript
Star
15.3k
Fork
1.9k
Merge trung bình
1 ngày 5 giờ
Pull request đã merge (30 ngày)
46

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TanStack/router

Tất cả issue của TanStack/router

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.