From 6f508ed194ba38fab450026bcb54552a3865fe75 Mon Sep 17 00:00:00 2001 From: Thomas Nilles Date: Tue, 13 Jan 2026 00:14:28 -0500 Subject: [PATCH] Serve static files at /static and set JS MIME type Add strings import for .js detection. Register an additional FileServer at "/static/" to expose static files under that prefix. In the FileServer handler, set Content-Type to "application/javascript" for .js requests. --- cmd/app/main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index 48264b7..bada9c3 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "strings" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" @@ -17,9 +18,10 @@ func main() { // Register the hello world handler at the root path - // Serve static HTML files from the "static" directory - // (e.g., /static/index.html will be accessible at /static/index.html) + // Serve static HTML files from the "static" directory at the root FileServer(r, "/", http.Dir("./static")) + // Also serve the same files under the "/static" URL prefix + FileServer(r, "/static/", http.Dir("./static")) // Start the HTTP server http.ListenAndServe(":8080", r) @@ -36,6 +38,10 @@ func FileServer(r chi.Router, publicPath string, root http.FileSystem) { } fs := http.StripPrefix(publicPath, http.FileServer(root)) r.Get(publicPath+"*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Set correct MIME type for JavaScript files + if strings.HasSuffix(r.URL.Path, ".js") { + w.Header().Set("Content-Type", "application/javascript") + } fs.ServeHTTP(w, r) })) }