16

I'm using django-compressor to concatenate and compress my CSS and JS files on this site. I'm serving static files from an S3 bucket.

On my local copy of the site, using a different S3 bucket, this all works perfectly. But on the live site, hosted on Heroku, it all works except the relative URLs for images in the CSS files do not get re-written.

eg, this line in the CSS file:

background-image: url("../img/glyphicons-halflings-grey.png");

gets rewritten to:

background-image:url('https://my-dev-bucket-name.s3.amazonaws.com/static/img/glyphicons-halflings-grey.png')

on my development site, but isn't touched on the live site. So the live site ends up looking in pepysdiary.s3.amazonaws.com/static/CACHE/img/ for the images (as it's relative to the new, compressed CSS file).

For now, I've put a directory at that location containing the images, but I can't work out why there's this difference. Both sites have this in their settings:

COMPRESS_CSS_FILTERS = [
    # Creates absolute urls from relative ones.
    'compressor.filters.css_default.CssAbsoluteFilter',
    # CSS minimizer.
    'compressor.filters.cssmin.CSSMinFilter'
]

And the CSS files are being minimised just fine... but it's like the other filter isn't being applied on the live site.

Phil Gyford
  • 13,432
  • 14
  • 81
  • 143
  • FWIW, I gave up on this. Couldn't work out the issue, and then later the CSS and JS files Compressor generated on S3 were returning 403 Forbidden errors. Life's too short. – Phil Gyford Mar 21 '13 at 10:53

3 Answers3

20

I recently ran into this issue on heroku, and running the latest version of django-compressor (1.3) does not solve the problem. I will provide the solution that I am using, as well as an explanation of the problems I ran into along the way.

The solution

I created my own 'CssAbsoluteFilter' that removes the settings.DEBUG check from the 'find' method like this:

# compress_filters.py
from compressor.filters.css_default import CssAbsoluteFilter
from compressor.utils import staticfiles


class CustomCssAbsoluteFilter(CssAbsoluteFilter):
    def find(self, basename):
        # The line below is the original line.  I removed settings.DEBUG.
        # if settings.DEBUG and basename and staticfiles.finders:
        if basename and staticfiles.finders:
            return staticfiles.finders.find(basename)

# settings.py
COMPRESS_CSS_FILTERS = [
# 'compressor.filters.css_default.CssAbsoluteFilter',
'app.compress_filters.CustomCssAbsoluteFilter',
'compressor.filters.cssmin.CSSMinFilter',
]

The absolute urls now always work for me whether DEBUG = True or False.

The Problem

The issue is connected to 'compressor.filters.css_default.CssAbsoluteFilter', your DEBUG setting, and the fact that heroku has a read-only file system and overwrites your app files every time you deploy.

The reason compress works correctly on your development server is because CssAbsoluteFilter will always find your static files when DEBUG = True, even if you never run 'collectstatic'. It looks for them in STATICFILES_DIRS.

When DEBUG = False on your production server, CssAbsoluteFilter assumes that static files have already been collected into your COMPRESS_ROOT and won't apply the absolute filter if it can't find the files.

Jerdez, django-compressor's author, explains it like this:

the CssAbsoluteFilter works with DEBUG = False if you've successfully provided the files to work with. During development compressors uses the staticfiles finder as a convenience so you don't have to run collectstatic all the time.

Now for heroku. Even though you are storing your static files on S3, you need to also store them on heroku (using CachedS3BotoStorage). Since heroku is a read-only file system, the only way to do this is to let heroku collect your static files automatically during deployment (see https://devcenter.heroku.com/articles/django-assets).

In my experience, running 'heroku run python manage.py collectstatic --noinput' manually or even in your Procfile will upload the files to S3, but it will NOT save the files to your STATIC_ROOT directory (which compressor uses by default as the COMPRESS_ROOT). You can confirm that your static files have been collected on heroku using 'heroku run ls path/to/collected'.

If your files have been collected on heroku successfully, you should be able to compress your files successfully as well, without the solution I provided above.

However, it seems heroku will only collect static files if you have made changes to your static files since your last deploy. If no changes have been made to your static files, you will see something like "0 of 250 static files copied". This is a problem because heroku completely replaces your app contents when you deploy, so you lose whatever static files were previously collected in COMPRESS_ROOT/STATIC_ROOT. If you try to compress your files after the collected files no longer exist on heroku and DEBUG = False, the CssAbsoluteFilter will not replace the relative urls with absolute urls.

My solution above avoids the heroku problem altogether, and replaces relative css urls with absolute urls even when DEBUG = False.

Hopefully other people will find this information helpful as well.

Belgand
  • 3
  • 1
Michael Godshall
  • 880
  • 11
  • 18
  • I wish I had found this 3 days ago. :) I put the collectstatic and compress calls in bin/post_compile and heroku config:set DISABLE_COLLECTSTATIC=0 – derek73 Jan 21 '14 at 04:02
  • This also explains why it worked the very first time I deployed, but then failed every time after that. Thanks for the thorough explanation. – derek73 Jan 21 '14 at 04:22
  • for those using sass, this and `COMPRESS_PRECOMPILERS` with `('text/x-scss', 'django_libsass.SassCompiler')` solved my issues – fjsj Feb 06 '15 at 00:57
0

I've had this exact same problem for a month now, but this is fixed in the 1.3 release (3/18/13, so you were probably on 1.2), so just upgrade:

pip install -U django-compressor

The exact problem I gave up on working out, but it's related to Heroku and CssAbsoluteFilter being called but failing at _converter method. Looking at the 1.3 changelog, the only related commit is this: https://github.com/jezdez/django_compressor/commit/8254f8d707f517ab154ad0d6d77dfc1ac292bf41

I gave up there, life's too short.

yuanma
  • 1
  • Interesting, thanks yuanma! So have you tried 1.3 and it fixes the problem for you? I can't see how that particular commit would fix things, but I may be wrong, or there might be something else in there that does the job. – Phil Gyford Mar 25 '13 at 10:12
  • Upgrading to 1.3 did not work for me. See my solution: http://stackoverflow.com/a/17033883/387337 – Michael Godshall Jun 10 '13 at 22:38
0

Meanwhile this has been fixed in django-compressor 1.6. From the changelog:

Apply CssAbsoluteFilter to precompiled css even when compression is disabled

i.e. the absolute filter is run even with DEBUG = True.

karyon
  • 1,957
  • 1
  • 11
  • 16