# ----------------------------------------
# Compression Settings (Improves load speed)
# - Content compression
# - Browser optimization
# - MIME type handling
# ----------------------------------------

# Force compression for mangled headers
<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>

# Enable Brotli Compression (Primary)
<IfModule mod_brotli.c>
    # Text formats
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript text/calendar text/cache-manifest
    AddOutputFilterByType BROTLI_COMPRESS text/markdown text/x-component text/x-cross-domain-policy

    # Application formats
    AddOutputFilterByType BROTLI_COMPRESS application/javascript application/x-javascript application/json application/ld+json application/manifest+json
    AddOutputFilterByType BROTLI_COMPRESS application/xml application/rss+xml application/atom+xml application/schema+json
    AddOutputFilterByType BROTLI_COMPRESS application/x-web-app-manifest+json application/x-httpd-php
    AddOutputFilterByType BROTLI_COMPRESS application/wasm

    # Font formats
    AddOutputFilterByType BROTLI_COMPRESS font/ttf font/otf font/woff font/woff2 font/eot font/collection
    AddOutputFilterByType BROTLI_COMPRESS application/vnd.ms-fontobject application/x-font-ttf application/x-font-opentype application/x-font-woff

    # Image formats
    AddOutputFilterByType BROTLI_COMPRESS image/svg+xml image/x-icon

    # Prevent double compression for already compressed files
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|avif|mp4|webm|ogg|mp3|wav|m4a|gz|br|zip|7z|rar|bz2|flac|vtt)$ no-gzip dont-vary
</IfModule>

# Enable GZIP Compression (Fallback)
<IfModule mod_deflate.c>
    # Text formats
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/calendar text/cache-manifest
    AddOutputFilterByType DEFLATE text/markdown text/x-component text/x-cross-domain-policy

    # Application formats
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript application/json application/ld+json application/manifest+json
    AddOutputFilterByType DEFLATE application/xml application/rss+xml application/atom+xml application/schema+json
    AddOutputFilterByType DEFLATE application/x-web-app-manifest+json application/x-httpd-php
    AddOutputFilterByType DEFLATE application/wasm

    # Font formats
    AddOutputFilterByType DEFLATE font/ttf font/otf font/woff font/woff2 font/eot font/collection
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject application/x-font-ttf application/x-font-opentype application/x-font-woff

    # Image formats
    AddOutputFilterByType DEFLATE image/svg+xml image/x-icon

    # Prevent double compression for already compressed files
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|avif|mp4|webm|ogg|mp3|wav|m4a|gz|br|zip|7z|rar|bz2|flac|vtt)$ no-gzip dont-vary

    # Legacy browser support
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

# Set Vary header only for compressible content
<IfModule mod_headers.c>
    SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
    Header append Vary Accept-Encoding env=HAVE_Accept-Encoding
</IfModule>