import { withFontCopy } from '@lumina/island/tools/font-copy-plugin'; import { withIconSprite } from '@lumina/island/tools/icon-sprite-plugin'; import { SERVICE_WORKER_HEADERS, SERVICE_WORKER_URL, writeServiceWorker, } from '@lumina/libs/pwa/plugin'; import type { NextConfig } from 'next'; import path from 'node:path'; import { API_PROXY_PATH, API_PROXY_TIMEOUT_MS } from './src/constants'; import { apiInternalUrl } from './src/env/env.config'; // Fonts and the icon sprite keep fixed names; a changed font gets a new name, and the sprite a new // ?v= (0011, 0012). Next serves public/ with max-age=0, so without this every visit asks for them again. const immutable = [ { key: 'Cache-Control', value: 'public, max-age=31536000, immutable' }, ]; const nextConfig: NextConfig = { // The `/api` proxy's wait for the gateway (0018); uploads outlast Next.js's 30-second default. experimental: { proxyTimeout: API_PROXY_TIMEOUT_MS }, // Workspace packages export TypeScript source, so Next.js compiles them. transpilePackages: [ '@lumina/market', '@lumina/integrations', '@lumina/island', '@lumina/libs', '@lumina/tools', ], // `/@{username}` is the public channel URL. A folder named `@…` is a // parallel-route slot in the App Router, so the routes live under // `channels/[username]` and the public URL is rewritten onto them. // // The browser calls the API on this site's origin (`/api/v1/…`, `/api/files/…`), and the API // calls are proxied to the gateway, so there is no CORS and the session cookies are this site's // (0018). async rewrites() { return [ { source: `${API_PROXY_PATH}/:path*`, destination: `${apiInternalUrl()}/:path*`, }, { source: '/@:username/:path*', destination: '/channels/:username/:path*', }, ]; }, async headers() { // Only Next calls `headers()`, on `next dev` and `next build`, so the worker is written here, as // island's plugins write theirs. Next runs from the app's folder, as every Nx target does. writeServiceWorker({ publicDir: path.join(process.cwd(), 'public') }); return [ { source: '/fonts/:path*', headers: immutable }, { source: '/icons/:path*', headers: immutable }, // Revalidated on every update check, so a new worker reaches visitors at their next // navigation; never immutable. /sw.js is never renamed or removed (pwa lib). { source: SERVICE_WORKER_URL, headers: Object.entries(SERVICE_WORKER_HEADERS).map(([key, value]) => ({ key, value, })), }, ]; }, // One canonical URL per channel: send direct `/channels/…` hits to `/@…`. async redirects() { return [ { source: '/channels/:username/:path*', destination: '/@:username/:path*', permanent: true, }, ]; }, }; // On next dev and next build, island's plugins write the fonts and icon sprite into public/ // under their fixed names, with no hash (0012). export default withIconSprite(withFontCopy(nextConfig));