import type { NextConfig } from 'next'; import { existsSync } from 'node:fs'; import path from 'node:path'; // Monorepo: the single `.env` lives 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 nextConfig: NextConfig = { reactStrictMode: true, poweredByHeader: false, agentRules: false, // Dev only: Next blocks client assets/HMR for origins other than the printed host (localhost). // Allow 127.0.0.1 so curl/Playwright smoke tests hydrate in `next dev` as well. allowedDevOrigins: ['127.0.0.1', 'localhost'], // Workspace packages are consumed as TypeScript sources. transpilePackages: ['@cancerindex/shared', '@cancerindex/database', '@cancerindex/ontology', '@cancerindex/ranking'], serverExternalPackages: ['postgres', 'pino'], // Public API: /api/v1/* is proxied to the Fastify service (apps/api) so one public host serves web + API. async rewrites() { const api = process.env.CI_API_URL ?? `http://127.0.0.1:${process.env.API_PORT ?? 8251}`; return [{ source: '/api/v1/:path*', destination: `${api}/v1/:path*` }]; }, experimental: { optimizePackageImports: ['lucide-react'], // Workspace packages use NodeNext-style `./file.js` imports that resolve to .ts sources. // Turbopack does not rewrite these outside the app root, so the app is built with webpack // (`next dev/build --webpack`) where extensionAlias handles the mapping. extensionAlias: { '.js': ['.ts', '.tsx', '.js'], '.mjs': ['.mts', '.mjs'] }, }, outputFileTracingRoot: path.resolve(__dirname, '../..'), async headers() { // Static-ish public routes: let ngrok/CDN/browser caches share a rendered page for 10 min and serve // stale for an hour while revalidating. Never for /admin, /search, /api (user- or query-specific). const cacheable = ['/cancer/:path*', '/gene/:path*', '/variant/:path*', '/drug/:path*', '/trial/:path*', '/publication/:path*', '/source/:path*', '/rankings', '/rankings/:path*']; 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=()' }, ], }, ...cacheable.map((source) => ({ source, headers: [PUBLIC_CACHE] })), { source: '/admin/:path*', headers: [NO_STORE] }, { source: '/search', headers: [NO_STORE] }, { source: '/api/:path*', headers: [NO_STORE] }, ]; }, }; export default nextConfig;