next-forge
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesenext-forge
next-forge
next-forge is a production-grade Turborepo template for building Next.js SaaS applications. It provides a monorepo structure with multiple apps, shared packages, and integrations for authentication, database, payments, email, CMS, analytics, observability, security, and more.
next-forge 是一款用于构建 Next.js SaaS 应用的生产级 Turborepo 模板。它提供了包含多个应用、共享包的单体仓库结构,并且集成了身份认证、数据库、支付、邮件、CMS、数据分析、可观测性、安全等多种能力。
Quick Start
快速开始
Initialize a new project:
bash
npx next-forge@latest initThe CLI prompts for a project name and package manager (bun, npm, yarn, or pnpm). After installation:
- Set the in
DATABASE_URLpointing to a PostgreSQL database (Neon recommended).packages/database/.env - Run database migrations:
bun run migrate - Add any optional integration keys to the appropriate files.
.env.local - Start development:
bun run dev
All integrations besides the database are optional. Missing environment variables gracefully disable features rather than causing errors.
初始化新项目:
bash
npx next-forge@latest initCLI 会提示你输入项目名称和包管理器(bun、npm、yarn 或 pnpm)。安装完成后:
- 在 中设置
packages/database/.env,指向 PostgreSQL 数据库(推荐使用 Neon)。DATABASE_URL - 运行数据库迁移:
bun run migrate - 在对应的 文件中添加可选的集成密钥。
.env.local - 启动开发环境:
bun run dev
除数据库外的所有集成都是可选的。缺失环境变量时会优雅地禁用对应功能,不会抛出错误。
Architecture Overview
架构概览
The monorepo contains apps and packages. Apps are deployable applications. Packages are shared libraries imported as .
@repo/<package-name>Apps (in ):
/apps/| App | Port | Purpose |
|---|---|---|
| 3000 | Main authenticated SaaS application |
| 3001 | Marketing website with CMS and SEO |
| 3002 | Serverless API for webhooks, cron jobs |
| 3003 | React Email preview server |
| 3004 | Documentation site (Mintlify) |
| 6006 | Design system component workshop |
| 3005 | Prisma Studio for database editing |
Core Packages: , , , , , , , , , , , , , , , , , , , , .
authdatabasepaymentsemailcmsdesign-systemanalyticsobservabilitysecuritystorageseofeature-flagsinternationalizationwebhookscronnotificationscollaborationairate-limitnext-configtypescript-configFor detailed structure, see .
references/architecture.md单体仓库包含应用和包两部分。应用是可直接部署的程序,包是可被导入的共享库,导入格式为 。
@repo/<package-name>应用(位于 目录下):
/apps/| 应用 | 端口 | 用途 |
|---|---|---|
| 3000 | 需身份认证的主 SaaS 应用 |
| 3001 | 搭载 CMS 和 SEO 能力的营销官网 |
| 3002 | 用于 webhook、定时任务的无服务 API |
| 3003 | React Email 预览服务 |
| 3004 | 文档站点(Mintlify) |
| 6006 | 设计系统组件工作台 |
| 3005 | 用于数据库编辑的 Prisma Studio |
核心包:、、、、、、、、、、、、、、、、、、、、。
authdatabasepaymentsemailcmsdesign-systemanalyticsobservabilitysecuritystorageseofeature-flagsinternationalizationwebhookscronnotificationscollaborationairate-limitnext-configtypescript-config如需了解详细结构,请查看 。
references/architecture.mdKey Concepts
核心概念
Environment Variables
环境变量
Environment variable files live alongside apps and packages:
- — Main app keys (Clerk, Stripe, etc.)
apps/app/.env.local - — Marketing site keys
apps/web/.env.local - — API keys
apps/api/.env.local - —
packages/database/.env(required)DATABASE_URL - — BaseHub token
packages/cms/.env.local - — Languine project ID
packages/internationalization/.env.local
Each package has a file that validates environment variables with Zod via . Type safety is enforced at build time.
keys.ts@t3-oss/env-nextjs环境变量文件和对应应用、包放在同一目录下:
- — 主应用密钥(Clerk、Stripe 等)
apps/app/.env.local - — 营销站点密钥
apps/web/.env.local - — API 密钥
apps/api/.env.local - —
packages/database/.env(必填)DATABASE_URL - — BaseHub 令牌
packages/cms/.env.local - — Languine 项目 ID
packages/internationalization/.env.local
每个包都有一个 文件,通过 搭配 Zod 验证环境变量,构建时会强制校验类型安全。
keys.ts@t3-oss/env-nextjsInter-App URLs
应用间 URL
Local URLs are pre-configured:
NEXT_PUBLIC_APP_URL=http://localhost:3000NEXT_PUBLIC_WEB_URL=http://localhost:3001NEXT_PUBLIC_API_URL=http://localhost:3002NEXT_PUBLIC_DOCS_URL=http://localhost:3004
Update these to production domains when deploying (e.g., , ).
app.yourdomain.comwww.yourdomain.com本地 URL 已经预先配置完成:
NEXT_PUBLIC_APP_URL=http://localhost:3000NEXT_PUBLIC_WEB_URL=http://localhost:3001NEXT_PUBLIC_API_URL=http://localhost:3002NEXT_PUBLIC_DOCS_URL=http://localhost:3004
部署时请将这些地址更新为生产环境域名(例如 、)。
app.yourdomain.comwww.yourdomain.comServer Components First
优先使用服务端组件
page.tsxlayout.tsx'use client'page.tsxlayout.tsx'use client'Graceful Degradation
优雅降级
All integrations beyond the database are optional. Clients use optional chaining (e.g., , ). If the corresponding environment variable is not set, the feature is silently disabled.
stripe?.prices.list()resend?.emails.send()除数据库外的所有集成都是可选的。客户端会使用可选链调用(例如 、),如果对应的环境变量未设置,相关功能会被静默禁用。
stripe?.prices.list()resend?.emails.send()Common Tasks
常见任务
Running Development
运行开发环境
bash
bun run dev # All apps
bun dev --filter app # Single app (port 3000)
bun dev --filter web # Marketing site (port 3001)bash
bun run dev # 启动所有应用
bun dev --filter app # 仅启动单个应用(端口 3000)
bun dev --filter web # 启动营销站点(端口 3001)Database Migrations
数据库迁移
After changing :
packages/database/prisma/schema.prismabash
bun run migrateThis runs Prisma format, generate, and db push in sequence.
修改 后执行:
packages/database/prisma/schema.prismabash
bun run migrate该命令会依次执行 Prisma 格式化、生成类型定义和推送 schema 到数据库的操作。
Adding shadcn/ui Components
添加 shadcn/ui 组件
bash
npx shadcn@latest add [component] -c packages/design-systemUpdate existing components:
bash
bun run bump-uibash
npx shadcn@latest add [component] -c packages/design-system更新已有组件:
bash
bun run bump-uiAdding a New Package
添加新包
Create a new directory in with a using the naming convention. Add it as a dependency in consuming apps.
/packages/package.json@repo/<name>在 目录下新建文件夹,创建 时遵循 的命名规范,然后在需要使用该包的应用中将其添加为依赖即可。
/packages/package.json@repo/<name>Linting and Formatting
代码检查与格式化
bash
bun run lint # Check code style (Ultracite/Biome)
bun run format # Fix code stylebash
bun run lint # 检查代码风格(Ultracite/Biome)
bun run format # 自动修复代码风格问题Testing
测试
bash
bun run test # Run tests across monorepobash
bun run test # 运行整个单体仓库的测试用例Building
构建
bash
bun run build # Build all apps and packages
bun run analyze # Bundle analysisbash
bun run build # 构建所有应用和包
bun run analyze # 包体积分析Deployment
部署
Deploy to Vercel by creating separate projects for , , and — each pointing to its respective root directory under . Add environment variables per project or use Vercel Team Environment Variables.
appwebapi/apps/For detailed setup and customization instructions, see:
- — Installation, prerequisites, environment variables, database and Stripe CLI setup
references/setup.md - — Detailed documentation for every package
references/packages.md - — Swapping providers, extending features, deployment configuration
references/customization.md - — Full monorepo structure, Turborepo pipeline, scripts
references/architecture.md
如需部署到 Vercel,需要为 、 和 分别创建项目,每个项目指向 下对应的根目录。你可以为每个项目单独添加环境变量,也可以使用 Vercel 团队环境变量。
appwebapi/apps/如需了解详细的设置和自定义教程,请查看:
- — 安装、前置依赖、环境变量、数据库和 Stripe CLI 配置
references/setup.md - — 每个包的详细文档
references/packages.md - — 替换服务商、扩展功能、部署配置
references/customization.md - — 完整单体仓库结构、Turborepo 流水线、脚本说明
references/architecture.md