Raw
1 export const metadata = {
2 title: `${pageNumber} Feature Flags`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you'll learn what feature flags are, what the available feature flags in Medusa are, and how to toggle them.
8
9 ## What are Feature Flags?
10
11 Feature flags allow you to ship new features that are still under development and testing, but not ready for production or wide use yet.
12
13 Medusa uses feature flags to ship new versions even when some features are not fully ready. This approach allows our team to continuously deliver updates and improvements while stabilizing new features.
14
15 ---
16
17 ## Available Feature Flags in Medusa
18
19 For a list of features hidden behind a feature flag in Medusa, check out the [feature-flags](https://github.com/medusajs/medusa/tree/develop/packages/medusa/src/feature-flags) directory in the `@medusajs/medusa` package.
20
21 ---
22
23 ## Toggle Feature Flags
24
25 There are multiple ways to enable or disable feature flags in Medusa:
26
27 - In the `medusa-config.ts` file;
28 - Or using environment variables.
29
30 ### 1. Using the `medusa-config.ts` file
31
32 The Medusa configurations in `medusa-config.ts` accept a `featureFlags` configuration to toggle feature flags. Its value is an object whose key is the feature flag key (defined in the feature flag's file), and the value is a boolean indicating whether the feature is enabled.
33
34 For example, to enable the [Index Module](../../fundamentals/query/index-module/page.mdx)'s feature flag in `medusa-config.ts`:
35
36 ```ts title="medusa-config.ts"
37 module.exports = defineConfig({
38 // ...
39 featureFlags: {
40 "index_engine": true,
41 },
42 })
43 ```
44
45 Make sure to [run migrations](../../fundamentals/data-models/write-migration/page.mdx#run-the-migration) after enabling a feature flag, in case it requires database changes.
46
47 Before disabling a feature flag, make sure to [roll back the migrations](../../fundamentals/data-models/write-migration/page.mdx#rollback-the-migration) that depend on it.
48
49 ### 2. Using Environment Variables
50
51 The feature flags can also be enabled or disabled using environment variables. This is useful to control the feature flag based on the environment.
52
53 To toggle a feature flag using an environment variable, set an environment variable with its environment variable name (defined in the feature flag's file) and set its value to `true` or `false`.
54
55 For example, to enable the [Index Module](../../fundamentals/query/index-module/page.mdx)'s feature flag using an environment variable:
56
57 ```shell
58 MEDUSA_FF_INDEX_ENGINE=true
59 ```
60
61 Make sure to [run migrations](../../fundamentals/data-models/write-migration/page.mdx#run-the-migration) after enabling a feature flag, in case it requires database changes.
62
63 Before disabling a feature flag, make sure to [roll back the migrations](../../fundamentals/data-models/write-migration/page.mdx#rollback-the-migration) that depend on it.
64
65 ---
66
67 ## Check Feature Flag Status
68
69 During development, you can check whether a feature flag is enabled. This is useful to add customizations that only apply when a specific feature is enabled.
70
71 To build backend customizations around feature flags, you can either:
72
73 - [Conditionally run code blocks](#conditionally-run-code-blocks) with the `FeatureFlag` utility;
74 - Or [conditionally load files](#conditionally-load-files) with the `defineFileConfig` utility.
75
76 For client customizations, you can use the [Feature Flags API Route](#feature-flags-api-route).
77
78 ### Conditionally Run Code Blocks
79
80 The `FeatureFlag` utility allows you to check whether a specific feature flag is enabled in your backend customizations, including scheduled jobs, subscribers, and workflow steps.
81
82 For example, to enable access to a route only if a feature flag is enabled, you can use the `FeatureFlag` utility in a [middleware](../../fundamentals/api-routes/middlewares/page.mdx):
83
84 export const middlewareHighlights = [
85 ["11", "isFeatureEnabled", "Check if the Index Module flag is enabled."]
86 ]
87
88 ```ts title="src/api/middlewares.ts" highlights={middlewareHighlights}
89 import { defineMiddlewares } from "@medusajs/framework/http"
90 import { FeatureFlag } from "@medusajs/framework/utils"
91
92 export default defineMiddlewares({
93 routes: [
94 {
95 matcher: "/custom",
96 method: ["GET"],
97 middlewares: [
98 async (req, res, next) => {
99 if (!FeatureFlag.isFeatureEnabled("index_engine")) {
100 return res.sendStatus(404)
101 }
102 next()
103 },
104 ],
105 },
106 ],
107 })
108 ```
109
110 The `FeatureFlag.isFeatureEnabled` method accepts the feature flag key as a parameter and returns a boolean indicating whether the feature is enabled or not.
111
112 In the above example, you return a `404` response if a `GET` request is sent to the `/custom` route and the `index_engine` feature flag is disabled.
113
114 ### Conditionally Load Files
115
116 You can also combine the `FeatureFlag` utility with the `defineFileConfig` utility that allows you to fully enable or disable loading a file based on a condition.
117
118 For example, instead of adding a middleware, you can use the `defineFileConfig` utility to conditionally load an API route file only if a feature flag is enabled:
119
120 export const apiRouteHighlights = [
121 ["13", "defineFileConfig", "Define logic to conditionally load the API route file"],
122 ["14", "isFeatureEnabled", "Only load the file if the feature flag is enabled"]
123 ]
124
125 ```ts title="src/api/routes/custom.ts" highlights={apiRouteHighlights}
126 import { MedusaRequest, MedusaResponse } from "@medusajs/framework"
127 import { defineFileConfig, FeatureFlag } from "@medusajs/framework/utils"
128
129 export async function GET(
130 req: MedusaRequest,
131 res: MedusaResponse
132 ): Promise<void> {
133 res.json({
134 message: "Hello World",
135 })
136 }
137
138 defineFileConfig({
139 isDisabled: () => !FeatureFlag.isFeatureEnabled("index_engine"),
140 })
141 ```
142
143 The `defineFileConfig` function accepts an object with an `isDisabled` property. Its value is a function that returns a boolean indicating whether the file should be disabled.
144
145 In the above example, the `GET` API route at `/custom` will only be available if the `index_engine` feature flag is enabled.
146
147 <Note title="Middleware vs defineFileConfig">
148
149 While both approaches can be used to disable API routes, a middleware is useful to disable specific HTTP methods at a route. For example, if a route file has `GET` and `POST` route handlers, the `defineFileConfig` approach will disable both `GET` and `POST` methods if the feature flag is disabled. If you need to disable only the `POST` route, you should use a middleware instead.
150
151 </Note>
152
153 ### Feature Flags API Route
154
155 For client customizations, you can use the [List Feature Flags API Route](!api!/admin/feature-flags/list-feature-flags) to check the status of feature flags.
156
157 For example, you can show an [admin widget](../../fundamentals/admin/widgets/page.mdx) only if a feature flag is enabled:
158
159 export const widgetHighlights = [
160 ["6", "featureFlags", "Retrieve feature flag statuses from the backend."],
161 ["13", "", "Hide widget if feature flag is disabled"]
162 ]
163
164 ```tsx title="src/admin/widgets/product-details.tsx" highlights={widgetHighlights}
165 import { defineWidgetConfig } from "@medusajs/admin-sdk"
166 import { sdk } from "../lib/sdk"
167 import { useQuery } from "@tanstack/react-query"
168
169 const ProductWidget = () => {
170 const { data: featureFlags } = useQuery({
171 queryKey: ["featureFlags"],
172 queryFn: () => sdk.client.fetch<{
173 feature_flags: Record<string, boolean>
174 }>(`/admin/feature-flags`),
175 })
176
177 if (!featureFlags?.feature_flags.index_engine) {
178 return null
179 }
180
181 return (
182 <div>
183 <h2>Product Widget</h2>
184 <p>Index engine feature is enabled</p>
185 </div>
186 )
187 }
188
189 export const config = defineWidgetConfig({
190 zone: "product.details",
191 })
192
193 export default ProductWidget
194 ```
195
196 In the above example, the Product Widget will only be displayed if the `index_engine` feature flag is enabled. It retrieves the feature flag statuses from the `/admin/feature-flags` API route.