Try using shorter cache times in skottie and jsfiddle

This will hopefully reduce the chance for the JS and WASM to skew.
It may allow us to put skottie.skia.org back on rolling
with ToT

Bug: skia:
Change-Id: I476def08b30d50dff3d656adfaa163dd00d284e1
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/199484
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
diff --git a/jsfiddle/go/jsfiddle/main.go b/jsfiddle/go/jsfiddle/main.go
index e7bc686..64d13c0 100644
--- a/jsfiddle/go/jsfiddle/main.go
+++ b/jsfiddle/go/jsfiddle/main.go
@@ -7,6 +7,7 @@
 	"flag"
 	"fmt"
 	"io/ioutil"
+	"mime"
 	"net/http"
 	"path/filepath"
 	"strings"
@@ -42,6 +43,10 @@
 			loadPages()
 		}
 		w.Header().Set("Content-Type", "text/html")
+		// Set the HTML to expire at the same time as the JS and WASM, otherwise the HTML
+		// (and by extension, the JS with its cachbuster hash) might outlive the WASM
+		// and then the two will skew
+		w.Header().Set("Cache-Control", "max-age=60")
 		w.WriteHeader(http.StatusOK)
 		if _, err := w.Write(page); err != nil {
 			httputils.ReportError(w, r, err, "Server could not load page")
@@ -49,6 +54,21 @@
 	}
 }
 
+func makeResourceHandler() func(http.ResponseWriter, *http.Request) {
+	fileServer := http.FileServer(http.Dir(*resourcesDir))
+	return func(w http.ResponseWriter, r *http.Request) {
+		// Use a shorter cache live to limit the risk of canvaskit.js (in indexbundle.js)
+		// from drifting away from the version of canvaskit.wasm. Ideally, canvaskit
+		// will roll at ToT (~35 commits per day), so living for a minute should
+		// reduce the risk of JS/WASM being out of sync.
+		w.Header().Add("Cache-Control", "max-age=60")
+		w.Header().Add("Access-Control-Allow-Origin", "*")
+		p := r.URL.Path
+		r.URL.Path = strings.TrimPrefix(p, "/res")
+		fileServer.ServeHTTP(w, r)
+	}
+}
+
 type fiddleContext struct {
 	Code string `json:"code"`
 	Type string `json:"type,omitempty"`
@@ -94,21 +114,6 @@
 	}
 }
 
-func makeResourceHandler() func(http.ResponseWriter, *http.Request) {
-	fileServer := http.FileServer(http.Dir(*resourcesDir))
-	return func(w http.ResponseWriter, r *http.Request) {
-		w.Header().Add("Cache-Control", "max-age=300")
-		w.Header().Add("Access-Control-Allow-Origin", "*")
-		p := r.URL.Path
-		r.URL.Path = strings.TrimPrefix(p, "/res")
-		if strings.HasSuffix(p, "wasm") {
-			// WASM won't do a streaming-compile if the mime-type isn't set
-			w.Header().Set("Content-Type", "application/wasm")
-		}
-		fileServer.ServeHTTP(w, r)
-	}
-}
-
 func loadPages() {
 	if p, err := ioutil.ReadFile(filepath.Join(*resourcesDir, "pathkit-index.html")); err != nil {
 		sklog.Fatalf("Could not find pathkit html: %s", err)
@@ -182,6 +187,11 @@
 		sklog.Fatalf("Failed to connect to store: %s", err)
 	}
 
+	// Need to set the mime-type for wasm files so streaming compile works.
+	if err := mime.AddExtensionType(".wasm", "application/wasm"); err != nil {
+		sklog.Fatal(err)
+	}
+
 	r := mux.NewRouter()
 	r.PathPrefix("/res/").HandlerFunc(makeResourceHandler()).Methods("GET")
 	r.HandleFunc("/canvaskit", cspHandler(htmlHandler(canvaskitPage))).Methods("GET")
diff --git a/particles/go/particles/main.go b/particles/go/particles/main.go
index c3455b8..ee319a0 100644
--- a/particles/go/particles/main.go
+++ b/particles/go/particles/main.go
@@ -64,7 +64,7 @@
 
 	// Need to set the mime-type for wasm files so streaming compile works.
 	if err := mime.AddExtensionType(".wasm", "application/wasm"); err != nil {
-		sklog.Fatal(err)
+		return nil, err
 	}
 
 	ts, err := auth.NewDefaultTokenSource(*local, storage.ScopeFullControl)
@@ -99,9 +99,14 @@
 		filepath.Join(*resourcesDir, "index.html"),
 	))
 }
+
 func (srv *Server) templateHandler(filename string) func(http.ResponseWriter, *http.Request) {
 	return func(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Content-Type", "text/html")
+		// Set the HTML to expire at the same time as the JS and WASM, otherwise the HTML
+		// (and by extension, the JS with its cachbuster hash) might outlive the WASM
+		// and then the two will skew
+		w.Header().Set("Cache-Control", "max-age=60")
 		if *local {
 			srv.loadTemplates()
 		}
@@ -110,6 +115,19 @@
 		}
 	}
 }
+
+func resourceHandler(resourcesDir string) func(http.ResponseWriter, *http.Request) {
+	fileServer := http.FileServer(http.Dir(resourcesDir))
+	return func(w http.ResponseWriter, r *http.Request) {
+		// Use a shorter cache live to limit the risk of canvaskit.js (in indexbundle.js)
+		// from drifting away from the version of canvaskit.wasm. Ideally, the skottie
+		// will roll at ToT (~35 commits per day), so living for a minute should
+		// reduce the risk of JS/WASM being out of sync.
+		w.Header().Add("Cache-Control", "max-age=60")
+		fileServer.ServeHTTP(w, r)
+	}
+}
+
 func (srv *Server) jsonHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Access-Control-Allow-Origin", "*")
@@ -240,7 +258,7 @@
 	r.HandleFunc("/_/j/{hash:[0-9A-Za-z]+}", srv.jsonHandler).Methods("GET")
 	r.HandleFunc("/_/upload", srv.uploadHandler).Methods("POST")
 
-	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.HandlerFunc(httputils.CorsHandler(httputils.MakeResourceHandler(*resourcesDir))))).Methods("GET")
+	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.HandlerFunc(httputils.CorsHandler(resourceHandler(*resourcesDir))))).Methods("GET")
 
 	// TODO(jcgregorio) Implement CSRF.
 	h := httputils.LoggingGzipRequestResponse(r)
diff --git a/skottie/go/skottie/main.go b/skottie/go/skottie/main.go
index cef5b0b..6e58ff0 100644
--- a/skottie/go/skottie/main.go
+++ b/skottie/go/skottie/main.go
@@ -71,7 +71,7 @@
 
 	// Need to set the mime-type for wasm files so streaming compile works.
 	if err := mime.AddExtensionType(".wasm", "application/wasm"); err != nil {
-		sklog.Fatal(err)
+		return nil, err
 	}
 
 	ts, err := auth.NewDefaultTokenSource(*local, storage.ScopeFullControl)
@@ -111,6 +111,10 @@
 func (srv *Server) templateHandler(filename string) func(http.ResponseWriter, *http.Request) {
 	return func(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Content-Type", "text/html")
+		// Set the HTML to expire at the same time as the JS and WASM, otherwise the HTML
+		// (and by extension, the JS with its cachbuster hash) might outlive the WASM
+		// and then the two will skew
+		w.Header().Set("Cache-Control", "max-age=60")
 		if *local {
 			srv.loadTemplates()
 		}
@@ -120,6 +124,18 @@
 	}
 }
 
+func resourceHandler(resourcesDir string) func(http.ResponseWriter, *http.Request) {
+	fileServer := http.FileServer(http.Dir(resourcesDir))
+	return func(w http.ResponseWriter, r *http.Request) {
+		// Use a shorter cache live to limit the risk of canvaskit.js (in indexbundle.js)
+		// from drifting away from the version of canvaskit.wasm. Ideally, the skottie
+		// will roll at ToT (~35 commits per day), so living for a minute should
+		// reduce the risk of JS/WASM being out of sync.
+		w.Header().Add("Cache-Control", "max-age=60")
+		fileServer.ServeHTTP(w, r)
+	}
+}
+
 func (srv *Server) verificationHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "text/html")
 	_, err := w.Write([]byte("google-site-verification: google99d1f93c6755806b.html"))
@@ -487,7 +503,7 @@
 	r.HandleFunc(`/_/a/{hash:[0-9A-Za-z]+}/{name:[A-Za-z0-9\._\-]+}`, srv.assetsHandler).Methods("GET")
 	r.HandleFunc("/_/upload", srv.uploadHandler).Methods("POST")
 
-	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.HandlerFunc(httputils.CorsHandler(httputils.MakeResourceHandler(*resourcesDir))))).Methods("GET")
+	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.HandlerFunc(httputils.CorsHandler(resourceHandler(*resourcesDir))))).Methods("GET")
 
 	// TODO(jcgregorio) Implement CSRF.
 	h := httputils.LoggingGzipRequestResponse(r)