import fs from "node:fs"; import path from "node:path"; import type { NextConfig } from "next"; /** * Portal reverse-proxies vertical apps so /nav /book /community stay same-origin. * Override upstreams via env when deploying or local multi-process dev. */ const NAV_UPSTREAM = (process.env.NAV_UPSTREAM || "http://127.0.0.1:5173").replace( /\/$/, "", ); const BOOK_UPSTREAM = ( process.env.BOOK_UPSTREAM || "http://127.0.0.1:3001" ).replace(/\/$/, ""); const COMMUNITY_UPSTREAM = ( process.env.COMMUNITY_UPSTREAM || "http://127.0.0.1:3002" ).replace(/\/$/, ""); const hasStaticNav = fs.existsSync( path.join(process.cwd(), "public", "nav", "index.html"), ); const nextConfig: NextConfig = { async rewrites() { const beforeFiles = hasStaticNav ? [ { source: "/nav", destination: "/nav/index.html" }, { source: "/nav/", destination: "/nav/index.html" }, ] : []; const fallback = [ ...(hasStaticNav ? [] : [ { source: "/nav", destination: `${NAV_UPSTREAM}/nav/` }, { source: "/nav/:path*", destination: `${NAV_UPSTREAM}/nav/:path*`, }, ]), { source: "/book", destination: `${BOOK_UPSTREAM}/book` }, { source: "/book/:path*", destination: `${BOOK_UPSTREAM}/book/:path*` }, { source: "/community", destination: `${COMMUNITY_UPSTREAM}/community`, }, { source: "/community/:path*", destination: `${COMMUNITY_UPSTREAM}/community/:path*`, }, ]; return { beforeFiles, fallback }; }, }; export default nextConfig;