Skip to main content

Inlines critical path CSS and defers loading full CSS asynchronously.

Project description


Inlines critical path CSS and defers loading full CSS asynchronously.

How it works

`django-critical` parses the HTML between `{% critical %}` and `{% endcritical
%}` and searches for CSS.

{% critical %}
<link rel="stylesheet" href="//">
.jumbotron h1 {
color: red;
{% endcritical %}

It downloads and concatenates all CSS that it finds and uses
[penthouse]( to extract the
critical path CSS rules from it, which end up inlined and minified in place of
the original CSS.


Later in the HTML, before the closing `</body>` tag, the `{% critical_async %}`
template tag takes care of loading the rest of the CSS using FilamentGroup's


* Install `django-critical`:

pip install django-critical

* Add `critical` to your `INSTALLED_APPS` setting:

# other apps

* Add `critical.middleware.CriticalCssMiddleware` to your `MIDDLEWARE_CLASSES`

# other middlewares

* Point your `CRITICAL_PENTHOUSE_PATH` setting to the correct path, e.g.:

BASE_DIR, 'node_modules/penthouse/penthouse.js')


`django-critical` calculates the critical path CSS for the first request, caches
the result and reuses this CSS verbatim for further requests. Most web
applications have different critical path CSS for different groups of pages,
though. This can be solved using the `{% critical_key "<key>" %}` template tag,
so that different templates can have different caching keys.

Is this stable and ready for production use?

No. `django-critical` is in a very early stage of development, so you should use
it at your own risk. [Bug
reports]( and
[contributions]( are
welcome, though!

Project details

Release history Release notifications

This version
History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django-critical-0.1.tar.gz (6.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page