mirror of
https://github.com/octoleo/syncthing.git
synced 2025-04-02 07:41:51 +00:00
gui: Enable proper asset caching (#4931)
This commit is contained in:
parent
fb198a0645
commit
e125f8b05b
@ -251,6 +251,8 @@ func handleMetrics(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleAssets(w http.ResponseWriter, r *http.Request) {
|
func handleAssets(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Cache-Control", "no-cache, must-revalidate")
|
||||||
|
|
||||||
assets := auto.Assets()
|
assets := auto.Assets()
|
||||||
path := r.URL.Path[1:]
|
path := r.URL.Path[1:]
|
||||||
if path == "" {
|
if path == "" {
|
||||||
@ -263,11 +265,28 @@ func handleAssets(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
etag := fmt.Sprintf("%d", auto.Generated)
|
||||||
|
modified := time.Unix(auto.Generated, 0).UTC()
|
||||||
|
|
||||||
|
w.Header().Set("Last-Modified", modified.Format(http.TimeFormat))
|
||||||
|
w.Header().Set("Etag", etag)
|
||||||
|
|
||||||
mtype := mimeTypeForFile(path)
|
mtype := mimeTypeForFile(path)
|
||||||
if len(mtype) != 0 {
|
if len(mtype) != 0 {
|
||||||
w.Header().Set("Content-Type", mtype)
|
w.Header().Set("Content-Type", mtype)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modified.Add(time.Second).After(t) {
|
||||||
|
w.WriteHeader(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if match := r.Header.Get("If-None-Match"); match != "" {
|
||||||
|
if strings.Contains(match, etag) {
|
||||||
|
w.WriteHeader(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
w.Header().Set("Content-Encoding", "gzip")
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/syncthing/syncthing/lib/auto"
|
"github.com/syncthing/syncthing/lib/auto"
|
||||||
"github.com/syncthing/syncthing/lib/config"
|
"github.com/syncthing/syncthing/lib/config"
|
||||||
@ -71,6 +72,8 @@ func (s *staticsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
|
func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Cache-Control", "no-cache, must-revalidate")
|
||||||
|
|
||||||
file := r.URL.Path
|
file := r.URL.Path
|
||||||
|
|
||||||
if file[0] == '/' {
|
if file[0] == '/' {
|
||||||
@ -122,6 +125,24 @@ func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
etag := fmt.Sprintf("%d", auto.Generated)
|
||||||
|
modified := time.Unix(auto.Generated, 0).UTC()
|
||||||
|
|
||||||
|
w.Header().Set("Last-Modified", modified.Format(http.TimeFormat))
|
||||||
|
w.Header().Set("Etag", etag)
|
||||||
|
|
||||||
|
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modified.Add(time.Second).After(t) {
|
||||||
|
w.WriteHeader(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if match := r.Header.Get("If-None-Match"); match != "" {
|
||||||
|
if strings.Contains(match, etag) {
|
||||||
|
w.WriteHeader(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mtype := s.mimeTypeForFile(file)
|
mtype := s.mimeTypeForFile(file)
|
||||||
if len(mtype) != 0 {
|
if len(mtype) != 0 {
|
||||||
w.Header().Set("Content-Type", mtype)
|
w.Header().Set("Content-Type", mtype)
|
||||||
|
@ -19,10 +19,13 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var tpl = template.Must(template.New("assets").Parse(`package auto
|
var tpl = template.Must(template.New("assets").Parse(`package auto
|
||||||
|
|
||||||
|
const Generated int64 = {{.Generated}}
|
||||||
|
|
||||||
func Assets() map[string][]byte {
|
func Assets() map[string][]byte {
|
||||||
var assets = make(map[string][]byte, {{.Assets | len}})
|
var assets = make(map[string][]byte, {{.Assets | len}})
|
||||||
{{range $asset := .Assets}}
|
{{range $asset := .Assets}}
|
||||||
@ -76,6 +79,7 @@ func walkerFor(basePath string) filepath.WalkFunc {
|
|||||||
|
|
||||||
type templateVars struct {
|
type templateVars struct {
|
||||||
Assets []asset
|
Assets []asset
|
||||||
|
Generated int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -85,6 +89,7 @@ func main() {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
tpl.Execute(&buf, templateVars{
|
tpl.Execute(&buf, templateVars{
|
||||||
Assets: assets,
|
Assets: assets,
|
||||||
|
Generated: time.Now().Unix(),
|
||||||
})
|
})
|
||||||
bs, err := format.Source(buf.Bytes())
|
bs, err := format.Source(buf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user