repos
/ orchard main

orchard

mirror

Every site I host, in one repo, along with the Cloudflare Tunnel and Caddy that front them. It's all Go, Vite, and SQLite, and it runs on a desktop at home with nothing listening on an inbound port.

blogbuncaddycloudflare-tunneldockergogolanghomelabhtml-templatemonorepoself-hostedseosqlitestatic-sitetypstuptime-monitoringviteweb-analytics

933 B · 34 lines · Go Raw History
 1package web
 2
 3import (
 4	"io/fs"
 5	"net/http"
 6	"strings"
 7)
 8
 9// Static serves the Vite build output at /static/. Content-hashed files get a
10// year and are never revalidated, since the name changes when the bytes do.
11// Everything else gets an hour, which covers files copied through from
12// publicDir under their original names.
13func Static(dist fs.FS, assets *Assets) http.Handler {
14	server := http.FileServer(http.FS(dist))
15	hashed := assets.Hashed()
16
17	return http.StripPrefix("/static/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18		clean := strings.TrimPrefix(r.URL.Path, "/")
19
20		// Server-side only; a browser has no use for it.
21		if strings.HasPrefix(clean, ".vite/") {
22			http.NotFound(w, r)
23			return
24		}
25
26		if hashed[clean] {
27			w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
28		} else {
29			w.Header().Set("Cache-Control", "public, max-age=3600")
30		}
31		server.ServeHTTP(w, r)
32	}))
33}