From ee7dc1dd082d8ce868d009cbb57dc462a5fe5784 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 17 Feb 2026 22:27:51 +0000 Subject: [PATCH] Add caching headers for production, fix css for RMO --- html/fileserver.go | 15 +++++++++++++++ html/html.go | 7 ------- html/static/js/address-or-report-suggestion.js | 2 +- html/template/rmo/layout/base.html | 2 +- main.go | 1 - 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/html/fileserver.go b/html/fileserver.go index bd6d86bb..a7ee06ab 100644 --- a/html/fileserver.go +++ b/html/fileserver.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/Gleipnir-Technology/nidus-sync/config" "github.com/go-chi/chi/v5" @@ -16,6 +17,8 @@ import ( // fileServer conveniently sets up a http.FileServer handler to serve // static files from a http.FileSystem. +var startedTime time.Time = time.Now() + func fileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embed.FS, embeddedPath string) { if strings.ContainsAny(path, "{}*") { panic("FileServer does not permit any URL parameters.") @@ -66,6 +69,18 @@ func fileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embe // Create a custom ResponseWriter that allows us to modify headers crw := &customResponseWriter{ResponseWriter: w} + // Add caching headers + if config.IsProductionEnvironment() { + ext := filepath.Ext(requestedPath) + switch ext { + case ".css", ".js", ".jpg", ".jpeg", ".png", ".gif", ".svg", ".woff", ".woff2", ".ttf": + // Cache for 1 week (604800 seconds) + crw.Header().Set("Cache-Control", "public, max-age=604800, stale-while-revalidate=86400") + default: + // Other files, 1 hour + crw.Header().Set("Cache-Control", "public, max-age=3600") + } + } // Serve the file http.ServeContent(crw, r, requestedPath, startedTime, fileToServe) diff --git a/html/html.go b/html/html.go index 0ff59d64..209fc7ae 100644 --- a/html/html.go +++ b/html/html.go @@ -2,15 +2,8 @@ package html import ( "net/http" - "time" ) func RenderOrError(w http.ResponseWriter, template_name string, content interface{}) { templates.renderOrError(w, template_name, content) } - -var startedTime time.Time - -func SetStartedTime() { - startedTime = time.Now() -} diff --git a/html/static/js/address-or-report-suggestion.js b/html/static/js/address-or-report-suggestion.js index 85a8b956..ba716a29 100644 --- a/html/static/js/address-or-report-suggestion.js +++ b/html/static/js/address-or-report-suggestion.js @@ -199,7 +199,7 @@ class AddressOrReportInput extends HTMLElement { this.shadowRoot.innerHTML = `