fix(sidebar): prevent spurious full-page reloads on rapid branch clicks#661
Merged
fix(sidebar): prevent spurious full-page reloads on rapid branch clicks#661
Conversation
Root cause: handleBranchClick created a new 300ms fallback timer on every click, but the cleanup function returned by useCallback was discarded (event handlers ignore return values), and popstate does not fire on programmatic router.push calls. As a result, timers accumulated — each one comparing a stale targetPath against the current pathname. Rapid clicks caused old timers to fire after navigation had already moved to a newer item, triggering `window.location.href` full-page reloads. Fix: track the single active timer in a ref (fallbackTimerRef). Each click cancels the previous timer before scheduling a new one, ensuring only the most-recently-clicked item can trigger the fallback. Also removed the broken popstate listener and increased delay to 500ms. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
サイドバーのブランチをクリックした際に画面全体がリロードされるケースがある問題の修正。
根本原因
handleBranchClickに3つのバグが重なっていた:setTimeoutを作成するが、返したクリーンアップ関数はイベントハンドラの戻り値なので誰も呼ばないpopstateが発火しないrouter.push()はhistory.pushState()を使うためpopstateは発火しない(ブラウザの戻る/進むのみ)targetPathと現在の pathname を比較してwindow.location.hrefによるフルリロードを起こす例: branchB → branchC と素早くクリック → 300ms後にbranchBのタイマーが
pathname=/worktrees/branchC ≠ /worktrees/branchBと判定 → フルリロード修正内容
fallbackTimerRef(useRef)で単一のタイマーを管理popstateリスナーを削除Test plan
🤖 Generated with Claude Code