Skip to main content

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

Project details


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

File hashes

Hashes for starlette_precompressed_static-2022.10.13.tar.gz
Algorithm Hash digest
SHA256 6c5b63b1dda2d73a0cc3e917a7f971206478d5818de804457aea70b8a5753f13
MD5 72e03821ee9060020408f9fe664e32cd
BLAKE2b-256 b1b6511352801c3b5571cfcf9e485fe5650eb24aa4dcc8e35b4d7380ac0f3336

See more details on using hashes here.

File details

Details for the file starlette_precompressed_static-2022.10.13-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_precompressed_static-2022.10.13-py3-none-any.whl
Algorithm Hash digest
SHA256 89c334ee2d87ef4a11ce8899c33084907223e9c3b4f9ffa84afdc75652b8b731
MD5 6ce8c1aeca70d8163d08861fdf314a19
BLAKE2b-256 7481b1d0295d9aeaeeddb18c455fe4cd46673750f73d23c9a82f0e3a5dfc9dec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page