Compression
SWS provides Gzip, Deflate, Brotli and Zstandard (zstd) compression of HTTP responses on the fly (dynamically).
This feature is enabled by default and can be controlled by the boolean -x, --compression option or the equivalent SERVER_COMPRESSION env.
Independent from static compression
Dynamic (on-the-fly) compression operates independently from pre-compressed files serving. Both features can be enabled individually or combined: static compression is tried first (zero CPU cost), and if no pre-compressed variant is found, dynamic compression kicks in automatically.
static-web-server --port 8787 --root ./my-public-dir --compressionChoice of compression algorithm
The compression algorithm is determined by the Accept-Encoding header and the compression support built into SWS. By default SWS builds with support for Gzip, Deflate, Brotli and Zstandard algorithms.
SWS honors the qualities specified by the client to choose the algorithm. In case of equal quality for several algorithms (or no qualities at all), the internal priority will be selected according to this list:
ZstandardBrotliGzipDeflate
MIME types compressed
Compression is only applied to files with the MIME types listed below, indicating text and similarly well compressing formats.
text/*- Application types that are essentially text/structured data.
application/csvapplication/graphqlapplication/javascriptapplication/jsonapplication/rtfapplication/sqlapplication/x-yamlapplication/xmlapplication/yaml
- Binary types that are not text but considered compressible.
application/wasmapplication/font-sfntapplication/vnd.ms-fontobjectimage/x-iconimage/vnd.microsoft.icon
Compression level
SWS allows selecting the compression level via --compression-level command line option or the equivalent SERVER_COMPRESSION_LEVEL env. The available values are fastest, best and default. fastest will result in the lowest CPU load but also the worst compression factor. best will attempt to compress the data as much as possible (not recommended with Brotli or Zstandard compression, will be very slow). default tries to strike a balance, choosing a compression level where compression factor is already fairly good but the CPU load is still low.