From e6cc650af0bc7b5cedd57a0770a7b9d049b64d95 Mon Sep 17 00:00:00 2001 From: Asher Date: Wed, 28 Aug 2024 09:53:51 -0800 Subject: [PATCH 1/2] fix: make non-existent asset paths return a 404 Before, if a file was not found we would serve the app. --- site/site.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/site/site.go b/site/site.go index b168910ce343c..595838ee6726a 100644 --- a/site/site.go +++ b/site/site.go @@ -205,6 +205,18 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { case reqFile == "bin" || strings.HasPrefix(reqFile, "bin/"): h.handler.ServeHTTP(rw, r) return + // If requesting assets, serve straight up with caching. + case reqFile == "assets" || strings.HasPrefix(reqFile, "assets/"): + // It could make sense to cache 404s, but the problem is that during an + // upgrade a load balance may route partially to the old server, and that + // would make new asset paths get cached as 404s and not load even once the + // new server was in place. To combat that, only cache if we have the file. + if h.exists(reqFile) && ShouldCacheFile(reqFile) { + rw.Header().Add("Cache-Control", "public, max-age=31536000, immutable") + } + // If the asset does not exist, this will return a 404. + h.handler.ServeHTTP(rw, r) + return // If the original file path exists we serve it. case h.exists(reqFile): if ShouldCacheFile(reqFile) { From 8c02c2825654502da5303ed370c0ddca2e7d27d7 Mon Sep 17 00:00:00 2001 From: Asher Date: Wed, 28 Aug 2024 10:18:42 -0800 Subject: [PATCH 2/2] fix: typo --- site/site.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/site.go b/site/site.go index 595838ee6726a..1924a17fe1a10 100644 --- a/site/site.go +++ b/site/site.go @@ -208,7 +208,7 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { // If requesting assets, serve straight up with caching. case reqFile == "assets" || strings.HasPrefix(reqFile, "assets/"): // It could make sense to cache 404s, but the problem is that during an - // upgrade a load balance may route partially to the old server, and that + // upgrade a load balancer may route partially to the old server, and that // would make new asset paths get cached as 404s and not load even once the // new server was in place. To combat that, only cache if we have the file. if h.exists(reqFile) && ShouldCacheFile(reqFile) {