问题
I'm using django-pipeline with s3. I'm successfully using collectstatic
to combined my Javascript files and store them in my s3 bucket, but they are not getting compressed for some reason (verified by looking at the file, its size, and its content-encoding). Otherwise things are working correctly with the combined scripts.js
that is produced.
Here are the changes I made to use django-pipeline:
- Added
pipeline
to installed apps. - Added
'pipeline.finders.PipelineFinder'
toSTATICFILES_FINDERS
. - Set
STATICFILES_STORAGE = 'mysite.custom_storages.S3PipelineManifestStorage'
where this class is as defined in the documentation, as seen below. - Set
PIPELINE_JS
as seen below, which works but just isn't compressed. PIPELINE_ENABLED = True
sinceDEBUG = True
and I'm running locally.PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
even though this should be default.- Installed the Yuglify Compressor with
npm -g install yuglify
. PIPELINE_YUGLIFY_BINARY = '/usr/local/bin/yuglify'
even though the default withenv
should work.- Using the
{% load pipeline %}
and{% javascript 'scripts' %}
which work.
More detail:
PIPELINE_JS = {
'scripts': {
'source_filenames': (
'lib/jquery-1.11.1.min.js',
...
),
'output_filename': 'lib/scripts.js',
}
}
class S3PipelineManifestStorage(PipelineMixin, ManifestFilesMixin, S3BotoStorage):
location = settings.STATICFILES_LOCATION
As mentioned, collectstatic
does produce scripts.js
just not compressed. The output of that command includes:
Post-processed 'lib/scripts.js' as 'lib/scripts.js'
I'm using Django 1.8, django-pipeline 1.5.2, and django-storages 1.1.8.
Similar questions:
- django-pipeline not compressing
- django pipeline with S3 storage not compressing
回答1:
The missing step was to also extend GZipMixin
, AND, it has to be first in the list of parents:
from pipeline.storage import GZIPMixin
class S3PipelineManifestStorage(GZIPMixin, PipelineMixin, ManifestFilesMixin, S3BotoStorage):
location = settings.STATICFILES_LOCATION
Now collectstatic
produces a .gz version of each file as well, but my templates still weren't referencing the .gz version.
To address this the author says:
To make it work with S3, you would need to change the staticfiles storage url method to return .gz urls (and staticfiles/pipeline template tags depending if you care for clients that don't support gzip). Also don't forget to setup the proper header on s3 to serve theses assets as being gzipped.
I adapted an example he provided elsewhere, which overrides the url
method:
class S3PipelineManifestStorage(GZIPMixin, PipelineMixin, ManifestFilesMixin, S3BotoStorage):
location = settings.STATICFILES_LOCATION
def url(self, name, force=False):
# Add *.css if you are compressing those as well.
gzip_patterns = ("*.js",)
url = super(GZIPMixin, self).url(name, force)
if matches_patterns(name, gzip_patterns):
return "{0}.gz".format(url)
return url
This still doesn't handle setting the Content-Encoding
header.
A simpler alternative is to use the S3Boto Storages option AWS_IS_GZIPPED
which performs gzipping AND sets the appropriate header.
More is required to support clients without gzip, however.
Also useful are these instructions from Amazon on serving compressed files from S3.
来源:https://stackoverflow.com/questions/31679243/django-pipeline-with-s3-storage-is-not-compressing-my-js