Skip to content

Modern Docker Management, Designed for Everyone

perf: use service-worker and better caching headers to better manage cache problems after updates

Kyle Mendell3200b395bcef
diff --git a/.github/workflows/build-next.yml b/.github/workflows/build-next.yml
index 90a2efe87..2ce244aa5 100644
--- a/.github/workflows/build-next.yml
+++ b/.github/workflows/build-next.yml
@@ -28,12 +28,6 @@ jobs:
- name: Checkout code
uses: actions/checkout@v5
- - name: Log in to Docker Hub
- uses: docker/login-action@v3
- with:
- username: ${{ secrets.DOCKER_USER }}
- password: ${{ secrets.DOCKER_PAT }}
-
- name: Setup pnpm
uses: pnpm/action-setup@v4
@@ -52,9 +46,6 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- with:
- driver: cloud
- endpoint: "kmendell/ofkm"
- name: Set DOCKER_IMAGE_NAME
run: |
diff --git a/backend/frontend/frontend.go b/backend/frontend/frontend.go
index eca716424..f419061f0 100644
--- a/backend/frontend/frontend.go
+++ b/backend/frontend/frontend.go
@@ -79,6 +79,28 @@ func NewFileServerWithCaching(root http.FileSystem, maxAge int) *FileServerWithC
}
func (f *FileServerWithCaching) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ path := strings.TrimPrefix(r.URL.Path, "/")
+ if path == "" {
+ path = "index.html"
+ }
+
+ // Never cache index.html or service-worker.js - they need to be fresh to detect updates
+ if path == "index.html" || path == "service-worker.js" {
+ w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+ w.Header().Set("Pragma", "no-cache")
+ w.Header().Set("Expires", "0")
+ http.FileServer(f.root).ServeHTTP(w, r)
+ return
+ }
+
+ // For immutable assets (with content hashes), use long-term caching
+ if strings.Contains(path, "/_app/immutable/") {
+ w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
+ http.FileServer(f.root).ServeHTTP(w, r)
+ return
+ }
+
+ // For other static assets, use the configured cache duration
if ifModifiedSince := r.Header.Get("If-Modified-Since"); ifModifiedSince != "" {
ifModifiedSinceTime, err := time.Parse(http.TimeFormat, ifModifiedSince)
if err == nil && f.lastModified.Before(ifModifiedSinceTime.Add(1*time.Second)) {
diff --git a/frontend/src/app.html b/frontend/src/app.html
index 5ec408266..198b50d4a 100644
--- a/frontend/src/app.html
+++ b/frontend/src/app.html
@@ -2,6 +2,9 @@
<html lang="%lang%">
<head>
<meta charset="utf-8" />
+ <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
+ <meta http-equiv="Pragma" content="no-cache" />
+ <meta http-equiv="Expires" content="0" />
<link rel="icon" href="/api/app-images/favicon" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, viewport-fit=cover" />
<link rel="manifest" href="%sveltekit.assets%/app.webmanifest" />
diff --git a/frontend/src/routes/+layout.svelte b/frontend/src/routes/+layout.svelte
index 288f92b45..6003b0b19 100644
--- a/frontend/src/routes/+layout.svelte
+++ b/frontend/src/routes/+layout.svelte
@@ -17,6 +17,8 @@
import MobileFloatingNav from '$lib/components/mobile-nav/mobile-floating-nav.svelte';
import MobileDockedNav from '$lib/components/mobile-nav/mobile-docked-nav.svelte';
import { getEffectiveNavigationSettings, navigationSettingsOverridesStore } from '$lib/utils/navigation.utils';
+ import { browser, dev } from '$app/environment';
+ import { onMount } from 'svelte';
let {
data,
@@ -26,6 +28,12 @@
children: Snippet;
} = $props();
+ onMount(() => {
+ if (!dev && browser && 'serviceWorker' in navigator) {
+ navigator.serviceWorker.register('/service-worker.js');
+ }
+ });
+
const { versionInformation, user, settings } = data;
const isMobile = new IsMobile();
diff --git a/frontend/src/service-worker.ts b/frontend/src/service-worker.ts
new file mode 100644
index 000000000..d7ddb36b6
--- /dev/null
+++ b/frontend/src/service-worker.ts
@@ -0,0 +1,78 @@
+/// <reference no-default-lib="true"/>
+/// <reference lib="esnext" />
+/// <reference lib="webworker" />
+/// <reference types="@sveltejs/kit" />
+
+import { build, files, version } from '$service-worker';
+
+const self = globalThis.self as unknown as ServiceWorkerGlobalScope;
+
+const CACHE = `cache-${version}`;
+
+const ASSETS = [
+ ...build, // the app itself
+ ...files // everything in `static`
+];
+
+self.addEventListener('install', (event) => {
+ async function addFilesToCache() {
+ const cache = await caches.open(CACHE);
+ await cache.addAll(ASSETS);
+ }
+
+ event.waitUntil(addFilesToCache());
+});
+
+self.addEventListener('activate', (event) => {
+ async function deleteOldCaches() {
+ for (const key of await caches.keys()) {
+ if (key !== CACHE) await caches.delete(key);
+ }
+ }
+
+ event.waitUntil(deleteOldCaches());
+});
+
+self.addEventListener('fetch', (event) => {
+ if (event.request.method !== 'GET') return;
+
+ async function respond() {
+ const url = new URL(event.request.url);
+ const cache = await caches.open(CACHE);
+
+ // `build`/`files` can always be served from the cache
+ if (ASSETS.includes(url.pathname)) {
+ const response = await cache.match(url.pathname);
+
+ if (response) {
+ return response;
+ }
+ }
+
+ // for everything else, try the network first, but
+ // fall back to the cache if we're offline
+ try {
+ const response = await fetch(event.request);
+
+ if (!(response instanceof Response)) {
+ throw new Error('invalid response from fetch');
+ }
+
+ if (response.status === 200) {
+ cache.put(event.request, response.clone());
+ }
+
+ return response;
+ } catch (err) {
+ const response = await cache.match(event.request);
+
+ if (response) {
+ return response;
+ }
+
+ throw err;
+ }
+ }
+
+ event.respondWith(respond());
+});