Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django compressor using gzip to serve javascript

I am trying to serve gzip files from amazon s3. This is my settings.py:

AWS_IS_GZIPPED = True
AWS_PRELOAD_METADATA = True 
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'elasticbeanstalk-eu-west-1-2051565523'
STATIC_URL = 'https://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
COMPRESS_OFFLINE = True
COMPRESS_ENABLED = True
COMPRESS_URL = STATIC_URL
COMPRESS_CSS_FILTERS = [
    'compressor.filters.css_default.CssAbsoluteFilter',
    'compressor.filters.cssmin.CSSMinFilter'
]
COMPRESS_JS_FILTERS = [
    'compressor.filters.jsmin.JSMinFilter',
]
COMPRESS_STORAGE = 'compressor.storage.GzipCompressorFileStorage' 

When I do this django creates *.gz files for every *.js and *.css compressed but strangely only the *.css files are served as gzip. I can see on the aws s3 that the .css files have the Content-Encoding: gzip and the *.js don't. What is going on here?

like image 909
João Abrantes Avatar asked Oct 03 '22 20:10

João Abrantes


1 Answers

I had the same issue and was able to resolve this by adding text/javascript to the GZIP_CONTENT_TYPES setting like so:

GZIP_CONTENT_TYPES = (
    'text/css',
    'application/javascript',
    'application/x-javascript',
    'text/javascript'
)

I'm not sure why, but the default value of GZIP_CONTENT_TYPES in django-storages==1.1.8 does not seem to include text/javascript so it seems django-compressor skips GZIP for compressed .JS files with this content type.

See: http://code.larlet.fr/django-storages/src/e27c8b61ab57e5afaf21cccfee005c980d89480f/storages/backends/s3boto.py?at=default#cl-236

like image 175
Hakan B. Avatar answered Oct 07 '22 20:10

Hakan B.