具有s3存储的django-pipeline无法压缩我的js [英] django-pipeline with s3 storage is not compressing my js

查看:46
本文介绍了具有s3存储的django-pipeline无法压缩我的js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在s3中使用django-pipeline.我已经成功地使用 collectstatic 组合了我的Javascript文件并将它们存储在s3存储桶中,但是由于某种原因它们没有被压缩(通过查看文件,文件的大小和内容进行验证-编码).否则,生成的合并后的 scripts.js 不能正常工作.

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.

以下是我使用django-pipeline所做的更改:

Here are the changes I made to use django-pipeline:

  1. 向已安装的应用添加了管道.
  2. STATICFILES_FINDERS 中添加了'pipeline.finders.PipelineFinder'.
  3. 设置 STATICFILES_STORAGE ='mysite.custom_storages.S3PipelineManifestStorage',其中此类在文档中定义,如下所示.
  4. 如下所示设置 PIPELINE_JS ,该方法有效,但未压缩.
  5. PIPELINE_ENABLED = True ,因为 DEBUG = True ,并且我在本地运行.
  6. PIPELINE_JS_COMPRESSOR ='pipeline.compressors.yuglify.YuglifyCompressor',即使这是默认设置.
  7. 通过 npm -g install yuglify 安装了Yuglify Compressor.
  8. PIPELINE_YUGLIFY_BINARY ='/usr/local/bin/yuglify',即使使用 env 的默认设置也可以.
  9. 使用有效的 {%负载管道%} {%javascript'script'%} .
  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.

更多细节:

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

如前所述, collectstatic 确实会生成 scripts.js ,只是未压缩.该命令的输出包括:

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'

我正在使用Django 1.8,django-pipeline 1.5.2和django-storages 1.1.8.

I'm using Django 1.8, django-pipeline 1.5.2, and django-storages 1.1.8.

类似的问题:

推荐答案

缺少的步骤是还要扩展 GZipMixin ,并且,它必须在父母列表中排在首位.

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

现在 collectstatic 也会为每个文件生成.gz版本,但是我的模板仍未引用.gz版本.

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:

要使其与S3一起使用,您需要更改静态文件存储url方法以返回.gz url(和staticfiles/pipeline模板标签,具体取决于您是否关心不支持的客户端gzip).同样不要忘记在s3上设置适当的标头以进行投放这些资产被压缩.

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

这仍然无法处理设置 Content-Encoding 标头.

This still doesn't handle setting the Content-Encoding header.

一个更简单的选择是使用S3Boto存储选项 AWS_IS_GZIPPED ,该选项执行gzip压缩并设置适当的标头.

A simpler alternative is to use the S3Boto Storages option AWS_IS_GZIPPED which performs gzipping AND sets the appropriate header.

需要更多内容来支持不带gzip的客户端.

More is required to support clients without gzip, however.

Amazon上的以下说明也很有用:从S3 .

Also useful are these instructions from Amazon on serving compressed files from S3.

这篇关于具有s3存储的django-pipeline无法压缩我的js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆