reply with 304 if there's no new content to save some traffic

This commit is contained in:
Ted Unangst 2019-04-10 19:25:32 -04:00
parent 645e6a372a
commit d3b4e07565
1 changed files with 16 additions and 0 deletions

16
honk.go
View File

@ -172,11 +172,27 @@ func homepage(w http.ResponseWriter, r *http.Request) {
return honks[i].Date.After(honks[j].Date) return honks[i].Date.After(honks[j].Date)
}) })
reverbolate(honks) reverbolate(honks)
var modtime time.Time
if len(honks) > 0 {
modtime = honks[0].Date
}
imh := r.Header.Get("If-Modified-Since")
if imh != "" && !modtime.IsZero() {
ifmod, err := time.Parse(http.TimeFormat, imh)
if err == nil && !modtime.After(ifmod) {
w.WriteHeader(http.StatusNotModified)
return
}
}
msg := "Things happen." msg := "Things happen."
getconfig("servermsg", &msg) getconfig("servermsg", &msg)
templinfo["Honks"] = honks templinfo["Honks"] = honks
templinfo["ShowRSS"] = true templinfo["ShowRSS"] = true
templinfo["ServerMessage"] = msg templinfo["ServerMessage"] = msg
w.Header().Set("Cache-Control", "max-age=0")
w.Header().Set("Last-Modified", modtime.Format(http.TimeFormat))
err := readviews.ExecuteTemplate(w, "homepage.html", templinfo) err := readviews.ExecuteTemplate(w, "homepage.html", templinfo)
if err != nil { if err != nil {
log.Print(err) log.Print(err)