Skip to content

Commit ccf3a4e

Browse files
committed
feat: only enable browser cache for some content types
1 parent f8565b3 commit ccf3a4e

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

handler/main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ func NewRouter() http.Handler {
3838
}
3939
r.Use(wrapMiddleware, middleware.Recoverer, trafficMiddleware)
4040

41-
noCacheRouter := r.Methods(http.MethodGet).Subrouter()
42-
noCacheRouter.Use(middleware.NoCache)
43-
noCacheRouter.PathPrefix("/:/").Handler(plexClient)
44-
noCacheRouter.PathPrefix("/library/parts/").Handler(plexClient)
45-
noCacheRouter.PathPrefix("/video/:/transcode/").Handler(plexClient)
46-
4741
staticRouter := r.Methods(http.MethodGet).Subrouter()
4842
staticRouter.Use(staticMiddleware)
4943
staticRouter.Path("/library/media/{key}/chapterImages/{id}").Handler(plexClient)

handler/utils.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"context"
5+
"mime"
56
"net/http"
67
"net/url"
78
"runtime/debug"
@@ -18,22 +19,18 @@ func wrapResponseWriter(w http.ResponseWriter, protoMajor int) middleware.WrapRe
1819
}
1920

2021
func modifyResponse(resp *http.Response) error {
21-
contentType := resp.Header.Get(headerContentType)
22-
if contentType == "" {
23-
return nil
22+
var mediaType string
23+
if contentType := resp.Header.Get(headerContentType); contentType != "" {
24+
mediaType, _, _ = mime.ParseMediaType(contentType)
2425
}
25-
pieces := strings.Split(contentType, "/")
26-
if len(pieces) == 0 {
27-
return nil
28-
}
29-
switch pieces[0] {
30-
case "audio", "video":
31-
resp.Header.Set(headerCacheControl, "no-cache")
32-
resp.Header.Set(headerVary, "*")
33-
case "image":
26+
switch {
27+
case mediaType == "text/css",
28+
mediaType == "text/javascript",
29+
strings.HasPrefix(mediaType, "image/"),
30+
strings.HasPrefix(mediaType, "font/"):
3431
resp.Header.Set(headerCacheControl, "public, max-age=86400, s-maxage=259200")
3532
default:
36-
resp.Header.Set(headerCacheControl, "no-cache")
33+
resp.Header.Set(headerCacheControl, "no-cache, no-store, no-transform, must-revalidate, private, max-age=0, s-maxage=0")
3734
}
3835
return nil
3936
}

0 commit comments

Comments
 (0)