1

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:

  1. Added pipeline to installed apps.
  2. Added 'pipeline.finders.PipelineFinder' to STATICFILES_FINDERS.
  3. Set STATICFILES_STORAGE = 'mysite.custom_storages.S3PipelineManifestStorage' where this class is as defined in the documentation, as seen below.
  4. Set PIPELINE_JS as seen below, which works but just isn't compressed.
  5. PIPELINE_ENABLED = True since DEBUG = True and I'm running locally.
  6. PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor' even though this should be default.
  7. Installed the Yuglify Compressor with npm -g install yuglify.
  8. PIPELINE_YUGLIFY_BINARY = '/usr/local/bin/yuglify' even though the default with env should work.
  9. 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:

4

1 回答 1

3

缺少的步骤是也扩展GZipMixin,并且,它必须是父母列表中的第一个:

from pipeline.storage import GZIPMixin

class S3PipelineManifestStorage(GZIPMixin, PipelineMixin, ManifestFilesMixin, S3BotoStorage):
    location = settings.STATICFILES_LOCATION

现在collectstatic也生成每个文件的 .gz 版本,但我的模板仍然没有引用 .gz 版本。

为了解决这个问题,作者

要使其与 S3 一起使用,您需要更改 staticfiles 存储 url 方法以返回 .gz url(以及静态文件/管道模板标签,具体取决于您是否关心不支持 gzip 的客户端)。也不要忘记在 s3 上设置正确的标头以将这些资产作为 gzip 压缩。

我改编了他在其他地方提供的一个例子,它覆盖了该方法:url

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

这仍然不能处理设置Content-Encoding标题。

一个更简单的替代方法是使用 S3Boto Storages 选项AWS_IS_GZIPPED,该选项执行 gzipping 并设置适当的标头。

然而,支持没有 gzip 的客户端需要更多。

来自 Amazon 的这些关于从 S3 提供压缩文件的说明也很有用。

于 2015-07-29T01:33:44.140 回答