import type { NextConfig } from 'next'; const configuredBasePath = process.env.OPENMAIC_BASE_PATH?.trim().replace(/\/+$/, '') || ''; const basePath = configuredBasePath && configuredBasePath.startsWith('/') ? configuredBasePath : ''; const nextConfig: NextConfig = { // Keep `next dev` in its own build directory so its startup reset never // wipes a production `next build` output shared through `.next`. Start the // dev server with NEXT_DIST_DIR=.next-dev; production build/start keep the // default `.next`. distDir: process.env.NEXT_DIST_DIR || '.next', output: process.env.VERCEL ? undefined : 'standalone', ...(basePath ? { basePath } : {}), env: { NEXT_PUBLIC_OPENMAIC_BASE_PATH: basePath, }, transpilePackages: ['mathml2omml', 'pptxgenjs', '@openmaic/importer'], // These agent packages do a runtime `import(specifier)` with a computed // specifier (to lazily load node:fs/os/path without breaking browser/Vite // builds). webpack can't statically analyze that and bundling it throws // "Cannot find module as expression is too dynamic" at runtime on the server // (the "Edit with AI" Pro-mode path), which broke the #619 keep-alive e2e. // Mark them server-external so Next loads them natively and the dynamic // import resolves as a real Node call. serverExternalPackages: [ '@earendil-works/pi-ai', '@earendil-works/pi-agent-core', '@openmaic/generation', // instrumentation.ts imports the PostgreSQL-backed persistence scheduler. // Keep pg in Node at runtime; bundling it for webpack's non-Node graphs // tries to resolve conditional fs/path/stream imports and breaks builds. 'pg', ], experimental: { proxyClientMaxBodySize: '200mb', }, webpack(config, { nextRuntime, webpack }) { if (nextRuntime === 'edge') { // instrumentation.ts is compiled for both runtimes. Its guarded Node // branch owns the PostgreSQL asset collector; omitting that module from // the Edge graph keeps pg's fs/path/stream imports out of middleware // without weakening the Node server schedule. config.plugins.push( new webpack.IgnorePlugin({ resourceRegExp: /asset-collector-schedule$/ }), ); } return config; }, async headers() { const extraAncestors = process.env.ALLOWED_FRAME_ANCESTORS?.trim(); const frameAncestors = extraAncestors ? `'self' ${extraAncestors}` : "'self'"; return [ { source: '/(.*)', headers: [ // X-Frame-Options only supports SAMEORIGIN (no allow-list), // so we omit it when custom ancestors are configured. ...(!extraAncestors ? [{ key: 'X-Frame-Options', value: 'SAMEORIGIN' }] : []), { key: 'Content-Security-Policy', value: `frame-ancestors ${frameAncestors}`, }, ], }, ]; }, }; export default nextConfig;