How to Use Feature Flags in prompts.config.ts to Enable or Disable Functionality in prompts.chat
Feature flags in prompts.chat are centrally managed in prompts.config.ts and can be dynamically overridden at runtime using PCHAT_FEATURE_* environment variables, allowing operators to toggle functionality without rebuilding the application.
The open-source f/prompts.chat repository implements a centralized configuration system that controls runtime capabilities through static configuration and environment-based overrides. All feature toggles are declared in prompts.config.ts and loaded at startup by src/lib/config/index.ts, making it possible to enable or disable specific functionality like private prompts, AI search, or comments without modifying source code or redeploying containers.
How Feature Flags Work in prompts.chat
The application follows a three-layer configuration pattern: definition, resolution, and consumption. First, flags are defined as boolean properties under the features object in prompts.config.ts. Second, getConfig() in src/lib/config/index.ts resolves the final values by merging the static config with environment variables. Finally, components and API routes consume these values to conditionally render UI or execute logic.
The configuration loader applies overrides using the envBool() helper, which checks for environment variables following the PCHAT_FEATURE_<NAME> naming convention. For example, the privatePrompts flag checks PCHAT_FEATURE_PRIVATE_PROMPTS as implemented in src/lib/config/index.ts (lines 49–58).
Configuring Static Feature Flags in prompts.config.ts
Static toggles are defined directly in the root configuration file. The default export uses defineConfig() to declare available features:
// prompts.config.ts
export default defineConfig({
// ... other config
features: {
privatePrompts: true,
changeRequests: true,
categories: true,
tags: true,
aiSearch: true,
aiGeneration: true,
mcp: true,
comments: true,
},
});
To permanently disable a feature for a specific deployment, set its value to false in this file. For example, disabling private prompts at line 52:
// prompts.config.ts (line 52)
privatePrompts: false,
Overriding Flags at Runtime with Environment Variables
For dynamic control without code changes, set PCHAT_FEATURE_* environment variables. These override the static config values every time the application boots. The variable name uses uppercase snake_case with the PCHAT_FEATURE_ prefix mapped to the camelCase feature name.
For example, to disable comments via environment configuration:
# .env or Docker environment
PCHAT_FEATURE_COMMENTS=false
The resolution logic in src/lib/config/index.ts handles this pattern:
// src/lib/config/index.ts – applying env overrides
privatePrompts: envBool('PCHAT_FEATURE_PRIVATE_PROMPTS', config.features.privatePrompts),
Conditionally Rendering UI Components
Client-side code reads the resolved configuration to gate visual elements. The codebase consistently uses the pattern config.features.<flag> !== false to check flag status.
In src/app/prompts/[id]/page.tsx (lines 704–708), the comment section only renders when the feature is enabled and the prompt is public:
{config.features.comments !== false && !prompt.isPrivate && (
<CommentSection … />
)}
Similarly, src/app/[username]/page.tsx (line 618) conditionally shows a privacy notice:
{config.features.privatePrompts && (
<PrivatePromptsNote count={privatePromptsCount} />
)}
Guarding Server-Side Logic and API Routes
Server components and API routes use identical checks to avoid unnecessary database queries or to return early for disabled features. In src/app/tags/[slug]/page.tsx (line 134), tag-related queries are skipped when the feature is off:
if (config.features.tags) {
// Execute tag queries only when enabled
}
For API endpoints, you can reject requests immediately when a feature is disabled:
// src/app/api/mcp/route.ts (hypothetical pattern)
if (config.features.mcp === false) {
return new Response('MCP disabled', { status: 404 });
}
Summary
- Centralized configuration: All feature flags are declared in
prompts.config.tsunder thefeaturesobject. - Environment overrides: Set
PCHAT_FEATURE_<NAME>variables to override static config at startup without rebuilding. - Consistent checking: Use
config.features.<flag> !== falsein UI components and simple boolean checks in server logic. - Performance benefits: Disabled features skip database queries and component rendering, reducing bandwidth and load.
- Key files: Configuration resolution happens in
src/lib/config/index.ts, while consumption occurs throughout the app in page components and API routes.
Frequently Asked Questions
What is the naming convention for feature flag environment variables?
Environment variables use the prefix PCHAT_FEATURE_ followed by the uppercase snake_case version of the feature name. For example, the aiSearch flag in prompts.config.ts is overridden by PCHAT_FEATURE_AI_SEARCH, while privatePrompts maps to PCHAT_FEATURE_PRIVATE_PROMPTS.
Do I need to rebuild the Docker image to change a feature flag?
No. Because getConfig() in src/lib/config/index.ts applies environment variable overrides at startup, you can change flags by restarting the container with new environment variables. The static values in prompts.config.ts serve only as defaults.
What happens if a feature flag is missing from the configuration?
The configuration loader uses the static value from prompts.config.ts as the fallback. If a feature is omitted from the features object entirely, checks against it will evaluate to undefined, which the codebase typically treats as falsy using strict equality checks (!== false).
Can I enable feature flags for specific users only?
The current implementation in f/prompts.chat uses global feature flags that apply to all users. Per-user feature toggles would require extending the configuration system to check user roles or subscription tiers against the resolved config, which is not implemented in the base repository.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →