flags
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFeature Flags
功能标志
Use this skill when adding or changing framework feature flags in Next.js internals.
当在Next.js内部添加或更改框架功能标志时,请使用此skill。
Required Wiring
必要的配置流程
All flags need: (type) → (zod). If the flag is consumed in user-bundled code (client components, edge routes, template), also add it to for build-time injection. Runtime-only flags consumed exclusively in pre-compiled bundles can skip .
config-shared.tsconfig-schema.tsapp-page.tsdefine-env.tsdefine-env.ts所有标志都需要:(类型)→ (Zod)。如果该标志在用户打包的代码(客户端组件、边缘路由、模板)中使用,还需将其添加到以实现构建时注入。仅在预编译包中使用的纯运行时标志可以跳过。
config-shared.tsconfig-schema.tsapp-page.tsdefine-env.tsdefine-env.tsWhere the Flag Is Consumed
标志的使用场景
Client/bundled code only (e.g. in client components): is sufficient. Webpack/Turbopack replaces at the user's build time.
__NEXT_PPRdefine-env.tsprocess.env.XPre-compiled runtime bundles (e.g. code in ): The flag must also be set as a real var at runtime, because runs from pre-compiled bundles where doesn't reach. Two approaches:
app-render.tsxprocess.envapp-render.tsxdefine-env.ts- Runtime env var: Set in +
next-server.ts. Both code paths stay in one bundle. Simple but increases bundle size.export/worker.ts - Separate bundle variant: Add DefinePlugin entry in (scoped to
next-runtime.webpack-config.js), new taskfile tasks, updatebundleType === 'app'selector, and still set env var inmodule.compiled.js+next-server.tsfor bundle selection. Eliminates dead code but adds build complexity.export/worker.ts
For runtime flags, also add the field to the Pick type in .
NextConfigRuntimeconfig-shared.ts仅客户端/打包代码(例如客户端组件中的):只需配置即可。Webpack/Turbopack会在用户构建时替换。
__NEXT_PPRdefine-env.tsprocess.env.X预编译运行时包(例如中的代码):该标志还必须在运行时设置为真实的变量,因为从预编译包运行,无法作用于这些包。有两种方法:
app-render.tsxprocess.envapp-render.tsxdefine-env.ts- 运行时环境变量:在+
next-server.ts中设置。两个代码路径保留在同一个包中。实现简单但会增加包体积。export/worker.ts - 单独的包变体:在中添加DefinePlugin条目(限定为
next-runtime.webpack-config.js)、新的taskfile任务、更新bundleType === 'app'选择器,同时仍需在module.compiled.js+next-server.ts中设置环境变量以选择包。这种方式可以消除无用代码,但会增加构建复杂度。export/worker.ts
对于运行时标志,还需在的 Pick类型中添加对应字段。
config-shared.tsNextConfigRuntimeRuntime-Bundle Model
运行时包模型
- Runtime bundles are built by (rspack) via
next-runtime.webpack-config.jsbundle tasks.taskfile.js - Bundle selection occurs at runtime in based on
src/server/route-modules/app-page/module.compiled.jsvars.process.env - Variants: = up to 16 bundles per route type.
{turbo/webpack} × {experimental/stable/nodestreams/experimental-nodestreams} × {dev/prod} - affects user bundling, not pre-compiled runtime internals.
define-env.ts - checks in
process.env.Xare either replaced by DefinePlugin at runtime-bundle-build time, or read as actual env vars at server startup. They are NOT affected by the user's defines fromapp-render.tsx.define-env.ts - Gotcha: DefinePlugin entries in must be scoped to the correct
next-runtime.webpack-config.js(e.g.bundleTypeonly, notapp) to avoid replacing assignment targets inserver.next-server.ts
- 运行时包由(rspack)通过
next-runtime.webpack-config.js的打包任务构建。taskfile.js - 包选择在运行时由根据
src/server/route-modules/app-page/module.compiled.js变量决定。process.env - 变体:= 每种路由类型最多16个包。
{turbo/webpack} × {experimental/stable/nodestreams/experimental-nodestreams} × {dev/prod} - 影响用户打包,而非预编译的运行时内部代码。
define-env.ts - 中的
app-render.tsx检查要么在运行时包构建时被DefinePlugin替换,要么在服务器启动时读取实际环境变量。它们不受用户从process.env.X定义的内容影响。define-env.ts - 注意事项:中的DefinePlugin条目必须限定为正确的
next-runtime.webpack-config.js(例如仅bundleType,而非app),以避免替换server中的赋值目标。next-server.ts
Related Skills
相关技能
- - DCE-safe require patterns and edge constraints
$dce-edge - - entry-base boundaries and vendored React
$react-vendoring - - reproduction and verification workflow
$runtime-debug
- - 支持DCE的require模式和边缘约束
$dce-edge - - 基于入口的边界和 vendored React
$react-vendoring - - 复现与验证工作流
$runtime-debug