我正在尝试压缩所有网站文件以优化网站的性能。到目前为止,我已经包含了 HTML5 boilerplate .htaccess 中的压缩部分。例子。
以下是相关代码片段:
# ------------------------------------------------------------------------------
# | 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>
查看 gzipwtf.com 后,客户网站似乎正在对所有 CSS 文件进行 GZIP 压缩,但由于某种原因 JavaScript 被忽略了。我对 .htaccess
和这方面的乐趣还很陌生,所以我不确定如何强制压缩。有什么建议吗?
最佳答案
为了确保正确的互操作性,您需要为 JavaScript 指定两种 mime 类型:
application/javascript
text/javascript
还有一个,不过好像只在过渡期使用过:
application/x-javascript
关于javascript - 对 JavaScript 文件强制进行 GZIP 压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25641486/