server {
  listen 80;
  server_name _;

  root /usr/share/nginx/html;
  index index.html;

  server_tokens off;
  access_log off;

  # Enable SharedArrayBuffer so ONNX Runtime can use multithreaded WASM when
  # WebGPU is unavailable or falls back to CPU execution.
  add_header Cross-Origin-Opener-Policy "same-origin" always;
  add_header Cross-Origin-Embedder-Policy "require-corp" always;

  gzip on;
  gzip_comp_level 6;
  gzip_min_length 1024;
  gzip_types
    text/plain
    text/css
    text/xml
    text/javascript
    application/javascript
    application/json
    application/xml
    image/svg+xml
    font/woff2;

  # Worker response headers are security-sensitive. Revalidate worker scripts
  # so a corrected COOP/COEP configuration is not hidden by a year-long cache.
  location ~* ^/assets/worker-[^/]+\.js$ {
    try_files $uri =404;
    expires -1;
    add_header Cache-Control "no-cache, max-age=0, must-revalidate";
    add_header Cross-Origin-Opener-Policy "same-origin" always;
    add_header Cross-Origin-Embedder-Policy "require-corp" always;
    add_header Cross-Origin-Resource-Policy "same-origin" always;
  }

  # nginx's bundled MIME map does not consistently include .mjs. ONNX Runtime
  # loads these files as JavaScript modules, which browsers reject when served
  # as application/octet-stream.
  location ~* ^/assets/.*\.mjs$ {
    try_files $uri =404;
    default_type application/javascript;
    expires 1y;
    add_header Cache-Control "public, immutable";
    add_header Cross-Origin-Opener-Policy "same-origin" always;
    add_header Cross-Origin-Embedder-Policy "require-corp" always;
    add_header Cross-Origin-Resource-Policy "same-origin" always;
  }

  location /assets/ {
    try_files $uri =404;
    expires 1y;
    add_header Cache-Control "public, immutable";
    add_header Cross-Origin-Opener-Policy "same-origin" always;
    add_header Cross-Origin-Embedder-Policy "require-corp" always;
    add_header Cross-Origin-Resource-Policy "same-origin" always;
  }

  location /imgly/ {
    try_files $uri =404;
    expires 1y;
    add_header Cache-Control "public, immutable";
    add_header Cross-Origin-Opener-Policy "same-origin" always;
    add_header Cross-Origin-Embedder-Policy "require-corp" always;
    add_header Cross-Origin-Resource-Policy "same-origin" always;
  }

  location = /favicon.svg {
    try_files $uri =404;
    expires 30d;
    add_header Cache-Control "public";
    add_header Cross-Origin-Opener-Policy "same-origin" always;
    add_header Cross-Origin-Embedder-Policy "require-corp" always;
    add_header Cross-Origin-Resource-Policy "same-origin" always;
  }

  location = /config.js {
    try_files $uri =404;
    add_header Cache-Control "no-store, no-cache, must-revalidate";
    add_header Cross-Origin-Opener-Policy "same-origin" always;
    add_header Cross-Origin-Embedder-Policy "require-corp" always;
    add_header Cross-Origin-Resource-Policy "same-origin" always;
    expires -1;
  }

  location / {
    try_files $uri $uri/ /index.html;
  }
}
