Skip to content

Commit c147564

Browse files
committed
Server actions guidance for triggering
1 parent 29b59a2 commit c147564

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

docs/v3/triggering.mdx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ You can trigger any task from your backend code, using either `trigger()` or `ba
2323
Trigger.dev API key to the world.
2424
</Note>
2525

26+
You can use Next.js Server Actions but [you need to be careful with bundling](#next-js-server-actions).
27+
2628
### Authentication
2729

2830
When you trigger a task from your backend code, you need to set the `TRIGGER_SECRET_KEY` environment variable. You can find the value on the API keys page in the Trigger.dev dashboard. [More info on API keys](/v3/apikeys).
@@ -188,3 +190,71 @@ export const batchParentTask = task({
188190
},
189191
});
190192
```
193+
194+
## Next.js Server Actions
195+
196+
Server Actions allow you to call your backend code without creating API routes. This is very useful for triggering tasks but you need to be careful you don't accidentally bundle the Trigger.dev SDK into your frontend code.
197+
198+
If you see an error like this then you've bundled `@trigger.dev/sdk` into your frontend code:
199+
200+
```bash
201+
Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme).
202+
Module build failed: UnhandledSchemeError: Reading from "node:process" is not handled by plugins (Unhandled scheme).
203+
Webpack supports "data:" and "file:" URIs by default.
204+
You may need an additional plugin to handle "node:" URIs.
205+
```
206+
207+
When you use server actions that use `@trigger.dev/sdk`:
208+
209+
- The file can't have any React components in it.
210+
- The file should have `"use server"` on the first line.
211+
212+
Here's an example of how to do it with a component that calls the server action and the actions file:
213+
214+
<CodeGroup>
215+
216+
```tsx app/page.tsx
217+
"use client";
218+
219+
import { create } from "@/app/actions";
220+
221+
export default function Home() {
222+
return (
223+
<main className="flex min-h-screen flex-col items-center justify-between p-24">
224+
<button
225+
onClick={async () => {
226+
const handle = await create();
227+
console.log(handle);
228+
}}
229+
>
230+
Create a thing
231+
</button>
232+
</main>
233+
);
234+
}
235+
```
236+
237+
```tsx app/actions.ts
238+
"use server";
239+
240+
import { createAvatar } from "@/trigger/create-avatar";
241+
242+
export async function create() {
243+
try {
244+
const handle = await createAvatar.trigger({
245+
payload: {
246+
userImage: "http://...",
247+
},
248+
});
249+
250+
return { handle };
251+
} catch (error) {
252+
console.error(error);
253+
return {
254+
error: "something went wrong",
255+
};
256+
}
257+
}
258+
```
259+
260+
</CodeGroup>

0 commit comments

Comments
 (0)