You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/apps/book/app/learn/fundamentals/modules/loaders/page.mdx
+15Lines changed: 15 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,6 +116,21 @@ Loaders are also executed when you run [migrations](../../data-models/write-migr
116
116
117
117
---
118
118
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
+
119
134
## Example: Register Custom MongoDB Connection
120
135
121
136
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.
Copy file name to clipboardExpand all lines: www/apps/book/public/llms-full.txt
+18-3Lines changed: 18 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1136,7 +1136,7 @@ You define the middlewares using the `defineMiddlewares` function and export its
1136
1136
1137
1137
In the middleware object, you define three properties:
1138
1138
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`.
1140
1140
- `method`: The HTTP method to restrict the middleware to, which is `POST`.
1141
1141
- `middlewares`: An array of middlewares to apply on the route. You pass the `validateAndTransformBody` middleware, passing it the Zod schema you created earlier.
1142
1142
@@ -3397,12 +3397,12 @@ export function register() {
3397
3397
3398
3398
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.
3399
3399
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:
3401
3401
3402
3402
The `NodeSDKConfiguration` properties are accepted since Medusa v2.5.1.
3403
3403
3404
3404
- 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.
3406
3406
- instrument: (\`object\`) Options specifying what to trace.
3407
3407
3408
3408
- http: (\`boolean\`) Whether to trace HTTP requests.
@@ -14234,6 +14234,21 @@ Loaders are also executed when you run [migrations](https://docs.medusajs.com/le
14234
14234
14235
14235
***
14236
14236
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
+
14237
14252
## Example: Register Custom MongoDB Connection
14238
14253
14239
14254
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