Skip to main content

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

Project description

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


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 because gzip is built into the Python standard library.

However, if you want highly-optimized gzip files you’ll need the zopfli module. And if you want to have the very best compression currently available, you’ll need to install the brotli module (which will require extra work in step 3).

$ pip install pelican_precompress
$ pip install zopfli  # This produces smaller gzip'd files. Use it!
$ pip install brotli  # This requires extra work in step 3.

Further reading: zopfli, brotli

2. Configure Pelican

You’ll need to import the plugin and add it 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!

import pelican_precompress

PLUGINS = [pelican_precompress]

# PRECOMPRESS_GZIP = True or False
# PRECOMPRESS_ZOPFLI = True or False
# PRECOMPRESS_BROTLI = 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 compression. To get it, you’ll need to compile the static brotli module as an nginx dynamic module, or recompile nginx from scratch. When it’s done you’ll add something like this to your nginx configuration file:

load_module /usr/lib/nginx/modules/ngx_http_brotli_static_module.so;

http {
    brotli_static on;
}

Further reading: gzip_static, gzip_vary, nginx brotli 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_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_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. Like PRECOMPRESS_ZOPFLI, if you set this to True when the brotli 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.

Testing

pelican_precompress has 100% test coverage. If you’d like to test the code yourself, clone the git repository and run these commands:

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install tox
(venv) $ 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 can run against; and coverage keeps track of which lines of code (and which branches) have been run.

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

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

pelican_precompress-1.1.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

pelican_precompress-1.1.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pelican_precompress-1.1.2.tar.gz.

File metadata

  • Download URL: pelican_precompress-1.1.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for pelican_precompress-1.1.2.tar.gz
Algorithm Hash digest
SHA256 1dba7dd5eee06123bff4934ad665ff0c3c7a0494db249d8b1243623c06419b5b
MD5 cf173571826aca34c81cf3e9227fa834
BLAKE2b-256 d8920cb3016ca5641c292a9a61ff51002d41399f9d1819e1a5f4e647e40d314e

See more details on using hashes here.

Provenance

File details

Details for the file pelican_precompress-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: pelican_precompress-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for pelican_precompress-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 32b9a5e4482f877d578f65e38d9de86731642c6a715e5f0a33dd0b9864263bc0
MD5 4ac896453ec686f3584b7e75bbc94288
BLAKE2b-256 8aa462a65340286c2c8166e1961bb94946a3e748fc54a15efb203643697161eb

See more details on using hashes here.

Provenance

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