Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
强制对JavaScript文件进行GZIP压缩_Javascript_Performance_.htaccess_Compression - Fatal编程技术网

强制对JavaScript文件进行GZIP压缩

强制对JavaScript文件进行GZIP压缩,javascript,performance,.htaccess,compression,Javascript,Performance,.htaccess,Compression,我试图压缩所有的网站文件,以优化网站的性能。到目前为止,我已经包括了示例中的压缩部分 以下是相关的代码片段: # ------------------------------------------------------------------------------ # | Compression | # -------------------------------

我试图压缩所有的网站文件,以优化网站的性能。到目前为止,我已经包括了示例中的压缩部分

以下是相关的代码片段:

# ------------------------------------------------------------------------------
# | Compression                                                                |
# ------------------------------------------------------------------------------

<IfModule mod_deflate.c>

# Force compression for mangled headers.
# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping
<IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
        SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
        RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
</IfModule>

# Compress all output labeled with one of the following MIME-types
# (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
#  and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
#  as `AddOutputFilterByType` is still in the core directives).
<IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml \
                                  application/javascript \
                                  application/json \
                                  application/rss+xml \
                                  application/vnd.ms-fontobject \
                                  application/x-font-ttf \
                                  application/x-web-app-manifest+json \
                                  application/xhtml+xml \
                                  application/xml \
                                  font/opentype \
                                  image/svg+xml \
                                  image/x-icon \
                                  text/css \
                                  text/html \
                                  text/plain \
                                  text/x-component \
                                  text/xml
</IfModule>

</IfModule>
#------------------------------------------------------------------------------
#|压缩|
# ------------------------------------------------------------------------------
#强制压缩损坏的标题。
# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping
SetEnvIfNoCase^((gzip | deflate)\s*,?\s*)+|[X~-]{4,13}$有接受编码
RequestHeader追加接受编码“gzip,deflate”env=HAVE_Accept-Encoding
#压缩使用以下MIME类型之一标记的所有输出
#(对于低于2.3.7的Apache版本,您不需要启用'mod_filter'`
#并且可以删除``和``行
#因为'AddOutputFilterByType'仍在核心指令中)。
AddOutputFilterByType DEFLATE应用程序/atom+xml\
应用程序/javascript\
应用程序/json\
application/rss+xml\
应用程序/vnd.ms-fontobject\
应用程序/x-font-ttf\
application/x-web-app-manifest+json\
application/xhtml+xml\
应用程序/xml\
字体/开放式\
image/svg+xml\
图像/x图标\
文本/css\
文本/html\
文本/纯文本\
文本/x组件\
文本/xml

在查看gzipwtf.com之后,客户端网站似乎正在gzip所有CSS文件,但由于某些原因JavaScript被忽略。我对
.htaccess
的乐趣和这方面的东西还相当陌生,所以我不确定如何强制压缩。有什么建议吗?

为了确保适当的互操作性,您需要为JavaScript指定两种mime类型:

application/javascript
text/javascript
还有一个,但afaict仅在过渡期使用:

application/x-javascript

英雄谢谢,看起来很好用。如果你想把它作为一个答案记录下来,我会给你荣誉。我还提出了HTML5样板回购的一个问题,以确保它在未来得到修复。再次感谢!