<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- Gzip compression for JavaScript and CSS files -->
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />
    <httpCompression>
      <dynamicTypes>
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="text/css" enabled="true" />
        <add mimeType="application/json" enabled="true" />
      </dynamicTypes>
      <staticTypes>
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="text/css" enabled="true" />
        <add mimeType="application/json" enabled="true" />
      </staticTypes>
    </httpCompression>
    
    <!-- Caching rules for static assets -->
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
      
      <!-- MIME types for modern formats -->
      <remove fileExtension=".webp" />
      <mimeMap fileExtension=".webp" mimeType="image/webp" />
      <remove fileExtension=".avif" />
      <mimeMap fileExtension=".avif" mimeType="image/avif" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="font/woff2" />
    </staticContent>
    
    <!-- HTTP Headers for better caching -->
    <httpProtocol>
      <customHeaders>
        <!-- Remove default cache header to allow per-path configuration -->
        <remove name="Cache-Control" />
      </customHeaders>
    </httpProtocol>
    
    
    <!-- URL Rewrite rules for SPA and Caching -->
    <rewrite>
      <rules>
        <rule name="Static Assets" stopProcessing="true">
          <match url="(.*\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|pdf|webp|avif))$" />
          <action type="Rewrite" url="{R:1}" />
        </rule>
        <rule name="Next.js Static Files" stopProcessing="true">
          <match url="^_next/(.*)$" />
          <action type="Rewrite" url="_next/{R:1}" />
        </rule>
      </rules>
      
      <outboundRules>
        <!-- Cache fonts for 1 year -->
        <rule name="Cache Fonts">
          <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="\.(ttf|woff|woff2|eot|otf)$" />
          </conditions>
          <action type="Rewrite" value="public, max-age=31536000, immutable" />
        </rule>
        
        <!-- Cache images for 1 year -->
        <rule name="Cache Images">
          <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="\.(jpg|jpeg|png|gif|svg|webp|avif|ico)$" />
          </conditions>
          <action type="Rewrite" value="public, max-age=31536000, immutable" />
        </rule>
        
        <!-- Cache CSS/JS from _next for 1 year -->
        <rule name="Cache Next Static">
          <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/_next/static/" />
          </conditions>
          <action type="Rewrite" value="public, max-age=31536000, immutable" />
        </rule>
        
        <!-- Cache other static assets for 1 year -->
        <rule name="Cache Static Assets">
          <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="\.(css|js|pdf)$" />
          </conditions>
          <action type="Rewrite" value="public, max-age=31536000, immutable" />
        </rule>
      </outboundRules>
    </rewrite>
  </system.webServer>
</configuration>
