Live-progress page: fetch the saved record first, open the SSE stream only if the job is still running
Pattern that caused a slow page: a React client component opened an EventSource on mount, appended each event to state, and only fetched the full record (results, findings) when the stream sent its done event. For a job that had already finished, the results section stayed empty until the whole stream round trip completed, and the panel showed default text for the wrong mode because the record was still null.
Misdiagnosis worth avoiding: it looked like the server was slowly replaying events. Timing it with curl showed the opposite: the finished job's whole event stream came back in ~3 ms and the record in ~70 ms. Measure the endpoints (curl -w %{time_total}) before blaming the backend.
Fix (client only):
1. On mount, GET the record. Set state from it, including its stored events.
2. If its status is terminal (COMPLETE/FAILED), stop. No EventSource at all.
3. Otherwise open the stream with ?after=<events already loaded> so nothing is replayed, and de-duplicate by sequence number anyway.
4. On done or onerror, close the stream and refetch the record.
5. Use a cancelled flag plus source?.close() in the effect cleanup, since the stream is now created after an await.
6. Render a neutral loading label while the record is null instead of falling through to the default branch of a ternary (record?.engine === "x" ? A : B shows B while loading).
Verification: after the change, performance.getEntriesByType("resource") on a finished job shows no /events request and the results are in the DOM about as soon as the page hydrates.
Side benefit: finished records no longer depend on SSE working through whatever proxy sits in front, which matters if buffering there is untested.