From 527c1759f9e95bcbf4513c6dcbba4f7dae7c3684 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 23 Nov 2026 16:36:10 +0000 Subject: [PATCH] fix(frontend): revalidate index.html on every load nginx sent index.html without Cache-Control, so browsers cached it heuristically. After a deploy, a browser kept loading the previous hashed bundle until the cache expired, and fixes did not appear. The SPA location now sends Cache-Control: no-cache. Hashed assets stay immutable. scripts/check_frontend_html_cache.sh checks both. --- Dockerfile.frontend | 2 ++ scripts/check_frontend_html_cache.sh | 48 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 scripts/check_frontend_html_cache.sh diff --git a/Dockerfile.frontend b/Dockerfile.frontend index 5a11a10..5ebd8f9 100644 --- a/Dockerfile.frontend +++ b/Dockerfile.frontend @@ -147,6 +147,8 @@ server { # Frontend SPA - serve index.html for all other routes location / { + # index.html names the hashed bundles; a cached copy keeps serving the previous release. + add_header Cache-Control "no-cache" always; try_files $uri $uri/ /index.html; } } diff --git a/scripts/check_frontend_html_cache.sh b/scripts/check_frontend_html_cache.sh new file mode 100755 index 0000000..461c325 --- /dev/null +++ b/scripts/check_frontend_html_cache.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# Builds the frontend image from a Dockerfile and checks that the SPA HTML is revalidated on every +# load while hashed assets stay immutable. A cached index.html keeps old bundles after a deploy. +# Usage: scripts/check_frontend_html_cache.sh [Dockerfile] +set -eu + +dockerfile="${1:-Dockerfile.frontend}" +root="$(git rev-parse --show-toplevel)" +work="$(mktemp -d)" +tag="gohoarder-frontend-cache-check:$$" +front="gohoarder-cache-check-$$" + +cleanup() { + docker rm -f "$front" >/dev/null 2>&1 || true + docker rmi "$tag" >/dev/null 2>&1 || true + rm -rf "$work" +} +trap cleanup EXIT + +mkdir -p "$work/frontend/dist/assets" +echo '' >"$work/frontend/dist/index.html" +echo 'console.log(1)' >"$work/frontend/dist/assets/index-abc123.js" +cp "$root/$dockerfile" "$work/Dockerfile" +docker build -q --load -t "$tag" "$work" >/dev/null +docker run -d --name "$front" -e BACKEND_HOST=127.0.0.1 -e BACKEND_PORT=8080 "$tag" >/dev/null + +headers() { + for _ in 1 2 3 4 5 6 7 8 9 10; do + out="$(docker exec "$front" wget -S -qO /dev/null "http://127.0.0.1$1" 2>&1 || true)" + case "$out" in *"HTTP/"*) printf '%s' "$out" | grep -i 'cache-control' || true; return ;; esac + sleep 1 + done +} + +status=0 +for path in / /index.html /packages /package/go/example.com/mod/v1.0.0; do + cc="$(headers "$path")" + case "$cc" in + *no-cache*) echo "ok: $path is revalidated" ;; + *) echo "FAIL: $path Cache-Control [$cc], want no-cache"; status=1 ;; + esac +done +cc="$(headers /assets/index-abc123.js)" +case "$cc" in +*immutable*) echo "ok: hashed asset is immutable" ;; +*) echo "FAIL: asset Cache-Control [$cc], want immutable"; status=1 ;; +esac +exit "$status"