A small library for Starlette, to serve pre-compressed static resources
Project description
starlette-precompressed-static
A small library for Starlette, to serve pre-compressed static resources.
Installation
pip install starlette-precompressed-static
Usage
Just replace:
from starlette.staticfiles import StaticFiles
StaticFiles(directory="www")
with:
from starlette_precompressed_static import PreCompressedStaticFiles
PreCompressedStaticFiles(directory="www")
and you're good to go! By default both brotli and gzip are supported, and brotli is always preferred over gzip. It is possible to disable one of the algorithms, like so:
PreCompressedStaticFiles(directory="www", gzip=False)
Limitations
This is a very simple implementation, and it has some limitations:
- It wil always try to fetch the compressed version first, and fallback to the uncompressed version if that doesn't exist. This means that if you have some files that should not be compressed (e.g. fonts) they should probably be separated and served by a normal
StaticFiles()
. - It works by adding
.br
og.gzip
to the requested path, so it does not supporthtml
mode were the path may be a folder, or the response a pretty 404.html page.
A note on older Python versions
This library needs that the Python mimetypes module knows about brotli, which was added in Python 3.9, and therefore only Python 3.9 and newer are supported. It should work fine on older versions as well, but brotli will need to be added to the encodings_map in the mimetypes module, like so:
import mimetypes
if ".br" not in mimetypes.encodings_map:
# We need brotli to exist in the encodings map, so
# that brotli encoded files (e.g. xxx.html.br) are
# detected as the correct file type. This was added
# in Python 3.9, so on older versions we must add it
# ourselves.
mimetypes.encodings_map[".br"] = "br"
This may have unexpected side effects, so be aware.
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
File details
Details for the file starlette_precompressed_static-2022.10.13.tar.gz
.
File metadata
- Download URL: starlette_precompressed_static-2022.10.13.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c5b63b1dda2d73a0cc3e917a7f971206478d5818de804457aea70b8a5753f13 |
|
MD5 | 72e03821ee9060020408f9fe664e32cd |
|
BLAKE2b-256 | b1b6511352801c3b5571cfcf9e485fe5650eb24aa4dcc8e35b4d7380ac0f3336 |
File details
Details for the file starlette_precompressed_static-2022.10.13-py3-none-any.whl
.
File metadata
- Download URL: starlette_precompressed_static-2022.10.13-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89c334ee2d87ef4a11ce8899c33084907223e9c3b4f9ffa84afdc75652b8b731 |
|
MD5 | 6ce8c1aeca70d8163d08861fdf314a19 |
|
BLAKE2b-256 | 7481b1d0295d9aeaeeddb18c455fe4cd46673750f73d23c9a82f0e3a5dfc9dec |