Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cache-Control header to static resources to help caching #901

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http/httputil"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -59,6 +60,8 @@ const ExtensionDisplayNameKey = "tekton-dashboard-display-name"
const ExtensionRoot = "/v1/extensions"

var webResourcesDir = os.Getenv("WEB_RESOURCES_DIR")
var webResourcesStaticPattern = regexp.MustCompile("^/([[:alnum:]]+\\.)?[[:alnum:]]+\\.(js)|(css)|(png)$")
var webResourcesStaticExcludePattern = regexp.MustCompile("^/favicon.png$")

// Register returns an HTTP handler that has the Dashboard REST API registered
func Register(resource endpoints.Resource) *Handler {
Expand Down Expand Up @@ -192,7 +195,14 @@ func registerKubeAPIProxy(r endpoints.Resource, container *restful.Container) {
func registerWeb(container *restful.Container) {
logging.Log.Info("Adding Web API")

container.Handle("/", http.FileServer(http.Dir(webResourcesDir)))
fs := http.FileServer(http.Dir(webResourcesDir))
container.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if webResourcesStaticPattern.Match([]byte(r.URL.Path)) && !webResourcesStaticExcludePattern.Match([]byte(r.URL.Path)) {
// Static resources are immutable and have a content hash in their URL
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
}
fs.ServeHTTP(w, r)
}))
}

// registerEndpoints registers the APIs to interface with core Tekton/K8s pieces
Expand Down