Skip to main content

Pre-compress your Pelican site using gzip, brotli, zstandard, and zopfli!


Are you using Pelican, the static site generator? If so, great! Are you pre-compressing your static files to have the fastest site possible? If not, install pelican-precompress today! It’s the plugin that makes your visitors happy and saves you money!

Installation

There are three steps required to start using static compression:

  1. Install the plugin and any supporting Python packages you want.

  2. Configure Pelican to use the pelican-precompress plugin.

  3. Configure your web server to use static, pre-compressed files.

1. Install the Python modules

At minimum, you’ll need to install the pelican-precompress plugin. It will automatically generate gzip files on all versions of Python, and zstandard files on Python 3.14 and higher, because those compression algorithms are built into the Python standard library.

However, if you want better compression you’ll need to install additional packages. pelican-precompress exposes each compression algorithm by name as a package extra:

  • brotli

  • zopfli

  • zstandard (for Python 3.13 and lower)

These can be selected as a comma-separated list during install:

$ pip install pelican-precompress[zstandard]
$ pip install pelican-precompress[zstandard,brotli,zopfli]

Further reading: brotli package, backports.zstd package, zopfli package

2. Configure Pelican

pelican-precompress supports Pelican’s namespace plugin architecture and will be automatically detected and loaded when Pelican runs.

However, if you’re maintaining a list of plugins for Pelican to use then you’ll need to add pelican-precompress to the list of active plugins.

Feel free to copy and paste the code below into your Pelican configuration file. Just uncomment and edit the configuration lines to your liking…or leave them alone because the defaults are awesome!

# You only need to add pelican-precompress to your PLUGINS list
# if your configuration file already has a PLUGINS list!
#
# PLUGINS = ['pelican.plugins.precompress']

# These options can be customized as desired.
#
# PRECOMPRESS_GZIP = True or False
# PRECOMPRESS_BROTLI = True or False
# PRECOMPRESS_ZSTANDARD = True or False
# PRECOMPRESS_ZOPFLI = True or False
# PRECOMPRESS_OVERWRITE = False
# PRECOMPRESS_MIN_SIZE = 20
# PRECOMPRESS_TEXT_EXTENSIONS = {
#     '.atom',
#     '.css',
#     '.html',
#     '.but-the-default-extensions-are-pretty-comprehensive',
# }

Further reading: Pelican plugins

3. Configure nginx

nginx supports gzip compression right out of the box. To enable it, add something like this to your nginx configuration file:

http {
    gzip_static on;
    gzip_vary on;
}

At the time of writing, nginx doesn’t natively support brotli or zstandard compression.

To serve pre-compressed brotli files, you’ll need the static brotli module. To serve pre-compressed zstandard files, you’ll need the static zstandard module. When either or both of those are installed, you’ll add something like this to your nginx configuration file:

load_module modules/ngx_http_brotli_static_module.so;
load_module modules/ngx_http_zstd_static_module.so;

http {
    brotli_static on;
    zstd_static on;
}

Further reading: gzip_static, gzip_vary, nginx brotli module, nginx zstd module

Configuration

There are a small number of configuration options available. You set them in your Pelican configuration file.

  • PRECOMPRESS_GZIP (bool, default is True)

    This is always True unless you set this to False. For example, you might turn this off during development.

  • PRECOMPRESS_BROTLI (bool, default is True if brotli is installed)

    If the brotli module is installed this will default to True. You might set this to False during development. If you set this to True when the brotli module isn’t installed then nothing will happen.

  • PRECOMPRESS_ZSTANDARD (bool, default is True if zstandard is available)

    When running on Python 3.14 or higher with zstandard support compiled in, or if the pyzstd module is installed, this will default to True. You might set this to False during development. If you set this to True when the zstandard compression isn’t available then nothing will happen.

  • PRECOMPRESS_ZOPFLI (bool, default is True if zopfli is installed)

    If the zopfli module is installed this will default to True. You might set this to False during development. Note that if you try to enable zopfli compression but the module isn’t installed then nothing will happen.

  • PRECOMPRESS_OVERWRITE (bool, default is False)

    When pelican-precompress encounters an existing compressed file it will refuse to overwrite it. If you want the plugin to overwrite files you can set this to True.

  • PRECOMPRESS_TEXT_EXTENSIONS (Set[str])

    This setting controls which file extensions will be pre-compressed.

    If you modify this setting in the Pelican configuration file it will completely replace the default extensions!

  • PRECOMPRESS_MIN_SIZE (int, default is 20)

    Small files tend to result in a larger file size when compressed, and any improvement is likely to be marginal. The default setting is chosen to avoid speculatively compressing files that are likely to result in a larger file size after compression.

    To try compressing every file regardless of size, set this to 0.

Development

If you’d like to develop and/or test the code yourself, clone the git repository and run these commands to set up a Python virtual environment, install dependencies, and run the test suite:

python -m venv .venv

# Activate the virtual environment (Linux)
source .venv/bin/activate

# Activate the virtual environment (Windows)
& .venv/Scripts/Activate.ps1

python -m pip install poetry pre-commit tox
pre-commit install
poetry install

# Run the test suite
tox

The test suite uses tox to setup multiple environments with varying dependencies using multiple Python interpreters; pytest allows the test suite to have parametrized tests; pyfakefs creates a fake filesystem that the tests safely create and erase files in; and coverage keeps track of which lines of code have been run.

pelican-precompress has 100% test coverage, but there may still be bugs. Please report any issues that you encounter.

Further reading: poetry, tox, venv, pytest, pyfakefs, coverage

Release files for pelican-precompress 2.4.0

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

Source distribution (sdist)

Source distribution for pelican-precompress 2.4.0
File Size Uploaded
pelican_precompress-2.4.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pelican-precompress 2.4.0
File Interpreter ABI Platform
pelican_precompress-2.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.0 kB

Release files / pelican_precompress-2.4.0.tar.gz

Download URL pelican_precompress-2.4.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
078063a4cdba962538263b6c35afdcf4817c2171a783a227b7147caf9ff49ac4
BLAKE2b-256 checksum
How to use checksums
26db94449a106833680a59282f478af37070d8fa61dcf72c8d19fd688136a674
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.10

Release files / pelican_precompress-2.4.0-py3-none-any.whl

Download URL pelican_precompress-2.4.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15bdf85cee644bd79bb5e0d3eea8e0effb46df2ec984fd342b85eafefc8242e8
BLAKE2b-256 checksum
How to use checksums
4295073e376c8b061ae4c8e7f0c6a309b5d8234b3fe8dfe160dc5efc50297173
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.10

Release history Release notifications | RSS feed

This release

2.4.0 This release

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.0.0

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