Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/tasks/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,28 @@ export const myTask = task({
});
```

#### Per-task middleware

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:

```ts
import { task, locals } from "@trigger.dev/sdk";

const myLocal = locals.create<string>("myLocal");

export const myTask = task({
id: "my-task",
middleware: async ({ payload, ctx, next }) => {
locals.set(myLocal, "some-value");
await next();
},
run: async (payload) => {
const value = locals.getOrThrow(myLocal);
// ...
},
});
```

### `onStartAttempt` function

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