diff --git a/internal/idlewatcher/events.go b/internal/idlewatcher/events.go index bb408671..39d6e23b 100644 --- a/internal/idlewatcher/events.go +++ b/internal/idlewatcher/events.go @@ -55,6 +55,7 @@ func (w *Watcher) clearEventHistory() { } func (w *Watcher) sendEvent(eventType WakeEventType, message string, err error) { + // NOTE: events will be cleared on stop/pause event := w.newWakeEvent(eventType, message, err) w.l.Debug().Str("event", string(eventType)).Str("message", message).Err(err).Msg("sending event") diff --git a/internal/idlewatcher/handle_http.go b/internal/idlewatcher/handle_http.go index efdbe3e8..84f4ba55 100644 --- a/internal/idlewatcher/handle_http.go +++ b/internal/idlewatcher/handle_http.go @@ -15,8 +15,6 @@ import ( _ "unsafe" ) -// FIXME: html and js ccannot be separte - type ForceCacheControl struct { expires string http.ResponseWriter @@ -179,6 +177,7 @@ func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldN if !acceptHTML { serveStaticContent(rw, http.StatusOK, "text/plain", []byte("Container woken")) + return false } // Send a loading response to the client diff --git a/internal/idlewatcher/html/loading.js b/internal/idlewatcher/html/loading.js index aaf903aa..8eb485d0 100644 --- a/internal/idlewatcher/html/loading.js +++ b/internal/idlewatcher/html/loading.js @@ -4,6 +4,11 @@ window.onload = async function () { const consoleEl = document.getElementById("console"); const loadingDotsEl = document.getElementById("loading-dots"); + if (!consoleEl || !loadingDotsEl) { + console.error("Required DOM elements not found"); + return; + } + function formatTimestamp(timestamp) { const date = new Date(timestamp); return date.toLocaleTimeString("en-US", { @@ -34,11 +39,40 @@ window.onload = async function () { consoleEl.scrollTop = consoleEl.scrollHeight; } + if (typeof wakeEventsPath === "undefined" || !wakeEventsPath) { + addConsoleLine( + "error", + "Configuration error: wakeEventsPath not defined", + new Date().toISOString() + ); + loadingDotsEl.style.display = "none"; + return; + } + + if (typeof EventSource === "undefined") { + addConsoleLine( + "error", + "Browser does not support Server-Sent Events", + new Date().toISOString() + ); + loadingDotsEl.style.display = "none"; + return; + } + // Connect to SSE endpoint const eventSource = new EventSource(wakeEventsPath); eventSource.onmessage = function (event) { - const data = JSON.parse(event.data); + try { + const data = JSON.parse(event.data); + } catch (error) { + addConsoleLine( + "error", + "Invalid event data: " + event.data, + new Date().toISOString() + ); + return; + } if (data.type === "ready") { ready = true; diff --git a/internal/idlewatcher/html/loading_page.html b/internal/idlewatcher/html/loading_page.html index f5f82004..1cb990ba 100644 --- a/internal/idlewatcher/html/loading_page.html +++ b/internal/idlewatcher/html/loading_page.html @@ -26,7 +26,7 @@
- +