# Bonix App - Apache Configuration for Cache Control
#
# Strategy:
#   index.html / SW bootstrap  → always fresh (no-cache)
#   main.dart.js               → long cache, busted by ?v=<buildVersion> in index.html
#   other JS / CSS              → moderate cache with revalidation
#   images / fonts / wasm       → long immutable cache

<IfModule mod_headers.c>
    # index.html and manifest — always revalidate so deploys take effect immediately
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>

    # manifest.json needs freshness for PWA update detection
    <FilesMatch "^manifest\.json$">
        Header set Cache-Control "no-cache, must-revalidate, max-age=0"
    </FilesMatch>

    # SW bootstrap files — tiny, must be fresh so new builds are detected
    <FilesMatch "(flutter_service_worker|flutter_bootstrap|custom_sw|push_sw|push_helper)\.js$">
        Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>

    # main.dart.js — large bundle, safe to cache because index.html appends ?v=<hash>
    <FilesMatch "main\.dart\.js$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    # Other JS / CSS — short cache with revalidation
    <FilesMatch "\.(js|css)$">
        Header set Cache-Control "public, max-age=3600, must-revalidate"
    </FilesMatch>

    # Images, fonts, WASM — long immutable cache
    <FilesMatch "\.(jpg|jpeg|png|gif|svg|ico|woff|woff2|ttf|eot|webp|wasm)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    FileETag MTime Size
</IfModule>

# Enable GZIP compression for better performance
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml font/ttf font/otf application/octet-stream
</IfModule>

# Fallback to index.html for Flutter routing (SPA)
# Do NOT serve index.html for static assets - return 404 if missing.
# This prevents "unsupported MIME type text/html" when flutter_service_worker.js
# (or other .js) is requested but not found (e.g. not deployed).
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # If the request is for a real file or directory, do nothing
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    # Do not serve HTML for asset requests - browser expects JS/CSS/etc.
    RewriteCond %{REQUEST_URI} \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|json|wasm)(\?.*)?$ [NC]
    RewriteRule ^ - [L]
    # SPA fallback: everything else → index.html
    RewriteRule ^ index.html [L]
</IfModule>

