Skip to content

With Korvex

Korvex is a Python web framework that can serve a folder of static files. This page shows how to make that folder as small as possible: korpress minifies the files once, they are compressed once (gzip and brotli), and Korvex serves the compressed copies. Korvex has its own command for it, which uses korpress.

With korvex build

Since Korvex 1.0.0rc9, the command korvex build does it all for you. It minifies with korpress, writes .gz files (and .br files when brotli is installed) next to the results, and leaves the serving to app.static.

pip install --pre "korvex[minify,brotli]"
korvex build static --out dist/static

After this command, dist/static holds the minified files and their compressed copies. --out dist/static copies the result into a new folder and leaves static/ as it is. Use --in-place instead to change the folder itself, for example inside a Docker image. The minify extra installs korpress, and the brotli extra is optional.

Then serve the built folder:

from korvex import App

app = App()
app.static("/static", "dist/static", precompressed=True)

The arguments are the URL prefix, the directory, and precompressed=True. With it, Korvex sends the compressed copies as they are, so no work is done per request.

A few things to know, all described in Precompressed files in the Korvex guide, which also lists every option:

  • Never run it on a templates folder. {{ }} and {% %} are not valid HTML or JavaScript, so minifying can change what a template says. Build only the folder you serve as static files.
  • Names are reserved. Next to a file X, the names X.gz, X.br and X.zst belong to its compressed copies. korvex build replaces or deletes them, so do not keep your own archive under one of these names beside its source.
  • Files named *.min.* are not minified again. If korpress rejects a file, that file keeps its original, the other files are still built, and the command exits with status 1.
  • Run it before the server starts, and again after you copy files to the machine that serves them. A compressed copy older than its original is ignored.

See precompression and gzip and brotli in the glossary.

Without Korvex

For another framework, or a plain web server, do the same steps yourself. Minifying alone is one command with the command line:

korpress static -o build/static --copy

Then compress the result. This short script does both steps with korpress and the standard library. It reads static/ and writes build/static/, and your sources stay untouched.

"""Minify, then gzip, a folder of static files."""

import gzip
import shutil
import sys
from pathlib import Path

import korpress

COMPRESS = {".html", ".css", ".js", ".mjs", ".svg", ".json", ".txt"}


def build(src: Path, dst: Path) -> None:
    shutil.rmtree(dst, ignore_errors=True)
    for path in sorted(p for p in src.rglob("*") if p.is_file()):
        out = dst / path.relative_to(src)
        out.parent.mkdir(parents=True, exist_ok=True)

        # 1. Minify HTML, CSS and JS; copy the rest, and files already minified.
        if korpress.kind_of(path) and ".min." not in path.name:
            korpress.minify_file(path, out)
        else:
            shutil.copyfile(path, out)

        # 2. Compress after the file is written, so the copy is not older.
        if out.suffix.lower() in COMPRESS:
            data = out.read_bytes()
            packed = gzip.compress(data, 9, mtime=0)
            if len(packed) < len(data):
                out.with_name(out.name + ".gz").write_bytes(packed)


if __name__ == "__main__":
    build(Path(sys.argv[1]), Path(sys.argv[2]))

Run it with python build_static.py static build/static.

  • kind_of tells which files korpress can minify, from the extension: .html, .css and .js (also .htm, .mjs, .cjs). Images, fonts and the like are copied.
  • minify_file(path, out) reads path, writes the result to out and returns the size before and after, in bytes. A JavaScript file that korpress cannot minify (an unterminated string, for example) raises korpress.KorpressError, so the build stops instead of shipping a broken file. See Limits and safety.
  • Brotli files are a little smaller than gzip files. To make them, install brotli (pip install brotli) and write brotli.compress(data, quality=11) to X.br the same way.
  • mtime=0 makes the gzip output the same on every run.

The same app.static("/static", "build/static", precompressed=True) line serves this result in Korvex.

In depth

Why this order

The order matters:

  1. Minify the files once, when you build. korpress does this, not the server.
  2. Compress the minified files (gzip, and brotli if you want it), once.
  3. Serve the compressed copies. Korvex sends them as they are, so no work is done per request.

Doing the work at build time is the point. Minifying or compressing on every request costs CPU each time and gives you a worse compression level, because the server has to be fast. At build time you can use the highest levels and take your time.

How Korvex picks a file

For GET /static/css/app.css, Korvex looks for app.css.br or app.css.gz (and .zst) next to the file, and sends the one the browser accepts in Accept-Encoding. The response keeps the type of the original file (text/css), and has Content-Encoding and Vary: Accept-Encoding. Korvex never creates these files while serving.

Real numbers

These numbers come from a copy of the static folder of Korvex's taskboard example: one HTML page, one stylesheet, one SVG icon and eleven JavaScript files, 78,309 bytes in total. They were measured with the script above, with gzip only. korvex build minifies with the same korpress, and on this folder it reports the same 78,309 down to 60,832 bytes.

File Original Minified Minified + gzip
css/app.css 21,178 17,411 4,560
index.html 6,658 5,960 2,036
js/store.js 9,015 6,292 2,092
js/ui/tasks.js 11,620 8,966 3,429
all 15 files 78,309 60,832 21,699

In bytes, over the whole folder:

  • Minifying alone saves 22%: 78,309 down to 60,832.
  • Minifying then gzip saves 72%: 78,309 down to 21,699.
  • Gzip alone, without minifying, gives 26,176. Minifying first makes the gzip output about 17% smaller.

Gzip does most of the work, as it removes repetition. Minifying adds the part that gzip cannot do, which is removing bytes that carry no meaning. These numbers come from one folder, so yours will differ.

A request to /static/css/app.css with Accept-Encoding: gzip, through korvex.testing.TestClient, returned status 200, Content-Encoding: gzip, Content-Type: text/css and a body of 4,560 bytes.

Notes

  • The output folder (dist/static or build/static) is generated. Keep it out of version control and make it part of your deploy.
  • korpress does not change the meaning of your code, and it does not rename anything. That also means it does not bundle files together: each file is minified on its own.