# ============================================================
# NewzQuest PWA - Hostinger Apache Configuration v5.0
# Optimised for PWABuilder 100% audit score
# ============================================================

Options -Indexes

# ─── HTTPS Redirect ──────────────────────────────────────
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# ─── MIME Types for PWA ──────────────────────────────────
<IfModule mod_mime.c>
    AddType application/manifest+json           .webmanifest
    AddType application/manifest+json           .json
    AddType application/javascript              .js
    AddType text/javascript                     .js
    AddType image/png                           .png
    AddType image/jpeg                          .jpg .jpeg
    AddType image/svg+xml                       .svg
    AddType image/x-icon                        .ico
    AddType font/woff2                          .woff2
    AddType font/woff                           .woff
</IfModule>

# ─── Security & PWA Headers ──────────────────────────────
<IfModule mod_headers.c>

    # Service Worker & Manifest: never cache, always fresh
    <FilesMatch "(sw\.js|service-worker\.js|manifest\.json|manifest\.webmanifest)$">
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
        Header set Service-Worker-Allowed "/"
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, OPTIONS"
        Header set Access-Control-Allow-Headers "Content-Type"
        Header set X-Content-Type-Options "nosniff"
    </FilesMatch>

    # Widget template and data files
    <FilesMatch "(widget-template\.json|widget-data\.json)$">
        Header set Cache-Control "max-age=300, public"
        Header set Access-Control-Allow-Origin "*"
        Header set Content-Type "application/json"
    </FilesMatch>

    # App shell: short cache
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=3600, no-cache, must-revalidate"
        Header set X-Frame-Options "SAMEORIGIN"
        Header set X-Content-Type-Options "nosniff"
        Header set Referrer-Policy "strict-origin-when-cross-origin"
    </FilesMatch>

    # CSS & JS: medium cache
    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "max-age=86400, public"
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>

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

</IfModule>

# ─── Gzip Compression ────────────────────────────────────
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml
    AddOutputFilterByType DEFLATE text/css application/javascript
    AddOutputFilterByType DEFLATE application/json application/manifest+json
    AddOutputFilterByType DEFLATE image/svg+xml font/woff2
</IfModule>

# ─── Default Document ────────────────────────────────────
DirectoryIndex index.html
