I am using the @font-face CSS property to load a special font for headings on a website, and I want Apache to send the font compressed to reduce loading time.
However, the "AddOutputFilterByType" declaration in Apache 2's deflate module only accepts mime types and, as AFAIK, neither opentype nor truetype have registered mime types.
So, how do I configure Apache to deflate/compress font.otf or font.ttf files?
Fonts in general should be compressed, but not always. TrueType fonts ( ttf ) and OpenType fonts ( otf ) should be compressed, but Web Open Font Format ( woff ) fonts should not be compressed (they're already compressed).
Both WOFF and WOFF 2.0 have built-in compression.
You can compress . woff font files with https://github.com/hn/woff-compress. It uses zlib's Z_BEST_COMPRESSION flag or Zopfli to losslessly shrink files down to a minimum. If you want to lossy reduce files even more, you can remove unneeded glyphs from the font as discussed in Way to reduce size of .
Came up with a solution:
Use Apache's AddType
declaration to add a custom mime type and then use that mime type in the AddOutputFilterByType
declaration.
For example, for opentype and truetype fonts:
Addtype font/opentype .otf
Addtype font/truetype .ttf
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css font/opentype font/truetype
Both these declarations require their appropriate modules to be active: mod_mime
and mod_deflate
. And for best practice, the AddType
declaration should be in the mime.conf file, and the AddOutputFilterByType
declaration should be in the deflate.conf file. See your particular distribution's help files for enabling and configuring Apache modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With