From 7fefb833a9eebce28f413a940a741d09a22e7777 Mon Sep 17 00:00:00 2001 From: johnnyjoygh Date: Sat, 14 Nov 2026 23:34:31 +0800 Subject: [PATCH] fix(frontend): prevent caching missing static assets --- server/frontend/frontend.go | 1 + server/frontend/frontend_test.go | 22 +++++++++++++++++----- server/testdata/http_compat.jsonl | 6 +++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/server/frontend/frontend.go b/server/frontend/frontend.go index 2be538b87..1040ee103 100644 --- a/server/frontend/frontend.go +++ b/server/frontend/frontend.go @@ -65,6 +65,7 @@ func (*FrontendService) Handler(fallback http.Handler) http.Handler { } if err != nil { if !shouldServeFrontendHTML(requestPath) { + w.Header().Set("Cache-Control", "no-store") return &httphandler.Error{Code: http.StatusNotFound, Message: http.StatusText(http.StatusNotFound)} } filename = "index.html" diff --git a/server/frontend/frontend_test.go b/server/frontend/frontend_test.go index b87566b7f..eaf707ea2 100644 --- a/server/frontend/frontend_test.go +++ b/server/frontend/frontend_test.go @@ -120,18 +120,30 @@ func TestFrontendService_StaticCacheHeaders(t *testing.T) { } } -func TestFrontendService_MissingAssetDoesNotFallbackToIndex(t *testing.T) { +func TestFrontendService_MissingAssetsAreNotCached(t *testing.T) { ctx := context.Background() testStore := teststore.NewTestingStore(ctx, t) e := http.NewServeMux() NewFrontendService(&profile.Profile{}, testStore).Serve(ctx, e, http.HandlerFunc(httphandler.NotFound)) - req := httptest.NewRequest(http.MethodGet, "/assets/missing.js", nil) - rec := httptest.NewRecorder() - e.ServeHTTP(rec, req) + for _, requestPath := range []string{"/assets/missing-deadbeef.js", "/assets/missing-deadbeef.css", "/missing.png"} { + for _, method := range []string{http.MethodGet, http.MethodHead} { + t.Run(method+" "+requestPath, func(t *testing.T) { + req := httptest.NewRequest(method, requestPath, nil) + rec := httptest.NewRecorder() + SetCacheHeaders(rec, req) + e.ServeHTTP(rec, req) - require.Equal(t, http.StatusNotFound, rec.Code) + require.Equal(t, http.StatusNotFound, rec.Code) + require.Equal(t, "no-store", rec.Result().Header.Get("Cache-Control")) + require.NotContains(t, rec.Header().Get("Content-Type"), "text/html") + if method == http.MethodHead { + require.Empty(t, rec.Body.String()) + } + }) + } + } } func TestFrontendService_HiddenPathsDoNotFallbackToIndex(t *testing.T) { diff --git a/server/testdata/http_compat.jsonl b/server/testdata/http_compat.jsonl index 95368f8e6..3996b7b39 100644 --- a/server/testdata/http_compat.jsonl +++ b/server/testdata/http_compat.jsonl @@ -90,9 +90,9 @@ {"Method":"HEAD","Path":"/memos/x","RequestHeader":null,"Status":200,"Header":{"Accept-Ranges":["bytes"],"Cache-Control":["no-cache, no-store, must-revalidate"],"Content-Type":["text/html; charset=utf-8"],"Expires":["0"],"Pragma":["no-cache"],"Vary":["Origin"]},"Body":""} {"Method":"POST","Path":"/memos/x","RequestHeader":null,"Status":200,"Header":{"Accept-Ranges":["bytes"],"Cache-Control":["no-cache, no-store, must-revalidate"],"Content-Type":["text/html; charset=utf-8"],"Expires":["0"],"Pragma":["no-cache"],"Vary":["Origin"]},"Body":"\u003cfrontend HTML\u003e"} {"Method":"OPTIONS","Path":"/memos/x","RequestHeader":null,"Status":204,"Header":{"Vary":["Origin"]},"Body":""} -{"Method":"GET","Path":"/assets/missing.js","RequestHeader":null,"Status":404,"Header":{"Cache-Control":["public, max-age=2592000, immutable"],"Content-Type":["application/json"],"Vary":["Origin"]},"Body":"{\"message\":\"Not Found\"}\n"} -{"Method":"HEAD","Path":"/assets/missing.js","RequestHeader":null,"Status":404,"Header":{"Cache-Control":["public, max-age=2592000, immutable"],"Vary":["Origin"]},"Body":""} -{"Method":"POST","Path":"/assets/missing.js","RequestHeader":null,"Status":404,"Header":{"Cache-Control":["public, max-age=2592000, immutable"],"Content-Type":["application/json"],"Vary":["Origin"]},"Body":"{\"message\":\"Not Found\"}\n"} +{"Method":"GET","Path":"/assets/missing.js","RequestHeader":null,"Status":404,"Header":{"Cache-Control":["no-store"],"Content-Type":["application/json"],"Vary":["Origin"]},"Body":"{\"message\":\"Not Found\"}\n"} +{"Method":"HEAD","Path":"/assets/missing.js","RequestHeader":null,"Status":404,"Header":{"Cache-Control":["no-store"],"Vary":["Origin"]},"Body":""} +{"Method":"POST","Path":"/assets/missing.js","RequestHeader":null,"Status":404,"Header":{"Cache-Control":["no-store"],"Content-Type":["application/json"],"Vary":["Origin"]},"Body":"{\"message\":\"Not Found\"}\n"} {"Method":"OPTIONS","Path":"/assets/missing.js","RequestHeader":null,"Status":204,"Header":{"Vary":["Origin"]},"Body":""} {"Method":"GET","Path":"/index.html","RequestHeader":null,"Status":200,"Header":{"Accept-Ranges":["bytes"],"Cache-Control":["no-cache, no-store, must-revalidate"],"Content-Type":["text/html; charset=utf-8"],"Expires":["0"],"Pragma":["no-cache"],"Vary":["Origin"]},"Body":"\u003cfrontend HTML\u003e"} {"Method":"HEAD","Path":"/index.html","RequestHeader":null,"Status":200,"Header":{"Accept-Ranges":["bytes"],"Cache-Control":["no-cache, no-store, must-revalidate"],"Content-Type":["text/html; charset=utf-8"],"Expires":["0"],"Pragma":["no-cache"],"Vary":["Origin"]},"Body":""}