feat: add initial pwa support 58693ffa
Steve · 2026-11-12 12:54 42 file(s) · +403 −5
.gitignore +5 −0
18 18
*.out
19 19
coverage.*
20 20
vendor/
21 +
22 +
# pkg/ holds real shared source code in this monorepo. Some global Go
23 +
# gitignore templates ignore /pkg/, which would silently hide new files
24 +
# under pkg/ — unignore it here so the repo is safe on any machine.
25 +
!pkg/
21 26
apps/blobs/blobs
22 27
apps/bookmarks/bookmarks
23 28
apps/cellar/cellar
apps/bookmarks/go.mod +2 −0
6 6
	github.com/stevedylandev/andromeda/pkg/auth v0.0.0
7 7
	github.com/stevedylandev/andromeda/pkg/config v0.0.0
8 8
	github.com/stevedylandev/andromeda/pkg/darkmatter v0.0.0
9 +
	github.com/stevedylandev/andromeda/pkg/pwa v0.0.0
9 10
	github.com/stevedylandev/andromeda/pkg/sqlite v0.0.0
10 11
	github.com/stevedylandev/andromeda/pkg/web v0.0.0
11 12
	golang.org/x/net v0.41.0
30 31
	github.com/stevedylandev/andromeda/pkg/auth => ../../pkg/auth
31 32
	github.com/stevedylandev/andromeda/pkg/config => ../../pkg/config
32 33
	github.com/stevedylandev/andromeda/pkg/darkmatter => ../../pkg/darkmatter
34 +
	github.com/stevedylandev/andromeda/pkg/pwa => ../../pkg/pwa
33 35
	github.com/stevedylandev/andromeda/pkg/sqlite => ../../pkg/sqlite
34 36
	github.com/stevedylandev/andromeda/pkg/web => ../../pkg/web
35 37
)
apps/bookmarks/routes.go +2 −0
5 5
6 6
	"github.com/stevedylandev/andromeda/pkg/auth"
7 7
	"github.com/stevedylandev/andromeda/pkg/darkmatter"
8 +
	"github.com/stevedylandev/andromeda/pkg/pwa"
8 9
	"github.com/stevedylandev/andromeda/pkg/web"
9 10
)
10 11
35 36
36 37
	mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static"))
37 38
	darkmatter.Mount(mux, "/assets")
39 +
	pwa.Mount(mux)
38 40
	return mux
39 41
}
apps/bookmarks/static/site.webmanifest +23 −1
1 -
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
1 +
{
2 +
  "name": "Bookmarks",
3 +
  "short_name": "Bookmarks",
4 +
  "description": "Personal link saver organized by category",
5 +
  "id": "/",
6 +
  "start_url": "/",
7 +
  "scope": "/",
8 +
  "icons": [
9 +
    {
10 +
      "src": "/static/android-chrome-192x192.png",
11 +
      "sizes": "192x192",
12 +
      "type": "image/png"
13 +
    },
14 +
    {
15 +
      "src": "/static/android-chrome-512x512.png",
16 +
      "sizes": "512x512",
17 +
      "type": "image/png"
18 +
    }
19 +
  ],
20 +
  "theme_color": "#121113",
21 +
  "background_color": "#121113",
22 +
  "display": "standalone"
23 +
}
apps/bookmarks/templates/admin.html +1 −0
126 126
      </ul>
127 127
      {{end}}
128 128
    </section>
129 +
    <script src="/pwa.js" defer></script>
129 130
  </body>
130 131
</html>
apps/bookmarks/templates/index.html +1 −0
55 55
    </section>
56 56
    {{end}}
57 57
    {{end}}
58 +
    <script src="/pwa.js" defer></script>
58 59
  </body>
59 60
</html>
apps/bookmarks/templates/login.html +1 −0
30 30
        <button type="submit">Login</button>
31 31
      </div>
32 32
    </form>
33 +
    <script src="/pwa.js" defer></script>
33 34
  </body>
34 35
</html>
apps/cellar/go.mod +2 −0
6 6
	github.com/stevedylandev/andromeda/pkg/auth v0.0.0
7 7
	github.com/stevedylandev/andromeda/pkg/config v0.0.0
8 8
	github.com/stevedylandev/andromeda/pkg/darkmatter v0.0.0
9 +
	github.com/stevedylandev/andromeda/pkg/pwa v0.0.0
9 10
	github.com/stevedylandev/andromeda/pkg/sqlite v0.0.0
10 11
	github.com/stevedylandev/andromeda/pkg/web v0.0.0
11 12
)
29 30
	github.com/stevedylandev/andromeda/pkg/auth => ../../pkg/auth
30 31
	github.com/stevedylandev/andromeda/pkg/config => ../../pkg/config
31 32
	github.com/stevedylandev/andromeda/pkg/darkmatter => ../../pkg/darkmatter
33 +
	github.com/stevedylandev/andromeda/pkg/pwa => ../../pkg/pwa
32 34
	github.com/stevedylandev/andromeda/pkg/sqlite => ../../pkg/sqlite
33 35
	github.com/stevedylandev/andromeda/pkg/web => ../../pkg/web
34 36
)
apps/cellar/routes.go +2 −0
4 4
	"net/http"
5 5
6 6
	"github.com/stevedylandev/andromeda/pkg/darkmatter"
7 +
	"github.com/stevedylandev/andromeda/pkg/pwa"
7 8
	"github.com/stevedylandev/andromeda/pkg/web"
8 9
)
9 10
57 58
	// Static
58 59
	mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static"))
59 60
	darkmatter.Mount(mux, "/assets")
61 +
	pwa.Mount(mux)
60 62
	return mux
61 63
}
apps/cellar/static/site.webmanifest +23 −1
1 -
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
1 +
{
2 +
  "name": "Cellar",
3 +
  "short_name": "Cellar",
4 +
  "description": "Wine tasting log and wishlist",
5 +
  "id": "/",
6 +
  "start_url": "/",
7 +
  "scope": "/",
8 +
  "icons": [
9 +
    {
10 +
      "src": "/static/android-chrome-192x192.png",
11 +
      "sizes": "192x192",
12 +
      "type": "image/png"
13 +
    },
14 +
    {
15 +
      "src": "/static/android-chrome-512x512.png",
16 +
      "sizes": "512x512",
17 +
      "type": "image/png"
18 +
    }
19 +
  ],
20 +
  "theme_color": "#121113",
21 +
  "background_color": "#121113",
22 +
  "display": "standalone"
23 +
}
apps/cellar/templates/base.html +1 −0
25 25
  <main>
26 26
    {{block "content" .}}{{end}}
27 27
  </main>
28 +
  <script src="/pwa.js" defer></script>
28 29
</body>
29 30
</html>{{end}}
apps/cellar/templates/login.html +1 −0
20 20
      <button type="submit">login</button>
21 21
    </form>
22 22
  </main>
23 +
  <script src="/pwa.js" defer></script>
23 24
</body>
24 25
</html>{{end}}
apps/easel/go.mod +2 −0
5 5
require (
6 6
	github.com/stevedylandev/andromeda/pkg/config v0.0.0
7 7
	github.com/stevedylandev/andromeda/pkg/darkmatter v0.0.0
8 +
	github.com/stevedylandev/andromeda/pkg/pwa v0.0.0
8 9
	github.com/stevedylandev/andromeda/pkg/sqlite v0.0.0
9 10
	github.com/stevedylandev/andromeda/pkg/web v0.0.0
10 11
)
26 27
replace (
27 28
	github.com/stevedylandev/andromeda/pkg/config => ../../pkg/config
28 29
	github.com/stevedylandev/andromeda/pkg/darkmatter => ../../pkg/darkmatter
30 +
	github.com/stevedylandev/andromeda/pkg/pwa => ../../pkg/pwa
29 31
	github.com/stevedylandev/andromeda/pkg/sqlite => ../../pkg/sqlite
30 32
	github.com/stevedylandev/andromeda/pkg/web => ../../pkg/web
31 33
)
apps/easel/routes.go +2 −0
4 4
	"net/http"
5 5
6 6
	"github.com/stevedylandev/andromeda/pkg/darkmatter"
7 +
	"github.com/stevedylandev/andromeda/pkg/pwa"
7 8
	"github.com/stevedylandev/andromeda/pkg/web"
8 9
)
9 10
18 19
	mux.HandleFunc("GET /api/archive", a.withCORS(a.apiArchive))
19 20
	mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static"))
20 21
	darkmatter.Mount(mux, "/assets")
22 +
	pwa.Mount(mux)
21 23
	return mux
22 24
}
23 25
apps/easel/static/site.webmanifest +23 −1
1 -
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
1 +
{
2 +
  "name": "Easel",
3 +
  "short_name": "Easel",
4 +
  "description": "A daily painting from the Art Institute of Chicago",
5 +
  "id": "/",
6 +
  "start_url": "/",
7 +
  "scope": "/",
8 +
  "icons": [
9 +
    {
10 +
      "src": "/static/android-chrome-192x192.png",
11 +
      "sizes": "192x192",
12 +
      "type": "image/png"
13 +
    },
14 +
    {
15 +
      "src": "/static/android-chrome-512x512.png",
16 +
      "sizes": "512x512",
17 +
      "type": "image/png"
18 +
    }
19 +
  ],
20 +
  "theme_color": "#121113",
21 +
  "background_color": "#121113",
22 +
  "display": "standalone"
23 +
}
apps/easel/templates/base.html +1 −0
31 31
    <main>
32 32
      {{block "content" .}}{{end}}
33 33
    </main>
34 +
    <script src="/pwa.js" defer></script>
34 35
  </body>
35 36
</html>{{end}}
36 37
apps/kepler/go.mod +2 −0
5 5
require (
6 6
	github.com/go-git/go-git/v5 v5.13.1
7 7
	github.com/stevedylandev/andromeda/pkg/config v0.0.0
8 +
	github.com/stevedylandev/andromeda/pkg/pwa v0.0.0
8 9
	github.com/stevedylandev/andromeda/pkg/web v0.0.0
9 10
	github.com/yuin/goldmark v1.7.8
10 11
)
36 37
37 38
replace (
38 39
	github.com/stevedylandev/andromeda/pkg/config => ../../pkg/config
40 +
	github.com/stevedylandev/andromeda/pkg/pwa => ../../pkg/pwa
39 41
	github.com/stevedylandev/andromeda/pkg/web => ../../pkg/web
40 42
)
apps/kepler/routes.go +4 −0
4 4
	"mime"
5 5
	"net/http"
6 6
	"path"
7 +
8 +
	"github.com/stevedylandev/andromeda/pkg/pwa"
7 9
)
8 10
9 11
func (a *App) routes() *http.ServeMux {
55 57
	} {
56 58
		mux.HandleFunc("GET /assets/"+name, staticAsset)
57 59
	}
60 +
61 +
	pwa.Mount(mux)
58 62
59 63
	mux.HandleFunc("GET /api/repos", a.apiReposHandler)
60 64
apps/kepler/static/site.webmanifest +23 −1
1 -
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
1 +
{
2 +
  "name": "Kepler",
3 +
  "short_name": "Kepler",
4 +
  "description": "Read-only web view for on-disk git repositories",
5 +
  "id": "/",
6 +
  "start_url": "/",
7 +
  "scope": "/",
8 +
  "icons": [
9 +
    {
10 +
      "src": "/assets/android-chrome-192x192.png",
11 +
      "sizes": "192x192",
12 +
      "type": "image/png"
13 +
    },
14 +
    {
15 +
      "src": "/assets/android-chrome-512x512.png",
16 +
      "sizes": "512x512",
17 +
      "type": "image/png"
18 +
    }
19 +
  ],
20 +
  "theme_color": "#121113",
21 +
  "background_color": "#121113",
22 +
  "display": "standalone"
23 +
}
apps/kepler/templates/base.html +1 −0
35 35
            Powered by <a href="https://andromeda.build/apps/kepler">Kepler</a>
36 36
        </footer>
37 37
    </div>
38 +
    <script src="/pwa.js" defer></script>
38 39
</body>
39 40
</html>{{end}}
apps/library/go.mod +2 −0
6 6
	github.com/stevedylandev/andromeda/pkg/auth v0.0.0
7 7
	github.com/stevedylandev/andromeda/pkg/config v0.0.0
8 8
	github.com/stevedylandev/andromeda/pkg/darkmatter v0.0.0
9 +
	github.com/stevedylandev/andromeda/pkg/pwa v0.0.0
9 10
	github.com/stevedylandev/andromeda/pkg/sqlite v0.0.0
10 11
	github.com/stevedylandev/andromeda/pkg/web v0.0.0
11 12
)
29 30
	github.com/stevedylandev/andromeda/pkg/auth => ../../pkg/auth
30 31
	github.com/stevedylandev/andromeda/pkg/config => ../../pkg/config
31 32
	github.com/stevedylandev/andromeda/pkg/darkmatter => ../../pkg/darkmatter
33 +
	github.com/stevedylandev/andromeda/pkg/pwa => ../../pkg/pwa
32 34
	github.com/stevedylandev/andromeda/pkg/sqlite => ../../pkg/sqlite
33 35
	github.com/stevedylandev/andromeda/pkg/web => ../../pkg/web
34 36
)
apps/library/routes.go +2 −0
4 4
	"net/http"
5 5
6 6
	"github.com/stevedylandev/andromeda/pkg/darkmatter"
7 +
	"github.com/stevedylandev/andromeda/pkg/pwa"
7 8
	"github.com/stevedylandev/andromeda/pkg/web"
8 9
)
9 10
17 18
	mux.HandleFunc("GET /", a.indexHandler)
18 19
	mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static"))
19 20
	darkmatter.Mount(mux, "/assets")
21 +
	pwa.Mount(mux)
20 22
21 23
	mux.HandleFunc("GET /admin/login", a.loginGetHandler)
22 24
	mux.HandleFunc("POST /admin/login", a.loginPostHandler)
apps/library/static/site.webmanifest +23 −1
1 -
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
1 +
{
2 +
  "name": "Library",
3 +
  "short_name": "Library",
4 +
  "description": "Personal book tracker",
5 +
  "id": "/",
6 +
  "start_url": "/",
7 +
  "scope": "/",
8 +
  "icons": [
9 +
    {
10 +
      "src": "/static/android-chrome-192x192.png",
11 +
      "sizes": "192x192",
12 +
      "type": "image/png"
13 +
    },
14 +
    {
15 +
      "src": "/static/android-chrome-512x512.png",
16 +
      "sizes": "512x512",
17 +
      "type": "image/png"
18 +
    }
19 +
  ],
20 +
  "theme_color": "#121113",
21 +
  "background_color": "#121113",
22 +
  "display": "standalone"
23 +
}
apps/library/templates/admin.html +1 −0
358 358
        document.getElementById('scan-modal').hidden = true;
359 359
      }
360 360
    </script>
361 +
    <script src="/pwa.js" defer></script>
361 362
  </body>
362 363
</html>
apps/library/templates/admin_book.html +1 −0
111 111
      {{end}}
112 112
      {{end}}
113 113
    </section>
114 +
    <script src="/pwa.js" defer></script>
114 115
  </body>
115 116
</html>
apps/library/templates/admin_quotes.html +1 −0
89 89
      {{end}}
90 90
      {{end}}
91 91
    </section>
92 +
    <script src="/pwa.js" defer></script>
92 93
  </body>
93 94
</html>
apps/library/templates/book.html +1 −0
48 48
      </div>
49 49
      {{end}}
50 50
    </section>
51 +
    <script src="/pwa.js" defer></script>
51 52
  </body>
52 53
</html>
apps/library/templates/index.html +1 −0
71 71
    </div>
72 72
    {{end}}
73 73
    {{end}}
74 +
    <script src="/pwa.js" defer></script>
74 75
  </body>
75 76
</html>
apps/library/templates/login.html +1 −0
22 22
      <input type="password" id="password" name="password" required autofocus />
23 23
      <button type="submit">Login</button>
24 24
    </form>
25 +
    <script src="/pwa.js" defer></script>
25 26
  </body>
26 27
</html>
apps/library/templates/quote.html +1 −0
25 25
      {{if .Quote.Page}}<p class="quote-page-meta">p. {{.Quote.Page}}</p>{{end}}
26 26
      <p class="quote-page-meta">from <a href="/book/{{.Book.ID}}">{{.Book.Title}}</a> by {{.Book.Authors}}</p>
27 27
    </article>
28 +
    <script src="/pwa.js" defer></script>
28 29
  </body>
29 30
</html>
apps/library/templates/quotes.html +1 −0
40 40
    </section>
41 41
    {{end}}
42 42
    {{end}}
43 +
    <script src="/pwa.js" defer></script>
43 44
  </body>
44 45
</html>
apps/quotes/go.mod +2 −0
6 6
	github.com/stevedylandev/andromeda/pkg/auth v0.0.0
7 7
	github.com/stevedylandev/andromeda/pkg/config v0.0.0
8 8
	github.com/stevedylandev/andromeda/pkg/darkmatter v0.0.0
9 +
	github.com/stevedylandev/andromeda/pkg/pwa v0.0.0
9 10
	github.com/stevedylandev/andromeda/pkg/sqlite v0.0.0
10 11
	github.com/stevedylandev/andromeda/pkg/web v0.0.0
11 12
)
29 30
	github.com/stevedylandev/andromeda/pkg/auth => ../../pkg/auth
30 31
	github.com/stevedylandev/andromeda/pkg/config => ../../pkg/config
31 32
	github.com/stevedylandev/andromeda/pkg/darkmatter => ../../pkg/darkmatter
33 +
	github.com/stevedylandev/andromeda/pkg/pwa => ../../pkg/pwa
32 34
	github.com/stevedylandev/andromeda/pkg/sqlite => ../../pkg/sqlite
33 35
	github.com/stevedylandev/andromeda/pkg/web => ../../pkg/web
34 36
)
apps/quotes/routes.go +2 −0
4 4
	"net/http"
5 5
6 6
	"github.com/stevedylandev/andromeda/pkg/darkmatter"
7 +
	"github.com/stevedylandev/andromeda/pkg/pwa"
7 8
	"github.com/stevedylandev/andromeda/pkg/web"
8 9
)
9 10
18 19
	mux.HandleFunc("GET /random", a.randomHandler)
19 20
	mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static"))
20 21
	darkmatter.Mount(mux, "/assets")
22 +
	pwa.Mount(mux)
21 23
22 24
	mux.HandleFunc("GET /admin/login", a.loginGetHandler)
23 25
	mux.HandleFunc("POST /admin/login", a.loginPostHandler)
apps/quotes/static/site.webmanifest +4 −0
1 1
{
2 2
  "name": "Quotes",
3 3
  "short_name": "Quotes",
4 +
  "description": "A quote a day from classic literature",
5 +
  "id": "/",
6 +
  "start_url": "/",
7 +
  "scope": "/",
4 8
  "icons": [
5 9
    {
6 10
      "src": "/static/android-chrome-192x192.png",
apps/quotes/templates/admin.html +1 −0
79 79
    <p class="empty">Showing the 50 most recent. Use search to find older quotes.</p>
80 80
    {{end}}
81 81
    {{end}}
82 +
    <script src="/pwa.js" defer></script>
82 83
  </body>
83 84
</html>
apps/quotes/templates/index.html +1 −0
35 35
      <p class="attribution">No quotes yet.</p>
36 36
      {{end}}
37 37
    </main>
38 +
    <script src="/pwa.js" defer></script>
38 39
  </body>
39 40
</html>
apps/quotes/templates/login.html +1 −0
27 27
        <button type="submit">Login</button>
28 28
      </div>
29 29
    </form>
30 +
    <script src="/pwa.js" defer></script>
30 31
  </body>
31 32
</html>
pkg/pwa/go.mod (added) +3 −0
1 +
module github.com/stevedylandev/andromeda/pkg/pwa
2 +
3 +
go 1.24.4
pkg/pwa/pwa.go (added) +48 −0
1 +
// Package pwa provides the shared service worker, registration script, and
2 +
// offline fallback page used by andromeda apps so read-only pages keep
3 +
// working offline.
4 +
//
5 +
// Apps opt in by calling Mount in their route setup and adding
6 +
// <script src="/pwa.js" defer></script> to their templates. Their
7 +
// site.webmanifest should include start_url, scope, and id.
8 +
package pwa
9 +
10 +
import (
11 +
	"embed"
12 +
	"net/http"
13 +
)
14 +
15 +
//go:embed static/sw.js static/pwa.js static/offline.html
16 +
var files embed.FS
17 +
18 +
// Mount registers the PWA routes on mux:
19 +
//   - GET /sw.js    the service worker (served from the root so its default
20 +
//     scope covers the whole origin)
21 +
//   - GET /pwa.js   the registration script included by app templates
22 +
//   - GET /offline  fallback page shown when an uncached navigation fails
23 +
func Mount(mux *http.ServeMux) {
24 +
	mux.HandleFunc("GET /sw.js", func(w http.ResponseWriter, r *http.Request) {
25 +
		serve(w, "static/sw.js", "application/javascript; charset=utf-8", true)
26 +
	})
27 +
	mux.HandleFunc("GET /pwa.js", func(w http.ResponseWriter, r *http.Request) {
28 +
		serve(w, "static/pwa.js", "application/javascript; charset=utf-8", true)
29 +
	})
30 +
	mux.HandleFunc("GET /offline", func(w http.ResponseWriter, r *http.Request) {
31 +
		serve(w, "static/offline.html", "text/html; charset=utf-8", false)
32 +
	})
33 +
}
34 +
35 +
func serve(w http.ResponseWriter, name, contentType string, noCache bool) {
36 +
	data, err := files.ReadFile(name)
37 +
	if err != nil {
38 +
		http.Error(w, "not found", http.StatusNotFound)
39 +
		return
40 +
	}
41 +
	w.Header().Set("Content-Type", contentType)
42 +
	if noCache {
43 +
		// Browsers cache service worker scripts for up to 24h by default.
44 +
		// Always revalidate so worker updates ship with each binary.
45 +
		w.Header().Set("Cache-Control", "no-cache")
46 +
	}
47 +
	_, _ = w.Write(data)
48 +
}
pkg/pwa/static/offline.html (added) +34 −0
1 +
<!doctype html>
2 +
<html lang="en">
3 +
<head>
4 +
  <meta charset="UTF-8">
5 +
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 +
  <title>Offline</title>
7 +
  <meta name="theme-color" content="#121113">
8 +
  <style>
9 +
    /* Inline only: this page must render with zero network requests. */
10 +
    * { margin: 0; padding: 0; box-sizing: border-box; }
11 +
    body {
12 +
      background: #121113;
13 +
      color: #e8e6e3;
14 +
      font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
15 +
      min-height: 100vh;
16 +
      display: flex;
17 +
      align-items: center;
18 +
      justify-content: center;
19 +
      padding: 2rem;
20 +
    }
21 +
    main { max-width: 34rem; text-align: center; }
22 +
    h1 { font-size: 1.25rem; margin-bottom: 1rem; }
23 +
    p { font-size: 0.875rem; line-height: 1.6; color: #a8a5a1; margin-bottom: 0.75rem; }
24 +
    a { color: #e8e6e3; }
25 +
  </style>
26 +
</head>
27 +
<body>
28 +
  <main>
29 +
    <h1>You're offline</h1>
30 +
    <p>This page hasn't been visited while online, so it isn't available offline yet.</p>
31 +
    <p><a href="/">Go to the home page</a> &mdash; if you've visited it before, it will load from cache.</p>
32 +
  </main>
33 +
</body>
34 +
</html>
pkg/pwa/static/pwa.js (added) +13 −0
1 +
// Registers the shared andromeda service worker. Included by app templates
2 +
// as <script src="/pwa.js" defer></script>.
3 +
//
4 +
// Note: service workers require a secure context (https, or localhost).
5 +
// Registration over plain http on a LAN IP will fail — it is logged to the
6 +
// console rather than thrown so production users aren't spammed.
7 +
if ('serviceWorker' in navigator) {
8 +
  window.addEventListener('load', () => {
9 +
    navigator.serviceWorker
10 +
      .register('/sw.js')
11 +
      .catch((err) => console.warn('service worker registration failed:', err.message));
12 +
  });
13 +
}
pkg/pwa/static/sw.js (added) +137 −0
1 +
// Shared andromeda service worker.
2 +
//
3 +
// Strategy:
4 +
//   - HTML navigations: network-first. Successful responses are cached, so
5 +
//     any page visited while online keeps working offline. Uncached pages
6 +
//     fall back to /offline.
7 +
//   - Static assets (css/js/fonts/images): stale-while-revalidate.
8 +
//   - Auth surfaces (/admin, /login, /logout), APIs, non-GET requests, and
9 +
//     cross-origin requests are never intercepted or cached.
10 +
//
11 +
// Bump CACHE_VERSION to invalidate all caches when this file changes.
12 +
13 +
const CACHE_VERSION = 'v1';
14 +
const SHELL_CACHE = 'andromeda-shell-' + CACHE_VERSION;
15 +
const PAGES_CACHE = 'andromeda-pages-' + CACHE_VERSION;
16 +
const ASSETS_CACHE = 'andromeda-assets-' + CACHE_VERSION;
17 +
const MAX_PAGE_ENTRIES = 60;
18 +
19 +
const OFFLINE_URL = '/offline';
20 +
21 +
// App-shell assets attempted at install. Entries missing in a given app are
22 +
// skipped, so the same worker works across apps with different layouts.
23 +
const SHELL_ASSETS = [
24 +
  OFFLINE_URL,
25 +
  '/static/site.webmanifest',
26 +
  '/static/styles.css',
27 +
  '/assets/darkmatter.css',
28 +
  '/assets/kepler.css',
29 +
];
30 +
31 +
// Requests the worker never touches.
32 +
const SKIP_PATTERNS = [
33 +
  /^\/admin(\/|$)/,
34 +
  /^\/login$/,
35 +
  /^\/logout$/,
36 +
  /^\/api\//,
37 +
];
38 +
39 +
self.addEventListener('install', (event) => {
40 +
  event.waitUntil(
41 +
    (async () => {
42 +
      const cache = await caches.open(SHELL_CACHE);
43 +
      await Promise.allSettled(
44 +
        SHELL_ASSETS.map(async (url) => {
45 +
          const res = await fetch(url, { cache: 'reload' });
46 +
          if (res.ok) await cache.put(url, res);
47 +
        })
48 +
      );
49 +
      await self.skipWaiting();
50 +
    })()
51 +
  );
52 +
});
53 +
54 +
self.addEventListener('activate', (event) => {
55 +
  event.waitUntil(
56 +
    (async () => {
57 +
      const keep = [SHELL_CACHE, PAGES_CACHE, ASSETS_CACHE];
58 +
      const names = await caches.keys();
59 +
      await Promise.all(
60 +
        names.filter((n) => !keep.includes(n)).map((n) => caches.delete(n))
61 +
      );
62 +
      await self.clients.claim();
63 +
    })()
64 +
  );
65 +
});
66 +
67 +
self.addEventListener('fetch', (event) => {
68 +
  if (!shouldHandle(event.request)) return;
69 +
  if (event.request.mode === 'navigate') {
70 +
    event.respondWith(handleNavigation(event.request));
71 +
  } else {
72 +
    event.respondWith(staleWhileRevalidate(event.request));
73 +
  }
74 +
});
75 +
76 +
function shouldHandle(request) {
77 +
  if (request.method !== 'GET') return false;
78 +
  const url = new URL(request.url);
79 +
  if (url.origin !== self.location.origin) return false;
80 +
  return !SKIP_PATTERNS.some((re) => re.test(url.pathname));
81 +
}
82 +
83 +
// Network-first for pages: fresh when online, cached copy when offline.
84 +
async function handleNavigation(request) {
85 +
  const cache = await caches.open(PAGES_CACHE);
86 +
  try {
87 +
    const response = await fetch(request);
88 +
    if (response.ok) {
89 +
      cache.put(request, response.clone());
90 +
      trimCache(cache, MAX_PAGE_ENTRIES);
91 +
    }
92 +
    return response;
93 +
  } catch (err) {
94 +
    const cached = await cache.match(request);
95 +
    if (cached) return cached;
96 +
    const shell = await caches.open(SHELL_CACHE);
97 +
    const offline = await shell.match(OFFLINE_URL);
98 +
    return offline || Response.error();
99 +
  }
100 +
}
101 +
102 +
// Stale-while-revalidate for static assets.
103 +
async function staleWhileRevalidate(request) {
104 +
  const cache = await caches.open(ASSETS_CACHE);
105 +
  const cached = await cache.match(request);
106 +
  const network = fetch(request)
107 +
    .then((response) => {
108 +
      if (response.ok && isCacheableAsset(response)) {
109 +
        cache.put(request, response.clone());
110 +
      }
111 +
      return response;
112 +
    })
113 +
    .catch(() => null);
114 +
  if (cached) return cached;
115 +
  const response = await network;
116 +
  return response || new Response('offline', { status: 503, statusText: 'Offline' });
117 +
}
118 +
119 +
function isCacheableAsset(response) {
120 +
  const type = (response.headers.get('content-type') || '').toLowerCase();
121 +
  return (
122 +
    type.startsWith('text/css') ||
123 +
    type.startsWith('image/') ||
124 +
    type.startsWith('font/') ||
125 +
    type.startsWith('application/javascript') ||
126 +
    type.startsWith('text/javascript')
127 +
  );
128 +
}
129 +
130 +
// Keep the pages cache bounded by evicting the oldest entries.
131 +
async function trimCache(cache, max) {
132 +
  const keys = await cache.keys();
133 +
  if (keys.length <= max) return;
134 +
  for (const key of keys.slice(0, keys.length - max)) {
135 +
    await cache.delete(key);
136 +
  }
137 +
}