fix(收藏/聊天记录检索): 切换页内tab不再污染浏览器历史记录
用户反馈"我的收藏"页面点击tabs切换(全部/图片/视频/文件/语音/文本/链接)会记录到浏览器 历史记录里,导致点击返回一直在这个页面内切换tab,而不是真正返回上一页。 根因:content-library-view.jsx的Favorites/HistoryNav每次切tab都调用navigate(), 即HashNavigation.navigateTo() -> location.hash = ...,每次赋值都会被浏览器记为一条 新的历史记录。切N次tab就push N条记录,无论是系统返回键还是应用自己的"返回"按钮 (HashNavigation.goBack()),都要先把这N条tab切换记录走完才能真正离开页面。 修复:新增HashNavigation.navigateReplace(),用history.replaceState替换当前历史记录 而不是新增一条(hash该更新还是更新,刷新/复制链接不受影响),并手动派发hashchange事件 供tinode-web.jsx现有的监听器捕获——replaceState本身不会自动触发该事件。Favorites的 TYPE_LABELS tabs和HistoryNav("查找聊天记录"页的聊天记录/图片/视频/文件/链接tabs) 两处tab切换都改用这个新方法,不再计入navigationCount。 已在浏览器实测两处:连续切3次tab后,无论是原生history.back()还是应用返回按钮,都能 一次性返回到进入这个页面之前的真实上一页,不再卡在tab切换历史里。
This commit is contained in:
@@ -40,6 +40,22 @@ export default class HashNavigation {
|
||||
window.location.hash = url;
|
||||
}
|
||||
|
||||
// For switching between sub-tabs of the *same* page (e.g. content-library-view.jsx's favorites/
|
||||
// history type tabs), not for navigating to a different page. `navigateTo`/`location.hash =`
|
||||
// always pushes a new history entry, so clicking through N tabs pushes N entries -- the system
|
||||
// back button (and this class's own goBack()) then has to step back through every tab the user
|
||||
// ever visited before it reaches whatever page they were actually on before this one. `replaceState`
|
||||
// updates the hash (still correct for a refresh or a copied link) without adding a history entry,
|
||||
// so it doesn't inflate navigationCount either -- a tab switch was never a real in-app navigation
|
||||
// to begin with. replaceState doesn't fire `hashchange` on its own, so dispatch one manually for
|
||||
// the existing `window.addEventListener('hashchange', ...)` listener (tinode-web.jsx's
|
||||
// handleHashRoute) to pick up; that handler re-reads window.location.hash itself rather than the
|
||||
// event, so a synthetic event with no detail is indistinguishable from a real one to it.
|
||||
static navigateReplace(url) {
|
||||
window.history.replaceState(null, '', '#' + url);
|
||||
window.dispatchEvent(new Event('hashchange'));
|
||||
}
|
||||
|
||||
// Real "back": retraces the hash the user was actually on before, via native browser history,
|
||||
// rather than a page's own guess at where it must have been reached from. Returns whether it
|
||||
// actually did that -- every call site already has its own hardcoded-destination `navigate(...)`
|
||||
|
||||
Reference in New Issue
Block a user