This page documents the Next.js build configuration and the middleware layer responsible for request routing, security, and authentication guards in LobeHub.
LobeHub uses a centralized configuration factory via defineConfig() to manage build variations across different environments (Vercel, Docker, Standalone).
defineConfig() ImplementationThe configuration logic is encapsulated in src/libs/next/config/define-config.ts. It dynamically adjusts settings based on environment variables like DOCKER, VERCEL_ENV, and NEXT_BUILD_STANDALONE.
| Feature | Description | Implementation Source |
|---|---|---|
| Output Mode | Switches to standalone if DOCKER=true or NEXT_BUILD_STANDALONE=1. | src/libs/next/config/define-config.ts28-31 |
| Asset Prefix | Configurable via NEXT_PUBLIC_ASSET_PREFIX for CDN offloading. | src/libs/next/config/define-config.ts59-65 |
| Compression | Enabled in production environments. | src/libs/next/config/define-config.ts70 |
| SWC Minification | Disabled for server-side code to preserve constructor names required by OIDC providers. | src/libs/next/config/define-config.ts80-84 |
LobeHub supports three primary deployment modes, each with specific tracing and exclusion rules to optimize bundle size:
musl binaries (Vercel uses Amazon Linux/glibc, not Alpine/musl).output: 'standalone'.public/**/* and .next/static/**/* in the tracing output.@napi-rs/canvas via dockerCanvasTracingIncludes which are otherwise missed by static analysis.The configuration handles specific requirements for AI and UI libraries:
dockerCanvasTracingIncludes for Docker builds to ensure node-canvas functionality in containerized environments. src/libs/next/config/define-config.ts50-53optimizePackageImports is used for heavy libraries like @lobehub/ui, @lobehub/icons, and emoji-mart to speed up compilation. src/libs/next/config/define-config.ts71-79Security is enforced through HTTP headers defined in the headers() async function:
ENABLED_CSP=1, it injects X-Frame-Options: DENY and Content-Security-Policy: frame-ancestors 'none';. src/libs/next/config/define-config.ts96-107/icons/, /images/, /videos/, and /favicon.ico. src/libs/next/config/define-config.ts114-203vercel.json. vercel.json3-16Sources:
LobeHub uses a Next.js Middleware (src/proxy.ts) to handle dynamic routing, locale detection, and platform-specific rewrites.
The middleware detects the user's device type and locale to rewrite requests to the appropriate internal path.
Title: Middleware Routing Logic
Sources:
This diagram maps the middleware implementation to specific code entities and route matchers.
Title: Middleware Entity Map
Sources:
/api, /trpc, or /webapi bypass the middleware to minimize latency on high-traffic backend endpoints. src/libs/next/proxy/define-config.ts52-62hl query param, the LOBE_LOCALE_COOKIE, or the accept-language header, and persists it via persistLocaleCookie. src/libs/next/proxy/define-config.ts70-78ua-parser-js to identify mobile devices. Certain paths like /share and /verify are forced to the desktop bundle for responsive consistency. src/libs/next/proxy/define-config.ts81-97MIDDLEWARE_REWRITE_THROUGH_LOCAL is enabled, the middleware rewrites requests to 127.0.0.1:[PORT] to resolve networking issues in certain Docker environments. src/libs/next/proxy/define-config.ts111-122Sources:
Refresh this wiki