From a55d495d852cdefaebbc2d83abeef45dc6226dfb Mon Sep 17 00:00:00 2001 From: "Sven R. Kunze" Date: Mon, 8 Mar 2021 21:06:24 +0100 Subject: [PATCH] added check if websocket-related element still exists --- src/htmx.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 6297d071..abbec7a6 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1044,7 +1044,7 @@ return (function () { for (var i = 0; i < values.length; i++) { var value = values[i].split(/:(.+)/); if (value[0] === "connect") { - processWebSocketSource(elt, value[1], 0); + ensureWebSocket(elt, value[1], 0); } if (value[0] === "send") { processWebSocketSend(elt); @@ -1052,7 +1052,11 @@ return (function () { } } - function processWebSocketSource(elt, wssSource, retryCount) { + function ensureWebSocket(elt, wssSource, retryCount) { + if (!bodyContains(elt)) { + return; // stop ensuring websocket connection when socket bearing element ceases to exist + } + if (wssSource.indexOf("/") == 0) { // complete absolute paths only var base_part = location.hostname + (location.port ? ':'+location.port: ''); if (location.protocol == 'https:') { @@ -1071,7 +1075,7 @@ return (function () { if ([1006, 1012, 1013].includes(e.code)) { // Abnormal Closure/Service Restart/Try Again Later var delay = getWebSocketReconnectDelay(retryCount); setTimeout(function() { - processWebSocketSource(elt, wssSource, retryCount+1); // creates a websocket with a new timeout + ensureWebSocket(elt, wssSource, retryCount+1); // creates a websocket with a new timeout }, delay); } };