Static file digest cache busting for FastAPI/Starlette
Project description
FastAPI Static Digest
A Starlette/FastAPI plugin to digest and compress static files, and integrate with Jinja templates.
pip install fastapi-static-digest
Overview
There are two classes: StaticDigest and StaticDigestCompiler.
The StaticDigestCompiler.compiler
method will produce the digested directory. The StaticDigest
(and starlette.staticfiles.StaticFiles
) should be configured to read this directory.
The compilation can be added to your build process, for instance with a click
CLI command (see below). Alternatively, the FASTAPI_STATICDIGEST_RELOAD=1
environment variable can be set to enable "hot reloading" during development.
Usage
# app.py
app_root = Path(__file__).parent
static_src = app_root / "static"
static = StaticDigest(source_dir=static_src)
routes = [
Mount('/static', app=StaticFiles(directory=static.directory), name="static"),
]
app = Starlette(routes=routes)
# manage.py
from .app import app_root
@click.command()
def compile():
src = app_root / "static"
click.echo("Source dir %s" % src)
compiler = StaticDigestCompiler(source_directory=src)
compiler.compile()
click.echo("Done.")
Jinja2 Integration
# app.py
app_root = Path(__file__).parent
static_src = app_root / "static"
static = StaticDigest(source_dir=static_src)
templates = Jinja2Templates(app_root / "templates")
static.register_static_url_for(templates)
routes = [
Mount('/static', app=StaticFiles(directory=static.directory), name="static"),
]
app = Starlette(routes=routes)
<!-- index.html -->
{{ static_url_for("static", "app.css" )}}
renders to
https://myhost.com/static/app.92fede82119d2e012f890e1102080a45.css
Development
PRs and issues are welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fastapi-static-digest-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35ed3fc8fa8e584dd64bdff48bc8044d8bf217d254f240ff813fd5e3636bafb7 |
|
MD5 | 47fa449e47d6b5bbc221b5bb4fc3de3d |
|
BLAKE2b-256 | 5ff16a9ef65b30a0f83aa1d144c704116f9e9ea045aeef02d40249199ff67924 |
Hashes for fastapi_static_digest-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e914148bfca39b5bad8d784ffd50037a2b7be577d5630dbb378ff59ab5ec3c2c |
|
MD5 | 59faa1c0e238d9904211ffd065db2c02 |
|
BLAKE2b-256 | b6940a64fcc042af4428c8fdc29cb3ef943e3ff893d856c0cf2939fa99d90e81 |