/** @type {import('next').NextConfig} */ const { buildSecurityHeaders, normalizeAssetPrefix } = require('../../config/nextSecurityHeaders') const toStoragePattern = (rawUrl) => { try { const u = new URL(rawUrl.replace(/\/api\/v1\/?$/, '')) return { protocol: u.protocol.replace(':', ''), hostname: u.hostname, ...(u.port ? { port: u.port } : {}), pathname: '/storage/**', } } catch { return null } } const storagePatterns = [ process.env.API_INTERNAL_URL ?? 'http://localhost:4000', process.env.API_URL, process.env.NEXT_PUBLIC_API_URL, ] .filter(Boolean) .map(toStoragePattern) .filter(Boolean) .filter((p, i, arr) => arr.findIndex((q) => q.hostname === p.hostname && q.port === p.port) === i) const DASHBOARD_BASE_PATH = '/dashboard' // In Docker dev the dashboard app runs on port 3001 while the marketplace proxy // is served from port 3000. When DASHBOARD_ASSET_PREFIX is set, Next should emit // absolute chunk URLs so /dashboard pages load their own JS/CSS from port 3001. const assetPrefix = normalizeAssetPrefix(process.env.DASHBOARD_ASSET_PREFIX, DASHBOARD_BASE_PATH) const securityHeaders = buildSecurityHeaders({ assetSources: [assetPrefix] }) const nextConfig = { basePath: DASHBOARD_BASE_PATH, ...(assetPrefix ? { assetPrefix } : {}), images: { remotePatterns: [ { protocol: 'https', hostname: 'res.cloudinary.com', }, ...storagePatterns, ], }, transpilePackages: ['@rentaldrivego/types'], async headers() { return [ { source: '/:path*', headers: securityHeaders, }, ] }, async rewrites() { const apiOrigin = (process.env.API_INTERNAL_URL ?? process.env.API_URL ?? 'http://localhost:4000').replace(/\/api\/v1\/?$/, '') return [ { source: '/api/:path*', destination: `${apiOrigin}/api/:path*`, }, { source: '/storage/:path*', destination: `${apiOrigin}/storage/:path*`, }, ] }, } module.exports = nextConfig