Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}\n" --output /dev/null                  152655                                                                                                                                                                                                                                   $ curl https://example.org/assets/svg/asset.svg --silent --write-out "%{size_download}\n" --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?

like image 798
Alex Reynolds Avatar asked Jan 25 '14 22:01

Alex Reynolds


People also ask

How do I enable compression in Apache?

Enable Compression for ApacheOpen your Apache virtual host file and enable gzip compression entries line by line. For example, to compress HTML, text, XML, CSS, and Javascript content types, add the following line to your virtual host or . htaccess file.

Are SVG files compressed?

SVG) file. It is compressed with gzip compression and contains data in XML format. SVGZ files support transparency, gradients, animations, and filters.

What compression type is SVG?

SVGs offer lossless compression — which means they're compressible to smaller file sizes at no cost to their definition, detail, or quality. PNGs also benefit from lossless compression of 5-20%, which can help make up for their large file size.


2 Answers

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

like image 55
Reeno Avatar answered Oct 09 '22 05:10

Reeno


I suspect this is due to the plus sign in the MIME type, which may need escaping in the AddOutputFilterByType directive. You can also try using the AddOutputFilter directive instead, to process all files with a certain extension:

AddOutputFilter DEFLATE svg 
like image 44
rmeakins Avatar answered Oct 09 '22 05:10

rmeakins