Update caching logic/headers (#184)

* Improve caching by adding etags and reducing the max-age

* Move caching into middleware. Set cache headers on segments
This commit is contained in:
Gabe Kangas
2020-09-30 14:14:39 -07:00
committed by GitHub
parent 6a0e8deae3
commit 8aa5c33999
5 changed files with 78 additions and 22 deletions

View File

@ -37,21 +37,29 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
return
}
// For search engine bots and social scrapers return a special
// server-rendered page.
if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
handleScraperMetadataPage(w, r)
return
}
// If the ETags match then return a StatusNotModified
if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
w.WriteHeader(responseCode)
return
}
if path.Ext(r.URL.Path) == ".m3u8" {
middleware.DisableCache(w)
clientID := utils.GenerateClientIDFromRequest(r)
core.SetClientActive(clientID)
} else {
// Set a cache control header of one day
middleware.SetCache(1, w)
}
// Set a cache control max-age header
middleware.SetCachingHeaders(w, r)
http.ServeFile(w, r, path.Join("webroot", r.URL.Path))
}