# Flutter Web on Apache: SPA routing, caching, compression, and correct MIME types

<IfModule mod_rewrite.c>
  Options -MultiViews
  RewriteEngine On

  # Serve existing files/directories directly
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # Fallback all other requests to index.html (client-side routing)
  RewriteRule ^ index.html [L]
</IfModule>

# Correct MIME types (important for CanvasKit/WebAssembly and fonts)
AddType application/wasm wasm
AddType font/woff2 woff2
AddType application/javascript js
AddType text/css css

# Caching policy
<IfModule mod_headers.c>
  # Long cache for static assets (Flutter uses hashed filenames)
  <FilesMatch "\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|wasm)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  # Service configs/JSON can be cached briefly
  <FilesMatch "\.(json)$">
    Header set Cache-Control "public, max-age=3600"
  </FilesMatch>

  # Never cache service worker or manifest (avoid stale app shell)
  <FilesMatch "^flutter_service_worker\.js$">
    Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
  </FilesMatch>
  <FilesMatch "^manifest\.json$">
    Header set Cache-Control "no-cache, max-age=0"
  </FilesMatch>

  # Never cache the HTML entry point
  <FilesMatch "^index\.html$">
    Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
  </FilesMatch>

  # Minimal hardening
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Compression (enable if modules are available)
<IfModule mod_brotli.c>
  AddOutputFilterByType BROTLI_COMPRESS \
    text/html text/plain text/css application/javascript application/json application/wasm image/svg+xml
</IfModule>
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE \
    text/html text/plain text/css application/javascript application/json application/wasm image/svg+xml
</IfModule>
