mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
fix(frontend): prevent caching missing static assets
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Vendored
+3
-3
@@ -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":""}
|
||||
|
||||
Reference in New Issue
Block a user