I recently set up Cloudflare on Eternum, and I wondered if they supported HTTP/2 push, and, if so, how. It turns out that they do, and it’s supported by simply adding a header to your response with the media you would like pushed to the client. Since that’s very easy for Django to do, I wondered if I could write some middleware that would parse the requested page, list all the static media (using Django’s {% static %} tag), and push them to the client.

I came up with the following middleware, after heavily stealing Django Debug Toolbar’s method of listing static media on a page. To use it, just copy it to a file, e.g. project/project/push_middleware.py and add it to your MIDDLEWARE list as project.push_middleware.push_middleware. That’s all you need to do, the middleware will add a Link header to your response and Cloudflare should push your media.

Here it is!:

Actually, I removed this from here. It is now released as its own library: https://github.com/skorokithakis/django-cloudflare-push.

Feel free to adapt the above to your own use, it’s released under the BSD license.