fix(frontend): prevent caching missing static assets

This commit is contained in:
johnnyjoygh
2026-11-14 23:34:31 +08:00
parent 4a0c5cfaf5
commit 7fefb833a9
3 changed files with 21 additions and 8 deletions
+1
View File
@@ -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"
+17 -5
View File
@@ -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) {
+3 -3
View File
@@ -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":""}