@@ -3,11 +3,13 @@ package proxy
33import (
44 "crypto/tls"
55 "errors"
6+ "fmt"
67 "io/ioutil"
78 "net"
89 "net/http"
910 "net/url"
1011 "os"
12+ "strings"
1113 "sync"
1214 "time"
1315
@@ -283,10 +285,37 @@ type staticFileHandler struct {
283285
284286func (sfh * staticFileHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
285287 common .EnableHSTS (w )
288+ var (
289+ serveCompressed = false
290+ currentURL = ""
291+ err error
292+ )
293+ if ! strings .Contains (r .Header .Get ("Accept-Encoding" ), "gzip" ) {
294+ sfh .fileServer .ServeHTTP (w , r )
295+ }
296+ isCSSFile := strings .HasSuffix (currentURL , ".css" )
297+ isJSFile := strings .HasSuffix (currentURL , ".js" )
298+ if isCSSFile || isJSFile {
299+ currentURL = r .URL .String ()
300+ newURL := fmt .Sprintf ("%v.gz" , currentURL )
301+ r .URL , err = url .Parse (newURL )
302+ if err != nil {
303+ err = fmt .Errorf ("failed to parse URL: %v" , err )
304+ serverError (w , err )
305+ }
306+ serveCompressed = true
307+ }
308+ if isCSSFile {
309+ w .Header ().Set ("Content-Type" , "text/css" )
310+ }
286311
287- // TODO: here is a good spot to look for asset requests (.js, .css, etc.)
288- // and append .gz and serve a gzipped version instead by rewriting
289- // the requested path.
312+ if isJSFile {
313+ w .Header ().Set ("Content-Type" , "application/x-javascript" )
314+ }
315+
316+ if serveCompressed {
317+ w .Header ().Set ("Content-Encoding" , "gzip" )
318+ }
290319
291320 sfh .fileServer .ServeHTTP (w , r )
292321}
0 commit comments