@@ -133,6 +133,7 @@ var (
133
133
GenerateIndexPages : true ,
134
134
Compress : true ,
135
135
CompressBrotli : true ,
136
+ CompressZstd : true ,
136
137
AcceptByteRange : true ,
137
138
}
138
139
rootFSHandler RequestHandler
@@ -156,6 +157,7 @@ func ServeFS(ctx *RequestCtx, filesystem fs.FS, path string) {
156
157
GenerateIndexPages : true ,
157
158
Compress : true ,
158
159
CompressBrotli : true ,
160
+ CompressZstd : true ,
159
161
AcceptByteRange : true ,
160
162
}
161
163
handler := f .NewRequestHandler ()
@@ -321,13 +323,20 @@ type FS struct {
321
323
// absolute paths on any filesystem.
322
324
AllowEmptyRoot bool
323
325
324
- // Uses brotli encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
326
+ // Uses brotli encoding and fallbacks to zstd or gzip in responses if set to true, uses zstd or gzip if set to false.
325
327
//
326
328
// This value has sense only if Compress is set.
327
329
//
328
330
// Brotli encoding is disabled by default.
329
331
CompressBrotli bool
330
332
333
+ // Uses zstd encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
334
+ //
335
+ // This value has sense only if Compress is set.
336
+ //
337
+ // zstd encoding is disabled by default.
338
+ CompressZstd bool
339
+
331
340
// Index pages for directories without files matching IndexNames
332
341
// are automatically generated if set.
333
342
//
@@ -487,6 +496,7 @@ func (fs *FS) initRequestHandler() {
487
496
generateIndexPages : fs .GenerateIndexPages ,
488
497
compress : fs .Compress ,
489
498
compressBrotli : fs .CompressBrotli ,
499
+ compressZstd : fs .CompressZstd ,
490
500
compressRoot : compressRoot ,
491
501
pathNotFound : fs .PathNotFound ,
492
502
acceptByteRange : fs .AcceptByteRange ,
@@ -518,6 +528,7 @@ type fsHandler struct {
518
528
generateIndexPages bool
519
529
compress bool
520
530
compressBrotli bool
531
+ compressZstd bool
521
532
acceptByteRange bool
522
533
}
523
534
@@ -1049,14 +1060,14 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
1049
1060
mustCompress = true
1050
1061
fileCacheKind = brotliCacheKind
1051
1062
fileEncoding = "br"
1063
+ case h .compressZstd && ctx .Request .Header .HasAcceptEncodingBytes (strZstd ):
1064
+ mustCompress = true
1065
+ fileCacheKind = zstdCacheKind
1066
+ fileEncoding = "zstd"
1052
1067
case ctx .Request .Header .HasAcceptEncodingBytes (strGzip ):
1053
1068
mustCompress = true
1054
1069
fileCacheKind = gzipCacheKind
1055
1070
fileEncoding = "gzip"
1056
- case ctx .Request .Header .HasAcceptEncodingBytes (strZstd ):
1057
- mustCompress = true
1058
- fileCacheKind = zstdCacheKind
1059
- fileEncoding = "zstd"
1060
1071
}
1061
1072
}
1062
1073
@@ -1122,10 +1133,13 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
1122
1133
switch fileEncoding {
1123
1134
case "br" :
1124
1135
hdr .SetContentEncodingBytes (strBr )
1136
+ hdr .addVaryBytes (strAcceptEncoding )
1125
1137
case "gzip" :
1126
1138
hdr .SetContentEncodingBytes (strGzip )
1139
+ hdr .addVaryBytes (strAcceptEncoding )
1127
1140
case "zstd" :
1128
1141
hdr .SetContentEncodingBytes (strZstd )
1142
+ hdr .addVaryBytes (strAcceptEncoding )
1129
1143
}
1130
1144
}
1131
1145
0 commit comments