Skip to content

Commit 894109d

Browse files
committed
docs: add per-task middleware section to tasks overview
1 parent 436f20e commit 894109d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/tasks/overview.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,28 @@ export const myTask = task({
247247
});
248248
```
249249

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+
export const myTask = task({
260+
id: "my-task",
261+
middleware: async ({ payload, ctx, next }) => {
262+
locals.set(myLocal, "some-value");
263+
await next();
264+
},
265+
run: async (payload) => {
266+
const value = locals.getOrThrow(myLocal);
267+
// ...
268+
},
269+
});
270+
```
271+
250272
### `onStartAttempt` function
251273

252274
<Info>The `onStartAttempt` function was introduced in v4.1.0</Info>

0 commit comments

Comments
 (0)