diff options
| -rw-r--r-- | internal/api/push.go | 15 | ||||
| -rw-r--r-- | internal/api/server.go | 1 | ||||
| -rw-r--r-- | web/app.js | 2 |
3 files changed, 17 insertions, 1 deletions
diff --git a/internal/api/push.go b/internal/api/push.go index 6fd805a..dde5441 100644 --- a/internal/api/push.go +++ b/internal/api/push.go @@ -6,6 +6,7 @@ import ( "github.com/google/uuid" "github.com/thepeterstone/claudomator/internal/storage" + webui "github.com/thepeterstone/claudomator/web" ) // pushSubscriptionStore is the minimal interface needed by push handlers. @@ -37,6 +38,20 @@ func (s *Server) handleGetVAPIDKey(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]string{"public_key": s.vapidPublicKey}) } +// handleServiceWorker serves sw.js with a Service-Worker-Allowed: / header so +// the SW can control the full origin even though it is registered from /api/push/sw.js. +func (s *Server) handleServiceWorker(w http.ResponseWriter, r *http.Request) { + data, err := webui.Files.ReadFile("sw.js") + if err != nil { + http.Error(w, "service worker not found", http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/javascript") + w.Header().Set("Service-Worker-Allowed", "/") + w.WriteHeader(http.StatusOK) + w.Write(data) +} + // handlePushSubscribe saves a new push subscription. func (s *Server) handlePushSubscribe(w http.ResponseWriter, r *http.Request) { var input struct { diff --git a/internal/api/server.go b/internal/api/server.go index 488c500..f640aba 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -134,6 +134,7 @@ func (s *Server) routes() { s.mux.HandleFunc("GET /api/health", s.handleHealth) s.mux.HandleFunc("POST /api/webhooks/github", s.handleGitHubWebhook) s.mux.HandleFunc("GET /api/push/vapid-key", s.handleGetVAPIDKey) + s.mux.HandleFunc("GET /api/push/sw.js", s.handleServiceWorker) s.mux.HandleFunc("POST /api/push/subscribe", s.handlePushSubscribe) s.mux.HandleFunc("DELETE /api/push/subscribe", s.handlePushUnsubscribe) s.mux.HandleFunc("GET /api/drops", s.handleListDrops) @@ -2598,7 +2598,7 @@ function renderStatsPanel(tasks, executions) { async function registerServiceWorker() { if (!('serviceWorker' in navigator) || !('PushManager' in window)) return null; - return navigator.serviceWorker.register('/sw.js'); + return navigator.serviceWorker.register('/api/push/sw.js', { scope: '/' }); } function urlBase64ToUint8Array(base64String) { |
