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: docs/tasks/overview.mdx
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -247,6 +247,28 @@ export const myTask = task({
247
247
});
248
248
```
249
249
250
+
#### Per-task middleware
251
+
252
+
You can also define middleware per task by passing the `middleware` option in the task definition. This runs after global middleware and before the `run` function. Use it when only specific tasks need certain locals or setup:
253
+
254
+
```ts
255
+
import { task, locals } from"@trigger.dev/sdk";
256
+
257
+
const myLocal =locals.create<string>("myLocal");
258
+
259
+
exportconst myTask =task({
260
+
id: "my-task",
261
+
middleware: async ({ payload, ctx, next }) => {
262
+
locals.set(myLocal, "some-value");
263
+
awaitnext();
264
+
},
265
+
run: async (payload) => {
266
+
const value =locals.getOrThrow(myLocal);
267
+
// ...
268
+
},
269
+
});
270
+
```
271
+
250
272
### `onStartAttempt` function
251
273
252
274
<Info>The `onStartAttempt` function was introduced in v4.1.0</Info>
0 commit comments