From d77f4eb59f6cf4f5474d1706f8e68933963eda5e Mon Sep 17 00:00:00 2001 From: James Brechtel Date: Wed, 20 May 2026 15:42:01 -0400 Subject: [PATCH] fix: persist SSE debounce across page reloads via sessionStorage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The in-memory lastReload variable was lost on location.reload() because the JavaScript context is destroyed. This caused a reload loop when the watcher fired multiple SSE events for the same file change — each reload reset the debounce to 0, so the next event would trigger another reload. Switch to sessionStorage, which persists across page loads within the same browser tab session. Also increases the debounce window to 3000ms to account for page load time. --- src/Roux/Html.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Roux/Html.hs b/src/Roux/Html.hs index 625fa48..4dde796 100644 --- a/src/Roux/Html.hs +++ b/src/Roux/Html.hs @@ -351,12 +351,12 @@ sseRecipeJs = , "if (!el) return;" , "var currentRecipe = el.textContent;" , "var es = new EventSource('/events');" - , "var lastReload = 0;" + , "var lastReload = parseInt(sessionStorage.getItem('roux-last-reload') || '0', 10);" , "es.addEventListener('recipe-changed', function(e) {" , " if (e.data !== currentRecipe) return;" , " var now = Date.now();" - , " if (now - lastReload < 2000) return;" - , " lastReload = now;" + , " if (now - lastReload < 3000) return;" + , " sessionStorage.setItem('roux-last-reload', String(now));" , " location.reload();" , "});" , "})();" @@ -370,11 +370,11 @@ sseIndexJs = , "'use strict';" , "if (!window.EventSource) return;" , "var es = new EventSource('/events');" - , "var lastReload = 0;" + , "var lastReload = parseInt(sessionStorage.getItem('roux-last-reload') || '0', 10);" , "es.addEventListener('recipe-changed', function() {" , " var now = Date.now();" - , " if (now - lastReload < 2000) return;" - , " lastReload = now;" + , " if (now - lastReload < 3000) return;" + , " sessionStorage.setItem('roux-last-reload', String(now));" , " location.reload();" , "});" , "})();"