# Correct MIME type for the PWA manifest so browsers accept the install metadata
# (many hosts serve .webmanifest as text/plain, which Chrome rejects).
<IfModule mod_mime.c>
    AddType application/manifest+json .webmanifest
</IfModule>

# Long-lived caching for the immutable app icons (content-addressed by name).
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/x-icon "access plus 30 days"
</IfModule>

# The service worker must never be held in the HTTP cache, or a new release
# won't be picked up. `no-cache, max-age=0, must-revalidate` does exactly that:
# every fetch revalidates against the server. (`no-store` is not needed for
# that and is deliberately absent — browsers keep their own copy of a worker
# script and revalidate it on navigation regardless.)
<IfModule mod_headers.c>
    <Files "sw.js">
        Header set Cache-Control "no-cache, max-age=0, must-revalidate"
    </Files>
</IfModule>

# ── Performance: compress + cache static assets ──────────────────────────────
# The vendor assets are large (grapesjs ~1.1 MB, html2pdf ~0.9 MB, bootstrap ~0.23 MB) and were
# served uncompressed with no cache. gzip cuts text payloads ~70%. A month-long cache removes the
# re-download on every page — but NOT "immutable", because these paths aren't content-hashed, so a
# platform update still propagates (the browser revalidates via ETag on expiry).
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml \
        application/javascript application/x-javascript text/javascript \
        application/json application/xml image/svg+xml application/manifest+json \
        font/ttf font/otf application/vnd.ms-fontobject
</IfModule>
<IfModule mod_expires.c>
    ExpiresByType text/css "access plus 30 days"
    ExpiresByType application/javascript "access plus 30 days"
    ExpiresByType text/javascript "access plus 30 days"
    ExpiresByType image/svg+xml "access plus 30 days"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType image/webp "access plus 30 days"
</IfModule>
# mod_expires is missing on a fair number of shared hosts — and on a host
# without it the block above is silently skipped, so every asset went out with
# NO cache headers at all and a phone re-fetched half a megabyte of CSS on
# every page. mod_headers is far more commonly enabled, so the same policy is
# stated again through it. Where both exist the result is identical, and the
# second one simply restates what the first already said.
<IfModule !mod_expires.c>
  <IfModule mod_headers.c>
    <FilesMatch "\.(css|js|mjs|svg|webp|png|jpg|jpeg|gif|ico)$">
        Header setIfEmpty Cache-Control "public, max-age=2592000"
    </FilesMatch>
    <FilesMatch "\.(woff2?|ttf|otf|eot)$">
        Header setIfEmpty Cache-Control "public, max-age=31536000"
    </FilesMatch>
    # The worker decides when the app updates, so it is never cached (this
    # restates the rule above for hosts that skipped the mod_expires block).
    <Files "sw.js">
        Header set Cache-Control "no-cache, max-age=0, must-revalidate"
    </Files>
  </IfModule>
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect direct requests to index.php to the base URL
    RewriteCond %{THE_REQUEST} \s/index\.php [NC]
    RewriteRule ^index\.php$ ./ [R=302,L]

    # If the requested file or directory exists, serve it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Otherwise, route everything to index.php
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

