# Enable Brotli Compression for supported files, only if they exist
RewriteEngine On

# The WebCLAP plugin origin serves the host frame and its service worker scope only, not the studio
RewriteCond %{HTTP_HOST} ^(www\.)?opendaw-plugins\.studio$ [NC]
RewriteCond %{REQUEST_URI} !/(wclap-frame\.html|wclap-sw\.js)(\.br)?$ [NC]
RewriteCond %{REQUEST_URI} !/wclap/ [NC]
RewriteRule ^ - [F,L]

# Serve .br files if the client supports Brotli
RewriteCond %{HTTP:Accept-Encoding} br
RewriteCond %{REQUEST_FILENAME}.br -f
RewriteRule ^(.*)$ $1.br [QSA,L]

# Set Content-Encoding header only for actual Brotli-compressed files
<FilesMatch "\.(js|css|html|json|svg|xml|wasm)\.br$">
    Header set Content-Encoding br
</FilesMatch>

# WebAssembly streaming compilation requires application/wasm
<FilesMatch "\.wasm\.br$">
    ForceType application/wasm
</FilesMatch>

# --------------------------------------------------
# Cache Control Headers
# --------------------------------------------------

# Never cache index.html - ensures users get new versions on reload
<Files "index.html">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
</Files>

# Never cache build-info.json - used for update checks
<Files "build-info.json">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
</Files>

# Cache versioned files forever (they have UUID in filename)
<FilesMatch "\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.(js|css|ttf)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>

# Also cache brotli-compressed versioned files
<FilesMatch "\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.(js|css)\.br$">
    Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>

# --------------------------------------------------
# Search engine indexing
# --------------------------------------------------

# Live collaboration rooms (/join/) and project deep links (/open-bundle/) are private user
# content. This is an SPA, so they are served the very same index.html as every other route,
# and that page's meta robots tag says "index, follow" — a meta tag cannot distinguish paths,
# so this header is the only per-route lever.
#
# These two paths are deliberately NOT disallowed in robots.txt: a disallowed URL is never
# fetched, so the crawler would never see this noindex and could still list the bare URL if
# someone shares the link. Letting the crawler fetch it is what makes the noindex binding, and
# it leaks nothing — the response is the empty SPA shell, never any room or project data.
RewriteCond %{REQUEST_URI} ^/(join|open-bundle)(/|$) [NC]
RewriteRule ^ - [E=NOINDEX_ROUTE:1]

<IfModule mod_headers.c>
    # The SPA fallback below is an internal redirect, which re-exports env vars under a
    # REDIRECT_ prefix, so the rewritten request only matches the second line. Both spellings
    # are needed to cover the request whether or not it got rewritten.
    Header set X-Robots-Tag "noindex, nofollow" env=NOINDEX_ROUTE
    Header set X-Robots-Tag "noindex, nofollow" env=REDIRECT_NOINDEX_ROUTE
</IfModule>

# --------------------------------------------------

# SPA fallback - route all remaining non-file requests to the studio index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]