disable cache for certain resources and respect try_files
This commit is contained in:
@@ -22,18 +22,24 @@ func (f *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
upath := path.Clean(path.Join("/", r.URL.Path))
|
upath := path.Clean(path.Join("/", r.URL.Path))
|
||||||
r.URL.Path = upath
|
r.URL.Path = upath
|
||||||
|
|
||||||
asset, err := f.root.Open(upath)
|
disableCache := func() {
|
||||||
if err != nil {
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
}
|
||||||
|
handleIndex := func() {
|
||||||
|
disableCache()
|
||||||
r.URL.Path = "/index.html"
|
r.URL.Path = "/index.html"
|
||||||
f.ServeHTTP(w, r)
|
f.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
asset, err := f.root.Open(upath)
|
||||||
|
if err != nil {
|
||||||
|
handleIndex()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer asset.Close()
|
defer asset.Close()
|
||||||
|
|
||||||
s, _ := asset.Stat()
|
s, _ := asset.Stat()
|
||||||
if s.IsDir() {
|
if s.IsDir() {
|
||||||
r.URL.Path = "/index.html"
|
handleIndex()
|
||||||
f.ServeHTTP(w, r)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +49,7 @@ func (f *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
switch s.Name() {
|
switch s.Name() {
|
||||||
case "index.html", "oidc-callback.html", "oidc-silent-redirect.html":
|
case "index.html", "oidc-callback.html", "oidc-silent-redirect.html":
|
||||||
|
disableCache()
|
||||||
_ = withBase(buf, asset, "/")
|
_ = withBase(buf, asset, "/")
|
||||||
default:
|
default:
|
||||||
_, _ = buf.ReadFrom(asset)
|
_, _ = buf.ReadFrom(asset)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package svc
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -90,7 +89,7 @@ func (p Web) getPayload() (payload []byte, err error) {
|
|||||||
Msg("web config doesn't exist")
|
Msg("web config doesn't exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err = ioutil.ReadFile(p.config.Web.Path)
|
payload, err = os.ReadFile(p.config.Web.Path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Fatal().
|
p.logger.Fatal().
|
||||||
@@ -102,7 +101,7 @@ func (p Web) getPayload() (payload []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Config implements the Service interface.
|
// Config implements the Service interface.
|
||||||
func (p Web) Config(w http.ResponseWriter, r *http.Request) {
|
func (p Web) Config(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
|
||||||
payload, err := p.getPayload()
|
payload, err := p.getPayload()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -152,13 +151,6 @@ func (p Web) Static(ttl int) http.HandlerFunc {
|
|||||||
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s, must-revalidate", strconv.Itoa(ttl)))
|
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s, must-revalidate", strconv.Itoa(ttl)))
|
||||||
w.Header().Set("Expires", expires)
|
w.Header().Set("Expires", expires)
|
||||||
w.Header().Set("Last-Modified", lastModified)
|
w.Header().Set("Last-Modified", lastModified)
|
||||||
|
|
||||||
if r.URL.Path == rootWithSlash || r.URL.Path == rootWithSlash+"index.html" {
|
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
|
||||||
|
|
||||||
} else {
|
|
||||||
w.Header().Set("Cache-Control", "must-revalidate")
|
|
||||||
}
|
|
||||||
w.Header().Set("SameSite", "Strict")
|
w.Header().Set("SameSite", "Strict")
|
||||||
|
|
||||||
static.ServeHTTP(w, r)
|
static.ServeHTTP(w, r)
|
||||||
|
|||||||
Reference in New Issue
Block a user