@@ -3,11 +3,13 @@ package proxy
3
3
import (
4
4
"crypto/tls"
5
5
"errors"
6
+ "fmt"
6
7
"io/ioutil"
7
8
"net"
8
9
"net/http"
9
10
"net/url"
10
11
"os"
12
+ "strings"
11
13
"sync"
12
14
"time"
13
15
@@ -283,10 +285,37 @@ type staticFileHandler struct {
283
285
284
286
func (sfh * staticFileHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
285
287
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
+ }
286
311
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
+ }
290
319
291
320
sfh .fileServer .ServeHTTP (w , r )
292
321
}
0 commit comments