import type { NextConfig } from 'next'; import { existsSync } from 'node:fs'; import path from 'node:path'; // Monorepo: a single `.env` may live at the repository root; Next only reads the app directory. for (const candidate of [path.resolve(process.cwd(), '../../.env'), path.resolve(process.cwd(), '.env')]) { if (existsSync(candidate)) { try { process.loadEnvFile(candidate); } catch { /* ignore malformed env */ } } } const API_URL = process.env.API_URL ?? 'http://127.0.0.1:8291'; const nextConfig: NextConfig = { reactStrictMode: true, poweredByHeader: false, allowedDevOrigins: ['127.0.0.1', 'localhost'], outputFileTracingRoot: path.resolve(__dirname, '../..'), experimental: { optimizePackageImports: ['lucide-react'], }, // Browser-side fetches go to the same origin; the FastAPI service is loopback-only (ARCHITECTURE §1, §9). async rewrites() { return [{ source: '/api/v1/:path*', destination: `${API_URL}/api/v1/:path*` }]; }, async headers() { const PUBLIC_CACHE = { key: 'Cache-Control', value: 'public, s-maxage=600, stale-while-revalidate=3600' }; const NO_STORE = { key: 'Cache-Control', value: 'private, no-store' }; return [ { source: '/(.*)', headers: [ { key: 'X-Content-Type-Options', value: 'nosniff' }, { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' }, { key: 'X-Frame-Options', value: 'SAMEORIGIN' }, { key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' }, ], }, { source: '/countries/:path*', headers: [PUBLIC_CACHE] }, { source: '/indicators/:path*', headers: [PUBLIC_CACHE] }, { source: '/rankings/:path*', headers: [PUBLIC_CACHE] }, { source: '/regions/:path*', headers: [PUBLIC_CACHE] }, { source: '/compare/:path*', headers: [PUBLIC_CACHE] }, { source: '/stories/:path*', headers: [PUBLIC_CACHE] }, { source: '/sources/:path*', headers: [PUBLIC_CACHE] }, { source: '/api/:path*', headers: [NO_STORE] }, { source: '/admin/:path*', headers: [NO_STORE] }, ]; }, }; export default nextConfig;