Minify & Asset Optimization
lila optimize scans public/,
writes minified copies of JS/CSS and WebP versions of PNG/JPG images, and records the mapping
in a manifest. Your templates keep referencing the original names:
public() swaps in the optimized file and appends a
cache-busting version.
Requirements
rjsminfor JavaScript — without it, JS files are skipped (never copied unminified over the original).rcssminfor CSS — without it, CSS files are skipped.Pillowfor PNG/JPG → WebP conversion.
# Everything at once
pip install "lila-framework[optimize]"
# Or individually
pip install rjsmin rcssmin pillow
Running it
# From the project root (alias: lila-optimize)
lila optimize
The command:
- walks every file under
public/; - minifies
*.js(except*.min.js) to*.min.jsand*.css(except*.min.css) to*.min.css; - converts
*.png,*.jpgand*.jpegto*.webp(quality 82); - writes
app/cache/assets_manifest.jsonandapp/cache/manifest_cache.py(ASSET_MANIFEST = {"js/app.js": "js/app.min.js", ...}).
MINIFY_HTML setting. For transport compression use
App(..., gzip=True) (default when enabled in main.py) or gzip/brotli
in your reverse proxy.
How public() consumes the manifest
The template helper loads app/cache/assets_manifest.json (or
manifest_cache.py) once per process, maps the requested path, and appends
?v=<file-mtime>. In production (DEBUG off and
APP_URL set) the URL is absolute.
<!-- In a Jinja2 template: reference the original file, not the minified one -->
<link rel="stylesheet" href="{{ public('css/app.css') }}">
<script src="{{ public('js/app.js') }}"></script>
<!-- After `lila optimize` these resolve to: -->
<!-- /css/app.min.css?v=1730000000 -->
<!-- /js/app.min.js?v=1730000000 -->
<!-- asset() builds the whole tag: -->
{{ asset('css/app.css') | safe }}
from lila.core.templates import public, asset
public("js/lila-client.js") # "/js/lila-client.min.js?v=1730000000"
asset("css/app.css") # a <link rel="stylesheet"> tag pointing at /css/app.min.css?v=...
Cache busting & static headers
StaticCacheMiddleware adds Cache-Control headers for
/public, /js, /img, /swagger and
/uploads. Requests with a ?v= parameter are marked
public, max-age=31536000, immutable; other static files use
STATIC_CACHE_MAX_AGE (86400 by default). Because the version is taken from the
file mtime, replacing an asset changes its URL automatically.
Deployment order
- Deploy the new
public/files. - Run
lila optimize(writes the new manifest). - Restart the app so the per-process URL cache is rebuilt. If you run multiple workers,
run
optimizebefore starting them.
Tailwind note: the bundled layouts use the Tailwind Play CDN in
development. For production, generate a precompiled CSS file, place it under
public/css/ and reference it with public('css/tailwind.css');
then lila optimize can minify it like any other stylesheet.