bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.19k stars 1.3k forks source link

SSE stops updating elements after 503 error #2225

Open vikstrous2 opened 9 months ago

vikstrous2 commented 9 months ago

I have a basic SSE updated div. Let's say:

<div hx-ext="sse" sse-connect="/events">
<div sse-swap="TimeComponent"><div>Current time: 01:14:25</div>
</div>

I have a server that responds with an event stream like:

event: TimeComponent
data: <div>Current time: 01:14:26</div>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

event: TimeComponent
data: <div>Current time: 01:14:27</div>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

When I shut down the server and start it back up fast enough for the browser to reconnect without an error, the dom continues to be updated.

If I shut down the server for long enough for the browser to retry and see a 503 error and then start it back up, it htmx still reconnects to the stream, but it doesn't update any dom elements from that point going forward unless I reload the page.

I hope this is enough information to understand the issue. Let me know if you need a more thorough repro.

vlad-tkachenko commented 9 months ago

When 503 error occurs, plugin invokes a logic to create a new EventSource by calling ensureEventSourceOnElement function: https://github.com/bigskysoftware/htmx/blob/master/dist/ext/sse.js#L243

That function on its own calls ensureEventSource that instantiates a new EventSource: https://github.com/bigskysoftware/htmx/blob/master/dist/ext/sse.js#L226

What happens is that new EventSource is completely missing previously added event listeners of a previous EventSource as the logic to add that happens inside the registerSSE function that is not being called anywhere when the new EventSource is being created: https://github.com/bigskysoftware/htmx/blob/master/dist/ext/sse.js#L157