Skip to content

Commit 0c4e8ac

Browse files
committed
Updated the overview and nav
1 parent d17fe9d commit 0c4e8ac

File tree

2 files changed

+11
-200
lines changed

2 files changed

+11
-200
lines changed

docs/docs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@
122122
"group": "React hooks (frontend)",
123123
"pages": [
124124
"realtime/react-hooks/overview",
125+
"realtime/react-hooks/triggering",
125126
"realtime/react-hooks/realtime",
126127
"realtime/react-hooks/metadata",
127-
"realtime/react-hooks/streams",
128-
"realtime/react-hooks/triggering"
128+
"realtime/react-hooks/streams"
129129
]
130130
},
131131
{

docs/realtime/react-hooks/overview.mdx

Lines changed: 9 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ yarn install @trigger.dev/react-hooks
3030

3131
## Authentication
3232

33-
All hooks accept an optional last argument `options` that accepts an `accessToken` param, which should be a valid Public Access Token. Learn more about [generating tokens in our authentication guide](/realtime/auth).
33+
All hooks require authentication with a Public Access Token. Pass the token via the `accessToken` option:
3434

3535
```tsx
3636
import { useRealtimeRun } from "@trigger.dev/react-hooks";
@@ -43,204 +43,24 @@ export function MyComponent({
4343
publicAccessToken: string;
4444
}) {
4545
const { run, error } = useRealtimeRun(runId, {
46-
accessToken: publicAccessToken, // This is required
46+
accessToken: publicAccessToken,
4747
baseURL: "https://your-trigger-dev-instance.com", // optional, only needed if you are self-hosting Trigger.dev
4848
});
4949

5050
// ...
5151
}
5252
```
5353

54-
### TriggerAuthContext
54+
Learn more about [generating and managing tokens in our authentication guide](/realtime/auth).
5555

56-
Alternatively, you can use our `TriggerAuthContext` provider to avoid passing the token to every hook:
57-
58-
```tsx
59-
import { TriggerAuthContext } from "@trigger.dev/react-hooks";
60-
61-
export function SetupTrigger({ publicAccessToken }: { publicAccessToken: string }) {
62-
return (
63-
<TriggerAuthContext.Provider value={{ accessToken: publicAccessToken }}>
64-
<MyComponent />
65-
</TriggerAuthContext.Provider>
66-
);
67-
}
68-
```
69-
70-
Now children components can use the hooks to interact with the Trigger.dev API. If you are self-hosting Trigger.dev, you can provide the `baseURL` to the `TriggerAuthContext` provider.
71-
72-
```tsx
73-
import { TriggerAuthContext } from "@trigger.dev/react-hooks";
74-
75-
export function SetupTrigger({ publicAccessToken }: { publicAccessToken: string }) {
76-
return (
77-
<TriggerAuthContext.Provider
78-
value={{
79-
accessToken: publicAccessToken,
80-
baseURL: "https://your-trigger-dev-instance.com",
81-
}}
82-
>
83-
<MyComponent />
84-
</TriggerAuthContext.Provider>
85-
);
86-
}
87-
```
88-
89-
### Next.js and client components
90-
91-
If you are using Next.js with the App Router, you have to make sure the component that uses the `TriggerAuthContext` is a client component. So for example, the following code will not work:
92-
93-
```tsx app/page.tsx
94-
import { TriggerAuthContext } from "@trigger.dev/react-hooks";
95-
96-
export default function Page() {
97-
return (
98-
<TriggerAuthContext.Provider value={{ accessToken: "your-access-token" }}>
99-
<MyComponent />
100-
</TriggerAuthContext.Provider>
101-
);
102-
}
103-
```
104-
105-
That's because `Page` is a server component and the `TriggerAuthContext.Provider` uses client-only react code. To fix this, wrap the `TriggerAuthContext.Provider` in a client component:
106-
107-
```ts components/TriggerProvider.tsx
108-
"use client";
109-
110-
import { TriggerAuthContext } from "@trigger.dev/react-hooks";
111-
112-
export function TriggerProvider({
113-
accessToken,
114-
children,
115-
}: {
116-
accessToken: string;
117-
children: React.ReactNode;
118-
}) {
119-
return (
120-
<TriggerAuthContext.Provider
121-
value={{
122-
accessToken,
123-
}}
124-
>
125-
{children}
126-
</TriggerAuthContext.Provider>
127-
);
128-
}
129-
```
130-
131-
### Passing the token to the frontend
132-
133-
Techniques for passing the token to the frontend vary depending on your setup. Here are a few ways to do it for different setups:
134-
135-
#### Next.js App Router
136-
137-
If you are using Next.js with the App Router and you are triggering a task from a server action, you can use cookies to store and pass the token to the frontend.
138-
139-
```tsx actions/trigger.ts
140-
"use server";
141-
142-
import { tasks } from "@trigger.dev/sdk/v3";
143-
import type { exampleTask } from "@/trigger/example";
144-
import { redirect } from "next/navigation";
145-
import { cookies } from "next/headers";
146-
147-
export async function startRun() {
148-
const handle = await tasks.trigger<typeof exampleTask>("example", { foo: "bar" });
149-
150-
// Set the auto-generated publicAccessToken in a cookie
151-
cookies().set("publicAccessToken", handle.publicAccessToken); // ✅ this token only has access to read this run
152-
153-
redirect(`/runs/${handle.id}`);
154-
}
155-
```
156-
157-
Then in the `/runs/[id].tsx` page, you can read the token from the cookie and pass it to the `TriggerProvider`.
158-
159-
```tsx pages/runs/[id].tsx
160-
import { TriggerProvider } from "@/components/TriggerProvider";
161-
162-
export default function RunPage({ params }: { params: { id: string } }) {
163-
const publicAccessToken = cookies().get("publicAccessToken");
164-
165-
return (
166-
<TriggerProvider accessToken={publicAccessToken}>
167-
<RunDetails id={params.id} />
168-
</TriggerProvider>
169-
);
170-
}
171-
```
172-
173-
Instead of a cookie, you could also use a query parameter to pass the token to the frontend:
174-
175-
```tsx actions/trigger.ts
176-
import { tasks } from "@trigger.dev/sdk/v3";
177-
import type { exampleTask } from "@/trigger/example";
178-
import { redirect } from "next/navigation";
179-
import { cookies } from "next/headers";
180-
181-
export async function startRun() {
182-
const handle = await tasks.trigger<typeof exampleTask>("example", { foo: "bar" });
183-
184-
redirect(`/runs/${handle.id}?publicAccessToken=${handle.publicAccessToken}`);
185-
}
186-
```
187-
188-
And then in the `/runs/[id].tsx` page:
189-
190-
```tsx pages/runs/[id].tsx
191-
import { TriggerProvider } from "@/components/TriggerProvider";
192-
193-
export default function RunPage({
194-
params,
195-
searchParams,
196-
}: {
197-
params: { id: string };
198-
searchParams: { publicAccessToken: string };
199-
}) {
200-
return (
201-
<TriggerProvider accessToken={searchParams.publicAccessToken}>
202-
<RunDetails id={params.id} />
203-
</TriggerProvider>
204-
);
205-
}
206-
```
207-
208-
Another alternative would be to use a server-side rendered page to fetch the token and pass it to the frontend:
209-
210-
<CodeGroup>
211-
212-
```tsx pages/runs/[id].tsx
213-
import { TriggerProvider } from "@/components/TriggerProvider";
214-
import { generatePublicAccessToken } from "@/trigger/auth";
215-
216-
export default async function RunPage({ params }: { params: { id: string } }) {
217-
// This will be executed on the server only
218-
const publicAccessToken = await generatePublicAccessToken(params.id);
219-
220-
return (
221-
<TriggerProvider accessToken={publicAccessToken}>
222-
<RunDetails id={params.id} />
223-
</TriggerProvider>
224-
);
225-
}
226-
```
56+
## Available hooks
22757

228-
```tsx trigger/auth.ts
229-
import { auth } from "@trigger.dev/sdk/v3";
230-
231-
export async function generatePublicAccessToken(runId: string) {
232-
return auth.createPublicToken({
233-
scopes: {
234-
read: {
235-
runs: [runId],
236-
},
237-
},
238-
expirationTime: "1h",
239-
});
240-
}
241-
```
58+
We provide several categories of hooks:
24259

243-
</CodeGroup>
60+
- **[Triggering hooks](/realtime/react-hooks/triggering)** - Trigger tasks from your frontend application
61+
- **[Realtime hooks](/realtime/react-hooks/realtime)** - Subscribe to runs, batches, and streams using real-time updates
62+
- **[Metadata hooks](/realtime/react-hooks/metadata)** - Subscribe to run metadata updates in React components
63+
- **[SWR hooks](#swr-hooks)** - Fetch data once and cache it using SWR
24464

24565
## SWR vs Realtime hooks
24666

@@ -252,15 +72,6 @@ We offer two "styles" of hooks: SWR and Realtime. The SWR hooks use the [swr](ht
25272
we recommend using the Realtime hooks for most use-cases.
25373
</Note>
25474

255-
## Available hooks
256-
257-
We provide several categories of hooks:
258-
259-
- **[Realtime hooks](/realtime/react-hooks/realtime)** - Subscribe to runs, batches, and streams using real-time updates
260-
- **[Metadata hooks](/realtime/react-hooks/metadata)** - Subscribe to run metadata updates in React components
261-
- **[Triggering hooks](/realtime/react-hooks/triggering)** - Trigger tasks from your frontend application
262-
- **[SWR hooks](#swr-hooks)** - Fetch data once and cache it using SWR
263-
26475
## SWR Hooks
26576

26677
### useRun

0 commit comments

Comments
 (0)