history: push/replace stamp __TSR_index before blockers are awaited, so a second navigation gets the wrong index (canGoBack() returns false)
维护者通常 1 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 半天
- 新手友好度
- 42/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- react, typescript
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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:
pushthenreplacein one click lands a single entry carrying the previous entry's index, sohistory.canGoBack()returnsfalseon a page you can go back from (a "Back" control reading it shows disabled).pushthenpushstamps 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
- Register any blocker on the history (
history.block({ blockerFn: async () => false }), oruseBlockerin an app). - In one synchronous handler, call
push(a)and thenreplace(b)(orpush(b)). - 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.
- 主要语言
- TypeScript
- 星标
- 15.3k
- 派生
- 1.9k
- 平均合并
- 1 天 5 小时
- 30 天内合并 PR
- 46
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/router 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
Masked navigation drops the first character of the hash each time (__tempLocation hash missing '#')可能已有人在做 @hassanabrar202 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
router-generator: lazy-load Prettier (costs ~300–450ms per build even when formatting is off)可能已有人在做 @Sheraff 于 27 天前认领。 未关闭package: router-generator performance
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
router-generator: sibling `physical()` subtrees in a virtual route config generate duplicate identifiers可能已有人在做 @Sheraff 于 27 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
docs(router): clarify static title fallback in SPA mode可能已有人在做 @Rohan-Dedhia 于 17 天前认领。 未关闭
难度 1/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
相似的 Issue
-
[BUG]: Shared links drop the configured basePath on GitHub Pages可能已有人在做 @anshulchikhale30-p 今天认领。 未关闭bug
难度 1/5 1 小时以内 新手友好度 88/100
StabilityNexus/IdentityTokens-EVM-Frontend#182 · 1 条评论 ·
维护者通常 1 天内回复
-
Add: GTrekker未关闭check:passed streams:add
难度 1/5 1 小时以内 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
Koenkk/zigbee-herdsman-converters#13900 ·
维护者通常 1 天内回复
-
area: ui Bug Payload OSS
难度 1/5 1 小时以内 新手友好度 88/100
payloadcms/payload#19839 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
jeanibarz/knowledge-base-mcp-server#1291 ·
维护者通常 1 天内回复