Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

.htaccess - How to send compressed (deflated) SVG via Apache2?

I have specified the following attributes in my site's .htaccess file:

AddOutputFilterByType DEFLATE image/svg+xml
DeflateCompressionLevel 9
Header append Vary Accept-Encoding

However, my SVG asset is not being sent in compressed form:

$ curl https://example.org/assets/svg/asset.svg --silent -H "Accept-Encoding: gzip,deflate" --write-out "${size_download}
" --output /dev/null                 
152655                                                                                                                                                                                                                                  
$ curl https://example.org/assets/svg/asset.svg --silent --write-out "%{size_download}
" --output /dev/null
152655

I verified that this asset (asset.svg) is being sent with MIME type image/svg+xml using Chrome, but using the Web Developer tools, this specific file is not being compressed when sent to the client.

Adding other MIME types to the .htaccess file is successful (e.g., adding text/html compresses the HTML assets).

This seems specific to how SVG data are handled. What else can I try or troubleshoot to get SVG compression working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If Apache doesn't know the mime type of the file (here image/svg+xml), you need to tell it specifically (not needed in most Apaches):

AddType image/svg+xml svg svgz

Now when Apache knows about the filetype, just add this to deflate it:

AddOutputFilterByType DEFLATE image/svg+xml

For more information see https://httpd.apache.org/docs/2.4/mod/mod_deflate.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...