Pre-compression
SWS provides support to serve pre-compressed Gzip, Brotli and Zstandard (zstd) files directly from the disk.
SWS can look up existing pre-compressed file variants (.gz, .br or .zst) on disk and serve them directly with zero CPU cost with no on-the-fly compression overhead.
This feature can be controlled by the boolean --compression-static option or the equivalent SERVER_COMPRESSION_STATIC env. It is disabled by default.
Independent from dynamic compression
Static compression operates independently from on-the-fly compression. You can enable one without the other, or combine both for maximum efficiency: pre-compressed variants are tried first, and dynamic compression falls through automatically if no variant exists.
When the compression-static option is enabled and a pre-compressed file is found on the file system then it's served directly. Otherwise, if the pre-compressed file is not found then SWS just continues the normal workflow (trying to serve the original file requested instead). If compression is also enabled, the requested file can be compressed on the fly right after.
Compressed file type
The pre-compressed file type is determined by the Accept-Encoding header value.
Here is an example:
static-web-server --port 8787 --root /public --compression-static -g traceBelow are some relevant log entries to show how the feature works.
2022-09-22T21:30:12.904102Z INFO static_web_server::handler: incoming request: method=GET uri=/downloads/Capture5.png
2022-09-22T21:30:12.904218Z TRACE static_web_server::static_files: dir: base="/public", route="downloads/Capture5.png"
2022-09-22T21:30:12.904295Z TRACE static_web_server::compression_static: preparing pre-compressed file path variant of /public/downloads/Capture5.png
2022-09-22T21:30:12.904509Z TRACE static_web_server::compression_static: getting metadata for pre-compressed file variant /public/downloads/Capture5.png.gz
2022-09-22T21:30:12.904746Z TRACE hyper::proto::h1::conn: flushed({role=server}): State { reading: KeepAlive, writing: Init, keep_alive: Busy }
2022-09-22T21:30:12.904932Z TRACE static_web_server::static_files: file found: "/public/downloads/Capture5.png.gz"
2022-09-22T21:30:12.904983Z TRACE static_web_server::compression_static: pre-compressed file variant found, serving it directly
2022-09-22T21:30:12.905095Z TRACE hyper::proto::h1::conn: flushed({role=server}): State { reading: KeepAlive, writing: Init, keep_alive: Busy }
2022-09-22T21:30:12.905836Z TRACE encode_headers: hyper::proto::h1::role: Server::encode status=200, body=Some(Unknown), req_method=Some(GET)
2022-09-22T21:30:12.905965Z TRACE encode_headers: hyper::proto::h1::role: close time.busy=138µs time.idle=35.4µs
2022-09-22T21:30:12.906236Z DEBUG hyper::proto::h1::io: flushed 242 bytes