Skip to content

Commit f4b3528

Browse files
authored
docs: add a section on heavy operations in loaders (medusajs#12749)
1 parent 48810fa commit f4b3528

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

www/apps/book/app/learn/fundamentals/modules/loaders/page.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ Loaders are also executed when you run [migrations](../../data-models/write-migr
116116

117117
---
118118

119+
## Avoid Heavy Operations in Loaders
120+
121+
Since loaders are executed when the Medusa application starts, heavy operations will increase the startup time of the application.
122+
123+
So, avoid operations that take a long time to complete, such as fetching a large amount of data from an external API or database, in loaders.
124+
125+
### Alternative Solutions
126+
127+
Instead of performing heavy operations in loaders, consider one of the following solutions:
128+
129+
- Use a [scheduled job](../../scheduled-jobs/page.mdx) to perform the operation at specified intervals. This way, the operation is performed asynchronously and doesn't block the application startup.
130+
- [Emit custom events](../../events-and-subscribers/emit-event/page.mdx) in an [API route](../../api-routes/page.mdx), then handle the event in a [subscriber](../../events-and-subscribers/page.mdx) to perform the operation asynchronously. You can then send a request to the API route to trigger the operation when needed.
131+
132+
---
133+
119134
## Example: Register Custom MongoDB Connection
120135

121136
As mentioned in this chapter's introduction, loaders are most useful when you need to register a custom resource in the module's container to re-use it in other customizations in the module.

www/apps/book/generated/edit-dates.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const generatedEditDates = {
1818
"app/learn/fundamentals/events-and-subscribers/page.mdx": "2025-05-16T13:40:16.111Z",
1919
"app/learn/fundamentals/modules/container/page.mdx": "2025-05-21T15:07:12.059Z",
2020
"app/learn/fundamentals/workflows/execute-another-workflow/page.mdx": "2024-12-09T15:56:22.895Z",
21-
"app/learn/fundamentals/modules/loaders/page.mdx": "2025-05-21T15:15:35.271Z",
21+
"app/learn/fundamentals/modules/loaders/page.mdx": "2025-06-16T13:34:16.462Z",
2222
"app/learn/fundamentals/admin/widgets/page.mdx": "2024-12-09T16:43:24.260Z",
2323
"app/learn/fundamentals/data-models/page.mdx": "2025-03-18T07:55:56.252Z",
2424
"app/learn/fundamentals/modules/remote-link/page.mdx": "2024-09-30T08:43:53.127Z",

www/apps/book/public/llms-full.txt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ You define the middlewares using the `defineMiddlewares` function and export its
11361136

11371137
In the middleware object, you define three properties:
11381138

1139-
- `matcher`: a string or regular expression indicating the API route path to apply the middleware on. You pass the create brand's route `/admin/brand`.
1139+
- `matcher`: a string or regular expression indicating the API route path to apply the middleware on. You pass the create brand's route `/admin/brands`.
11401140
- `method`: The HTTP method to restrict the middleware to, which is `POST`.
11411141
- `middlewares`: An array of middlewares to apply on the route. You pass the `validateAndTransformBody` middleware, passing it the Zod schema you created earlier.
11421142

@@ -3397,12 +3397,12 @@ export function register() {
33973397

33983398
In the `instrumentation.ts` file, you export a `register` function that uses Medusa's `registerOtel` utility function. You also initialize an instance of the exporter, such as Zipkin, and pass it to the `registerOtel` function.
33993399

3400-
`registerOtel` accepts an object where you can pass any [NodeSDKConfiguration](https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk_node.NodeSDKConfiguration.html) property along with the following properties:
3400+
`registerOtel` accepts an object where you can pass any [NodeSDKConfiguration](https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk-node.NodeSDKConfiguration.html) property along with the following properties:
34013401

34023402
The `NodeSDKConfiguration` properties are accepted since Medusa v2.5.1.
34033403

34043404
- serviceName: (\`string\`) The name of the service traced.
3405-
- exporter: (\[SpanExporter]\(https://open-telemetry.github.io/opentelemetry-js/interfaces/\_opentelemetry\_sdk\_trace\_base.SpanExporter.html)) An instance of an exporter, such as Zipkin.
3405+
- exporter: (\[SpanExporter]\(https://open-telemetry.github.io/opentelemetry-js/interfaces/\_opentelemetry\_sdk-node.node.SpanExporter.html)) An instance of an exporter, such as Zipkin.
34063406
- instrument: (\`object\`) Options specifying what to trace.
34073407

34083408
- http: (\`boolean\`) Whether to trace HTTP requests.
@@ -14234,6 +14234,21 @@ Loaders are also executed when you run [migrations](https://docs.medusajs.com/le
1423414234

1423514235
***
1423614236

14237+
## Avoid Heavy Operations in Loaders
14238+
14239+
Since loaders are executed when the Medusa application starts, heavy operations will increase the startup time of the application.
14240+
14241+
So, avoid operations that take a long time to complete, such as fetching a large amount of data from an external API or database, in loaders.
14242+
14243+
### Alternative Solutions
14244+
14245+
Instead of performing heavy operations in loaders, consider one of the following solutions:
14246+
14247+
- Use a [scheduled job](https://docs.medusajs.com/learn/fundamentals/scheduled-jobs/index.html.md) to perform the operation at specified intervals. This way, the operation is performed asynchronously and doesn't block the application startup.
14248+
- [Emit custom events](https://docs.medusajs.com/learn/fundamentals/events-and-subscribers/emit-event/index.html.md) in an [API route](https://docs.medusajs.com/learn/fundamentals/api-routes/index.html.md), then handle the event in a [subscriber](https://docs.medusajs.com/learn/fundamentals/events-and-subscribers/index.html.md) to perform the operation asynchronously. You can then send a request to the API route to trigger the operation when needed.
14249+
14250+
***
14251+
1423714252
## Example: Register Custom MongoDB Connection
1423814253

1423914254
As mentioned in this chapter's introduction, loaders are most useful when you need to register a custom resource in the module's container to re-use it in other customizations in the module.

0 commit comments

Comments
 (0)