Skip to content

Commit 703f355

Browse files
committed
PWA: Include minimal no-op service worker for tests photoprism#5274
Signed-off-by: Michael Mayer <[email protected]>
1 parent 0354fad commit 703f355

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

internal/server/routes_webapp.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"net/http"
5+
"os"
56
"path/filepath"
67

78
"github.com/gin-gonic/gin"
@@ -52,7 +53,20 @@ func registerWebAppRoutes(router *gin.Engine, conf *config.Config) {
5253
// Serve user interface service worker file.
5354
swWorker := func(c *gin.Context) {
5455
c.Header(header.CacheControl, header.CacheControlNoStore)
55-
c.File(filepath.Join(conf.BuildPath(), "sw.js"))
56+
57+
if swBuildPath := filepath.Join(conf.BuildPath(), "sw.js"); swBuildPath != "" {
58+
if _, err := os.Stat(swBuildPath); err == nil {
59+
c.File(swBuildPath)
60+
return
61+
}
62+
}
63+
64+
if len(fallbackServiceWorker) > 0 {
65+
c.Data(http.StatusOK, "application/javascript", fallbackServiceWorker)
66+
return
67+
}
68+
69+
c.Status(http.StatusNotFound)
5670
}
5771
router.Any("/sw.js", swWorker)
5872

internal/server/sw_fallback.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package server
2+
3+
import _ "embed"
4+
5+
// fallbackServiceWorker contains a minimal no-op service worker used when a
6+
// Workbox-generated sw.js is not available (for example during tests or when
7+
// frontend assets have not been built yet).
8+
//
9+
//go:embed sw_fallback.js
10+
var fallbackServiceWorker []byte

internal/server/sw_fallback.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Minimal fallback service worker served during development and tests.
2+
// Production builds replace this handler with the Workbox-generated sw.js.
3+
self.addEventListener("install", (event) => {
4+
self.skipWaiting();
5+
});
6+
7+
self.addEventListener("activate", (event) => {
8+
event.waitUntil(self.clients.claim());
9+
});
10+
11+
self.addEventListener("fetch", () => {
12+
// Default network-first behaviour; caching is delegated to the
13+
// Workbox-generated service worker in production builds.
14+
});

0 commit comments

Comments
 (0)