Skip to content

fix(sidebar): prevent spurious full-page reloads on rapid branch clicks#661

Merged
Kewton merged 1 commit intodevelopfrom
feature/651-worktree
Apr 14, 2026
Merged

fix(sidebar): prevent spurious full-page reloads on rapid branch clicks#661
Kewton merged 1 commit intodevelopfrom
feature/651-worktree

Conversation

@Kewton
Copy link
Copy Markdown
Owner

@Kewton Kewton commented Apr 14, 2026

Summary

サイドバーのブランチをクリックした際に画面全体がリロードされるケースがある問題の修正。

根本原因

handleBranchClick に3つのバグが重なっていた:

バグ 内容
① タイマーの蓄積 クリックのたびに setTimeout を作成するが、返したクリーンアップ関数はイベントハンドラの戻り値なので誰も呼ばない
popstate が発火しない router.push()history.pushState() を使うため popstate は発火しない(ブラウザの戻る/進むのみ)
③ 誤検知リロード 素早く複数クリックすると古いタイマーが蓄積し、staleな targetPath と現在の pathname を比較して window.location.href によるフルリロードを起こす

例: branchB → branchC と素早くクリック → 300ms後にbranchBのタイマーが pathname=/worktrees/branchC ≠ /worktrees/branchB と判定 → フルリロード

修正内容

  • fallbackTimerRefuseRef)で単一のタイマーを管理
  • 新しいクリック時に前のタイマーをキャンセルしてから新規セット
  • 最後にクリックしたアイテムのフォールバックのみが機能するようになった
  • 機能しなかった popstate リスナーを削除

Test plan

  • サイドバーを素早く連続クリックしても画面全体がリロードされない
  • 通常クリックで正常にページ遷移できる
  • router.push が失敗した場合のフォールバック(500ms後)が動作する

🤖 Generated with Claude Code

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>
@Kewton Kewton merged commit d246016 into develop Apr 14, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant