From ea893b232291602fde0e57de116fe9047bcbeccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Fri, 23 Apr 2021 19:15:13 +0200 Subject: [PATCH 1/2] Skip heartbeat if there is no response --- .../templates/analytics/scripts/page.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/shynet/analytics/templates/analytics/scripts/page.js b/shynet/analytics/templates/analytics/scripts/page.js index 89c4f0a..2c960f5 100644 --- a/shynet/analytics/templates/analytics/scripts/page.js +++ b/shynet/analytics/templates/analytics/scripts/page.js @@ -8,11 +8,18 @@ var Shynet = { idempotency: null, heartbeatTaskId: null, + skipHeartbeat: false, sendHeartbeat: function () { try { if (document.hidden) { return; } + if (Shynet.skipHeartbeat) { + console.warn("Heartbeat skipped due to lack of server response"); + return; + } + + Shynet.skipHeartbeat = true; var xhr = new XMLHttpRequest(); xhr.open( "POST", @@ -20,6 +27,12 @@ var Shynet = { true ); xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onload(function () { + Shynet.skipHeartbeat = false; + }); + xhr.onerror(function () { + Shynet.skipHeartbeat = false; + }); xhr.send( JSON.stringify({ idempotency: Shynet.idempotency, @@ -37,6 +50,7 @@ var Shynet = { clearInterval(Shynet.heartbeatTaskId); } Shynet.idempotency = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); + Shynet.skipHeartbeat = false; Shynet.heartbeatTaskId = setInterval(Shynet.sendHeartbeat, parseInt("{{heartbeat_frequency}}")); Shynet.sendHeartbeat(); } @@ -51,4 +65,4 @@ window.addEventListener("load", Shynet.newPageLoad); // -- START -- {{script_inject|safe}} // -- END -- -{% endif %} \ No newline at end of file +{% endif %} From 69ec37331af31384ac7dc3046a1424e31a5c08ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 25 Apr 2021 17:32:23 +0200 Subject: [PATCH 2/2] Fix xhr callbacks --- shynet/analytics/templates/analytics/scripts/page.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shynet/analytics/templates/analytics/scripts/page.js b/shynet/analytics/templates/analytics/scripts/page.js index 2c960f5..f2c96c1 100644 --- a/shynet/analytics/templates/analytics/scripts/page.js +++ b/shynet/analytics/templates/analytics/scripts/page.js @@ -27,12 +27,12 @@ var Shynet = { true ); xhr.setRequestHeader("Content-Type", "application/json"); - xhr.onload(function () { + xhr.onload = function () { Shynet.skipHeartbeat = false; - }); - xhr.onerror(function () { + }; + xhr.onerror = function () { Shynet.skipHeartbeat = false; - }); + }; xhr.send( JSON.stringify({ idempotency: Shynet.idempotency, @@ -43,7 +43,7 @@ var Shynet = { window.performance.timing.navigationStart, }) ); - } catch (e) { } + } catch (e) {} }, newPageLoad: function () { if (Shynet.heartbeatTaskId != null) {