Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to pipeline assets to a CDN with Django?

When it comes time to deploy, one best practice is compressing static assets (js, css), and moving them to a CDN. There are a few Django apps that handle some aspects (django-compressor, django-pipeline each come to mind), but they each have their own problems. For example,

  • django-compressor wants to generate content inside of the request/response loop, even when exclusively-offline compression is enabled.
  • django-pipeline doesn't store hashes anywhere, so it iterates through the path it writes compressed files to, regex-matches filenames, sorts those possible matches, and returns the last item in the list. Try doing this from a CDN! What?!

The (seemingly logical) pipeline workflow I'm looking for is this:

  1. Compress assets, using hashes of content for filename
  2. Stores those filenames in a cache or a module or something that can be easily referenced when it's time to render the HTML.

I shouldn't have to have every static file on all of my web front-end nodes -- only the CDN.

Right now, I feel like my best option is to hack support for caching filename hashes into django-pipeline, but I'm dreaming of a drop-in solution for this.

What are my best options in Django for this? Are there any?

Thanks!

like image 287
Carson Avatar asked Aug 22 '11 03:08

Carson


2 Answers

django-pipeline 1.1.12 is now caching hash, so you should be fine, see the changelog.

Disclaimer: I'm one of the author of django-pipeline.

like image 69
cyberdelia Avatar answered Sep 20 '22 17:09

cyberdelia


Are you running Django 1.3? If so, then you can write your own custom file storage backend that uploads things to a CDN. Luckily, if you want to use Amazon S3, then django-storages already does exactly what you want!

like image 1
Sam Starling Avatar answered Sep 23 '22 17:09

Sam Starling