Skip to main content

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 support html 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.

Release files for starlette-precompressed-static 2022.10.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for starlette-precompressed-static 2022.10.13
File Size Uploaded
starlette_precompressed_static-2022.10.13.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for starlette-precompressed-static 2022.10.13
File Interpreter ABI Platform
starlette_precompressed_static-2022.10.13-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / starlette_precompressed_static-2022.10.13.tar.gz

Download URL starlette_precompressed_static-2022.10.13.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6c5b63b1dda2d73a0cc3e917a7f971206478d5818de804457aea70b8a5753f13
BLAKE2b-256 checksum
How to use checksums
b1b6511352801c3b5571cfcf9e485fe5650eb24aa4dcc8e35b4d7380ac0f3336
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / starlette_precompressed_static-2022.10.13-py3-none-any.whl

Download URL starlette_precompressed_static-2022.10.13-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89c334ee2d87ef4a11ce8899c33084907223e9c3b4f9ffa84afdc75652b8b731
BLAKE2b-256 checksum
How to use checksums
7481b1d0295d9aeaeeddb18c455fe4cd46673750f73d23c9a82f0e3a5dfc9dec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release history Release notifications | RSS feed

This release

2022.10.13 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page