Repository navigation
fix(live-reload): SSE 再接続時に切断中の変更を再同期し、再接続バナーを接続状態と一致させる - #32
Merged
Merged
Conversation
サーバーはイベントを再送しないため、ライブリロードのストリームが切れている間 (サーバー再起動・スリープ復帰など)に変更されたファイルは、再接続後も古い内容の まま表示され続けていた。また再接続バナーは 4 秒で自動的に消え、再試行ごとに 再表示されて点滅していた。 - 切断を記録し、再接続の handshake (connected) でファイルツリー、左右ペインの ファイル、アルバムを再取得する - バナーは切断中は表示し続け、再接続後に "Reconnected" を 2 秒表示して消す - バナーに role="status" を付与し、innerHTML を textContent に置き換える - 切断中の変更が再接続後に反映されることを検証する回帰テストを追加 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C6Vo4j6UHSf62h8vFp5AN8
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a7a58d1570
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
比較ビューは直前のファイルを currentFilePath に残すため、再接続時の reloadCurrentFile() が loadServerFile() 経由で比較を閉じてそのファイルへ 戻っていた。比較中は通常ファイルの再読込を行わない。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C6Vo4j6UHSf62h8vFp5AN8
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.
背景と目的
DocView の中核機能であるライブリロード(chokidar → SSE → フロントエンドでの再読込)で、ストリームが切れていた間(サーバー再起動・スリープ復帰・ネットワーク切替など)に変更されたファイルが、再接続後も古い内容のまま表示され続けていました。 警告は出ず、利用者からは「最新を見ている」ように見えます。
あわせて、再接続バナーが接続状態と一致していませんでした。4 秒で自動的に消え、3 秒ごとの再試行で再表示されるため、切断が続いている間は点滅していました。
問題と原因
server.mjsのbroadcast()は、その時点で接続しているクライアントにだけイベントを送ります。イベントの再送や履歴はありません。src/main.tsのconnectSSE()は、再接続時のconnectedhandshake を受け取っても何もしていませんでした(if (event.data === 'connected') return;)。そのため、切断中に起きた change / add / unlink を取り戻す経路がありませんでした。setTimeout(() => banner.remove(), 4000)で消える予約がされ、接続状態とは無関係に消えていました。As-Is / To-Be
設計と変更内容(
src/main.tsのみ)sseNeedsResyncフラグ:onerrorで立て、次のconnectedでresyncAfterReconnect()を 1 回だけ呼びます。resyncAfterReconnect():各ビューが change イベントを受け取ったときと同じ処理を呼びます。具体的にはfileTree.refresh()、reloadCurrentFile()、loadIntoSplit(splitFilePath)、reloadCurrentAlbum()です(アルバムの再読込は既存のガードで、表示中のときだけ動きます)。新しい読込処理は作らず、スクロール保持や読込世代チェックなど既存の安全策をそのまま使います。compareActive)の表示中は、reloadCurrentFile()を呼びません。比較ビューは直前のファイルをcurrentFilePathに残すため、再読込すると比較が閉じてしまうからです(Codex 指摘、2d64246 で対応)。setReconnectBanner(text | null):バナーの状態管理を 1 箇所にまとめました。切断中は表示し続け、再接続したら "Reconnected" を 2 秒表示してから消します。2 秒以内に再び切れた場合は、タイマーを解除して文言を戻します。role="status"を付けました。innerHTMLはtextContentに置き換えています。検討した代替案: サーバー側でイベント ID と再送バッファを持ち、
Last-Event-IDで取り戻す方式も考えました。ただし、サーバーの状態とメモリ管理が増えるうえ、サーバー再起動時には履歴が消えるため、主要なケースを救えません。そこで、クライアント側で表示中のビューだけを再取得する方式を採りました。トレードオフ: 再接続時に、実際には変更されていないファイルも 1 回再描画されます。再接続はまれなイベントで、再描画は既存の change 処理と同じ経路(スクロールは保持)を通るため、許容範囲と判断しました。
視覚的な変更
既存の
.reconnect-bannerスタイルとデザイントークンはそのままで、表示状態と文言だけが変わります。分割ペインで左右に別ファイルを開いた状態で、ローカルの実ブラウザ(Chromium、1280×720)で確認しました。コンソールには、意図的に遮断した
/api/watchのERR_FAILEDと、.docview.css未配置による 404 以外のエラーはありませんでした。スクリーンショットはローカルでのみ取得し、リポジトリには追加していません。検証結果(最新コミット 2d64246)
npx tsc --noEmit/npm run buildnpm run test:unitnpm run check:contrastnpx playwright test --workers=4(Playwright 管理の新規サーバー)追加した回帰テスト(
tests/regression.spec.ts)は、いずれも修正前の dist では失敗し、修正後は成功します。/api/watchを handshake の直後に終了するストリームに差し替えて切断を再現します。再接続を拒否している間にファイルを書き換え、5 秒経ってもバナーが表示されたままであることを確認します。最後に実ストリームを通し、v2 が表示されてバナーが消えることを確認します。どちらのテストも chokidar のイベントに依存しない作りです。fixture
live-reload-resync.mdはtests/setup.tsで事前に作成し、他のページでツリー更新を起こすaddイベントが出ないようにしています。※ この環境の Chromium と、プロジェクトが固定している Playwright のブラウザのリビジョンが一致しなかったため、ローカルではスクラッチ領域の
PLAYWRIGHT_BROWSERS_PATHで別リビジョンを指定して E2E を実行しました。リポジトリの設定は変更していません。リスク・残存事項
applyTheme()にも、比較中にreloadCurrentFile()を呼んでしまう同じ形の処理があります。この PR 以前からある挙動なので、今回は手を入れていません。regression.spec.ts:20、frontend-audit.spec.ts:50)が失敗しました。サーバーの SSE を直接記録すると、chokidar のイベントが書き込みから 20〜40 秒遅れて届くか、テストの時間内には届きませんでした。🤖 Generated with Claude Code
https://claude.ai/code/session_01C6Vo4j6UHSf62h8vFp5AN8