fix scan SSE race: connect before starting scan so no events are missed
All checks were successful
Build and Push Docker Image / build (push) Successful in 34s

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-05 10:06:40 +01:00
parent 2b1dda3c7d
commit 28ca5679c6

View File

@@ -31,9 +31,8 @@ export function ScanPage() {
useEffect(() => { load(); }, []);
// Connect SSE when running
useEffect(() => {
if (!status?.running) return;
const connectSse = () => {
esRef.current?.close();
const es = new EventSource('/api/scan/events');
esRef.current = es;
@@ -53,24 +52,42 @@ export function ScanPage() {
es.addEventListener('complete', (e) => {
const d = JSON.parse(e.data || '{}') as { scanned?: number; errors?: number };
es.close();
esRef.current = null;
setStatusLabel(`Scan complete — ${d.scanned ?? '?'} items, ${d.errors ?? 0} errors`);
setStatus((prev) => prev ? { ...prev, running: false } : prev);
});
es.addEventListener('error', () => {
es.close();
esRef.current = null;
setStatusLabel('Scan connection lost — refresh to see current status');
setStatus((prev) => prev ? { ...prev, running: false } : prev);
});
return () => es.close();
return es;
};
// Reconnect SSE on page load if scan is already running (skip if already connected)
useEffect(() => {
if (!status?.running || esRef.current) return;
connectSse();
return () => { esRef.current?.close(); esRef.current = null; };
}, [status?.running]);
const startScan = async () => {
setLog([]);
setProgressScanned(0);
setProgressTotal(0);
setErrors(0);
setCurrentItem('');
setStatusLabel('Scan in progress…');
setStatus((prev) => prev ? { ...prev, running: true } : prev);
// Connect SSE before starting the scan so no events are missed
connectSse();
const limitNum = limit ? Number(limit) : undefined;
await api.post('/api/scan/start', limitNum !== undefined ? { limit: limitNum } : {});
setStatus((prev) => prev ? { ...prev, running: true } : prev);
setStatusLabel('Scan in progress…');
setLog([]);
};
const stopScan = async () => {