44 "bytes"
55 "compress/gzip"
66 _ "embed"
7+ "encoding/base64"
78 "encoding/json"
89 "fmt"
910 "io"
@@ -226,6 +227,24 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
226227
227228 return func (ctx * App , res http.ResponseWriter , req * http.Request ) {
228229 head := res .Header ()
230+ sign := signature ()
231+ base := WithBase ("/" )
232+ clear := req .Header .Get ("Cache-Control" ) == "no-cache"
233+ templateData := map [string ]any {
234+ "base" : base ,
235+ "version" : BUILD_REF ,
236+ "license" : LICENSE ,
237+ "preload" : preload (),
238+ "clear" : clear ,
239+ "hash" : sign ,
240+ "favicon" : favicon (),
241+ }
242+ calculatedEtag := QuickHash (base + BUILD_REF + LICENSE + fmt .Sprintf ("%t" , clear )+ sign , 10 )
243+ head .Set ("ETag" , calculatedEtag )
244+ if etag := req .Header .Get ("If-None-Match" ); etag == calculatedEtag {
245+ res .WriteHeader (http .StatusNotModified )
246+ return
247+ }
229248 var out io.Writer = res
230249 if strings .Contains (req .Header .Get ("Accept-Encoding" ), "gzip" ) {
231250 head .Set ("Content-Encoding" , "gzip" )
@@ -234,14 +253,7 @@ func ServeIndex(indexPath string) func(*App, http.ResponseWriter, *http.Request)
234253 out = gz
235254 }
236255 head .Set ("Content-Type" , "text/html" )
237- tmpl .Execute (out , map [string ]any {
238- "base" : WithBase ("/" ),
239- "version" : BUILD_REF ,
240- "license" : LICENSE ,
241- "preload" : preload (),
242- "clear" : req .Header .Get ("Cache-Control" ) == "no-cache" ,
243- "hash" : signature (),
244- })
256+ tmpl .Execute (out , templateData )
245257 }
246258}
247259
@@ -496,3 +508,15 @@ func signature() string {
496508 }
497509 return strings .ToLower (QuickHash (text , 3 ))
498510}
511+
512+ func favicon () string {
513+ file , err := WWWPublic .Open ("/assets/logo/favicon.svg" )
514+ if err != nil {
515+ return "favicon.ico"
516+ }
517+ f , err := io .ReadAll (file )
518+ if err != nil {
519+ return "favicon.ico"
520+ }
521+ return "data:image/svg+xml;base64," + base64 .StdEncoding .EncodeToString (f )
522+ }
0 commit comments