Static asset caching
This commit is contained in:
parent
600f105b00
commit
921bf66d20
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"shelves/backend/httpx"
|
"shelves/backend/httpx"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"shelves"
|
"shelves"
|
||||||
)
|
)
|
||||||
|
|
@ -28,9 +29,28 @@ func redirectSetup(next http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ServeStatic() http.Handler {
|
||||||
|
inner := http.StripPrefix("/static/", http.FileServerFS(shelves.Frontend))
|
||||||
|
startup := time.Now().UTC()
|
||||||
|
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ims := r.Header.Get("If-Modified-Since")
|
||||||
|
imsTime, _ := time.Parse(http.TimeFormat, ims)
|
||||||
|
imsTime = imsTime.Add(time.Second * 10)
|
||||||
|
|
||||||
|
if imsTime.Before(startup) {
|
||||||
|
w.Header().Add("Last-Modified", startup.Format(http.TimeFormat))
|
||||||
|
w.Header().Add("Cache-Control", "public, max-age=0, stale-while-revalidate=9999999")
|
||||||
|
inner.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusNotModified)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func Routes() http.Handler {
|
func Routes() http.Handler {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(shelves.Frontend)))
|
mux.Handle("GET /static/", ServeStatic())
|
||||||
mux.HandleFunc("GET /{$}", HomeGet)
|
mux.HandleFunc("GET /{$}", HomeGet)
|
||||||
mux.HandleFunc("GET /setup", SetupGet)
|
mux.HandleFunc("GET /setup", SetupGet)
|
||||||
mux.HandleFunc("POST /setup", SetupPost)
|
mux.HandleFunc("POST /setup", SetupPost)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue