Skip to content

Commit

Permalink
sse: Close connection before page switch
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Feb 20, 2025
1 parent 618e8e9 commit 0f7b0b5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/sse/sse.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ This extension adds support for Server Sent Events to htmx. See /www/extensions
if (htmx.createEventSource == undefined) {
htmx.createEventSource = createEventSource
}

// close SSE connections when page is about to unload
// cf. https://github.com/bigskysoftware/htmx/discussions/2109
window.addEventListener('beforeunload', function() {
document.querySelectorAll('[sse-connect], [hx-sse]').forEach(function(elt) {
var internalData = api.getInternalData(elt);
if (internalData && internalData.sseEventSource) {
internalData.sseEventSource.close();
}
});
});
},

getSelectors: function() {
Expand Down

0 comments on commit 0f7b0b5

Please sign in to comment.