Skip to content

Commit 1b4db79

Browse files
Chigalamatt-aitken
andauthored
Feat/astro support (#466)
* updated the @trigger.dev/astro package to support typescript * set up a example astro project * docs: manual setup of Trigger.dev in an Astro project * doc: updated the manual setup docs for astro * updated the webapp onboarding for astro * cleaned up the example astro project * removed next from dependency 😅 * updated the readme * moved trigger.ts file and job folder to src directory * created a .env.example file * updated tsconfig * updated to use aliases * updated package.json * updated the astro onboarding page * updated the manual installation guide for astro * updated to use alias * added astro as a dev dependency * Remove extra comma I left in * Changed Object.create() to a Record, so we have some type safety * Added the localhost URL to the API in the Astro example * Added the TRIGGER_API_URL to the trigger client in the example * Added the CLI dev dependency to the Astro example * Update the running instructions to be closer to Remix's * Create friendly-carpets-collect.md * Fix bad package name for Astro --------- Co-authored-by: Matt Aitken <[email protected]>
1 parent e1e6049 commit 1b4db79

File tree

26 files changed

+870
-55
lines changed

26 files changed

+870
-55
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/astro": patch
3+
---
4+
5+
The first release of Astro support

apps/webapp/app/components/frameworks/FrameworkSelector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function FrameworkSelector() {
6060
<FrameworkLink to={projectSetupRedwoodPath(organization, project)}>
6161
<RedwoodLogo className="w-44" />
6262
</FrameworkLink>
63-
<FrameworkLink to={projectSetupAstroPath(organization, project)}>
63+
<FrameworkLink to={projectSetupAstroPath(organization, project)} supported>
6464
<AstroLogo className="w-32" />
6565
</FrameworkLink>
6666
<FrameworkLink to={projectSetupNuxtPath(organization, project)}>
Lines changed: 108 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,119 @@
11
import { AstroLogo } from "~/assets/logos/AstroLogo";
2-
import { FrameworkComingSoon } from "~/components/frameworks/FrameworkComingSoon";
2+
import { ChatBubbleLeftRightIcon, Squares2X2Icon } from "@heroicons/react/20/solid";
3+
import invariant from "tiny-invariant";
4+
import { Feedback } from "~/components/Feedback";
5+
import { PageGradient } from "~/components/PageGradient";
6+
import { InitCommand, RunDevCommand, TriggerDevStep } from "~/components/SetupCommands";
7+
import { StepContentContainer } from "~/components/StepContentContainer";
8+
import { InlineCode } from "~/components/code/InlineCode";
39
import { BreadcrumbLink } from "~/components/navigation/NavBar";
10+
import { Button, LinkButton } from "~/components/primitives/Buttons";
11+
import {
12+
ClientTabs,
13+
ClientTabsContent,
14+
ClientTabsList,
15+
ClientTabsTrigger,
16+
} from "~/components/primitives/ClientTabs";
17+
import { ClipboardField } from "~/components/primitives/ClipboardField";
18+
import { Header1 } from "~/components/primitives/Headers";
19+
import { NamedIcon } from "~/components/primitives/NamedIcon";
20+
import { Paragraph } from "~/components/primitives/Paragraph";
21+
import { StepNumber } from "~/components/primitives/StepNumber";
22+
import { useAppOrigin } from "~/hooks/useAppOrigin";
23+
import { useProjectSetupComplete } from "~/hooks/useProjectSetupComplete";
24+
import { useDevEnvironment } from "~/hooks/useEnvironments";
25+
import { useOrganization } from "~/hooks/useOrganizations";
26+
import { useProject } from "~/hooks/useProject";
427
import { Handle } from "~/utils/handle";
5-
import { trimTrailingSlash } from "~/utils/pathBuilder";
28+
import { projectSetupPath, trimTrailingSlash } from "~/utils/pathBuilder";
29+
import { Callout } from "~/components/primitives/Callout";
30+
import { Badge } from "~/components/primitives/Badge";
631

732
export const handle: Handle = {
833
breadcrumb: (match) => <BreadcrumbLink to={trimTrailingSlash(match.pathname)} title="Astro" />,
934
};
1035

11-
export default function Page() {
36+
export default function SetUpAstro() {
37+
const organization = useOrganization();
38+
const project = useProject();
39+
useProjectSetupComplete();
40+
const devEnvironment = useDevEnvironment();
41+
invariant(devEnvironment, "Dev environment must be defined");
1242
return (
13-
<FrameworkComingSoon
14-
frameworkName="Astro"
15-
githubIssueUrl="https://github.com/triggerdotdev/trigger.dev/issues/452"
16-
githubIssueNumber={452}
17-
>
18-
<AstroLogo className="w-56" />
19-
</FrameworkComingSoon>
43+
<PageGradient>
44+
<div className="mx-auto max-w-3xl">
45+
<div className="flex items-center justify-between">
46+
<Header1 spacing className="text-bright">
47+
Get setup in 5 minutes
48+
</Header1>
49+
<div className="flex items-center gap-2">
50+
<LinkButton
51+
to={projectSetupPath(organization, project)}
52+
variant="tertiary/small"
53+
LeadingIcon={Squares2X2Icon}
54+
>
55+
Choose a different framework
56+
</LinkButton>
57+
<Feedback
58+
button={
59+
<Button variant="tertiary/small" LeadingIcon={ChatBubbleLeftRightIcon}>
60+
I'm stuck!
61+
</Button>
62+
}
63+
defaultValue="help"
64+
/>
65+
</div>
66+
</div>
67+
<div>
68+
<Callout
69+
variant={"info"}
70+
to="https://github.com/triggerdotdev/trigger.dev/discussions/430"
71+
className="mb-8"
72+
>
73+
Trigger.dev has full support for serverless. We will be adding support for long-running
74+
servers soon.
75+
</Callout>
76+
<div>
77+
<StepNumber
78+
stepNumber="1"
79+
title="Follow the steps from the Astro manual installation guide"
80+
/>
81+
<StepContentContainer className="flex flex-col gap-2">
82+
<Paragraph className="mt-2">Copy your server API Key to your clipboard:</Paragraph>
83+
<div className="mb-2 flex w-full items-center justify-between">
84+
<ClipboardField
85+
secure
86+
className="w-fit"
87+
value={devEnvironment.apiKey}
88+
variant={"secondary/medium"}
89+
icon={<Badge variant="outline">Server</Badge>}
90+
/>
91+
</div>
92+
<Paragraph>Now follow this guide:</Paragraph>
93+
<LinkButton
94+
to="https://trigger.dev/docs/documentation/guides/manual/astro"
95+
variant="primary/medium"
96+
TrailingIcon="external-link"
97+
>
98+
Manual installation guide
99+
</LinkButton>
100+
<div className="flex items-start justify-start gap-2"></div>
101+
</StepContentContainer>
102+
<StepNumber stepNumber="2" title="Run your Astro app" />
103+
<StepContentContainer>
104+
<RunDevCommand />
105+
</StepContentContainer>
106+
<StepNumber stepNumber="3" title="Run the CLI 'dev' command" />
107+
<StepContentContainer>
108+
<TriggerDevStep />
109+
</StepContentContainer>
110+
<StepNumber stepNumber="6" title="Wait for Jobs" displaySpinner />
111+
<StepContentContainer>
112+
<Paragraph>This page will automatically refresh.</Paragraph>
113+
</StepContentContainer>
114+
</div>
115+
</div>
116+
</div>
117+
</PageGradient>
20118
);
21119
}

docs/_snippets/manual-setup-astro.mdx

Lines changed: 248 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,248 @@
1-
We're in the process of building support for the Astro framework. You can follow along with progress or contribute via [this GitHub issue](https://github.com/triggerdotdev/trigger.dev/issues).
1+
## Install Required Packages
2+
3+
To begin, install the necessary packages in your Astro project directory. You can choose one of the following package managers:
4+
5+
<CodeGroup>
6+
7+
```bash npm
8+
npm i @trigger.dev/sdk @trigger.dev/astro
9+
```
10+
11+
```bash pnpm
12+
pnpm install @trigger.dev/sdk @trigger.dev/astro
13+
```
14+
15+
```bash yarn
16+
yarn add @trigger.dev/sdk @trigger.dev/astro
17+
```
18+
19+
</CodeGroup>
20+
21+
## Obtaining the Development API Key
22+
23+
To locate your development API key, login to the [Trigger.dev
24+
dashboard](https://cloud.trigger.dev) and select the Project you want to
25+
connect to. Then click on the Environments & API Keys tab in the left menu.
26+
You can copy your development API Key from the field at the top of this page.
27+
(Your development key will start with `tr_dev_`).
28+
29+
## Adding Environment Variables
30+
31+
Create a `.env` file at the root of your project and include your Trigger API key and URL like this:
32+
33+
```bash
34+
TRIGGER_API_KEY=ENTER_YOUR_DEVELOPMENT_API_KEY_HERE
35+
TRIGGER_API_URL=https://cloud.trigger.dev
36+
```
37+
38+
Replace `ENTER_YOUR_DEVELOPMENT_API_KEY_HERE` with the actual API key obtained from the previous step.
39+
40+
## Configuring the Trigger Client
41+
42+
To set up the Trigger Client for your project, follow these steps:
43+
44+
1. **Create Configuration File:**
45+
46+
In your project directory, create a configuration file named `trigger.ts` or `trigger.js`, depending on whether your project uses TypeScript (`.ts`) or JavaScript (`.js`).
47+
48+
2. **Choose Directory:**
49+
50+
Depending on your project structure, choose the appropriate directory for the configuration file. If your project uses a `src` directory, create the file within it or Otherwise, create it directly in the project root.
51+
52+
3. **Add Configuration Code:**
53+
54+
Open the configuration file you created and add the following code:
55+
56+
```typescript src/trigger.(ts/js)
57+
// trigger.ts (for TypeScript) or trigger.js (for JavaScript)
58+
59+
import { TriggerClient } from "@trigger.dev/sdk";
60+
61+
export const client = new TriggerClient({
62+
id: "my-app",
63+
apiKey: process.env.TRIGGER_API_KEY,
64+
apiUrl: process.env.TRIGGER_API_URL,
65+
});
66+
```
67+
68+
Replace **"my-app"** with an appropriate identifier for your project. The **apiKey** and **apiUrl** are obtained from the environment variables you set earlier.
69+
70+
4. **File Location:**
71+
72+
- You can save the file within the **src** directory or in the project rooot.
73+
74+
**Example Directory Structure with src:**
75+
76+
```
77+
project-root/
78+
├── src/
79+
├── trigger.ts
80+
├── other files...
81+
```
82+
83+
**Example Directory Structure without src:**
84+
85+
```
86+
project-root/
87+
├── trigger.ts
88+
├── other files...
89+
```
90+
91+
By following these steps, you'll configure the Trigger Client to work with your project, regardless of whether you have a separate **src** directory and whether you're using TypeScript or JavaScript files.
92+
93+
## update the astro.config file to enable ssr
94+
95+
- You need to enable ssr to use API endpoints that would be in the `pages/api` folder
96+
97+
```typescript astro.config.mjs
98+
import { defineConfig } from "astro/config";
99+
100+
export default defineConfig({
101+
output: "server",
102+
});
103+
```
104+
105+
## Creating the API Route
106+
107+
To establish an API route for interacting with Trigger.dev, follow these steps based on your project's file type and structure
108+
109+
1. Create a new file named `trigger.(ts/js)` within the `pages/api/` directory.
110+
2. Add the following code to `trigger.(ts/js)`:
111+
112+
```typescript api/trigger.ts/js
113+
import { createAstroRoute } from "@trigger.dev/astro";
114+
import { client } from "@/trigger";
115+
116+
//import your jobs
117+
import "@/jobs";
118+
119+
export const { POST } = createAstroRoute(client);
120+
```
121+
122+
## Creating the Example Job
123+
124+
1. Create a folder named `Jobs` alongside your `pages` directory
125+
2. Inside the `Jobs` folder, add two files named `example.(ts/js)` and `index.(ts/js)`.
126+
127+
<CodeGroup>
128+
129+
```typescript example.(ts/js)
130+
import { eventTrigger } from "@trigger.dev/sdk";
131+
import { client } from "@/trigger";
132+
133+
// your first job
134+
client.defineJob({
135+
id: "example-job",
136+
name: "Example Job",
137+
version: "0.0.1",
138+
trigger: eventTrigger({
139+
name: "example.event",
140+
}),
141+
run: async (payload, io, ctx) => {
142+
await io.logger.info("Hello world!", { payload });
143+
144+
return {
145+
message: "Hello world!",
146+
};
147+
},
148+
});
149+
```
150+
151+
```typescript index.ts/index.(ts/js)
152+
// import all your job files here
153+
154+
export * from "./examples";
155+
```
156+
157+
</CodeGroup>
158+
159+
## Additonal Job Definitions
160+
161+
You can define more job definitions by creating additional files in the `Jobs` folder and exporting them in `index` file.
162+
163+
For example, in `index.(ts/js)`, you can export other job files like this:
164+
165+
```typescript
166+
// import all your job files here
167+
168+
export * from "./examples";
169+
export * from "./other-job-file";
170+
```
171+
172+
## Adding Configuration to `package.json`
173+
174+
Inside the `package.json` file, add the following configuration under the root object:
175+
176+
```json
177+
"trigger.dev": {
178+
"endpointId": "my-app"
179+
}
180+
```
181+
182+
Your `package.json` file might look something like this:
183+
184+
```json
185+
{
186+
"name": "my-app",
187+
"version": "1.0.0",
188+
"dependencies": {
189+
// ... other dependencies
190+
},
191+
"trigger.dev": {
192+
"endpointId": "my-app"
193+
}
194+
}
195+
```
196+
197+
Replace **"my-app"** with the appropriate identifier you used during the step for creating the Trigger Client.
198+
199+
## Running
200+
201+
### Run your Astro app
202+
203+
Run your Astro app locally, like you normally would. For example:
204+
205+
<CodeGroup>
206+
207+
```bash npm
208+
npm run dev
209+
```
210+
211+
```bash pnpm
212+
pnpm run dev
213+
```
214+
215+
```bash yarn
216+
yarn run dev
217+
```
218+
219+
</CodeGroup>
220+
221+
### Run the CLI 'dev' command
222+
223+
In a **_separate terminal window or tab_** run:
224+
225+
<CodeGroup>
226+
227+
```bash npm
228+
npx @trigger.dev/cli@latest dev
229+
```
230+
231+
```bash pnpm
232+
pnpm dlx @trigger.dev/cli@latest dev
233+
```
234+
235+
```bash yarn
236+
yarn dlx @trigger.dev/cli@latest dev
237+
```
238+
239+
</CodeGroup>
240+
<br />
241+
<Note>
242+
You can optionally pass the port if you're not running on 3000 by adding
243+
`--port 4321` to the end
244+
</Note>
245+
<Note>
246+
You can optionally pass the hostname if you're not running on localhost by adding
247+
`--hostname <host>`. Example, in case your Astro app is running on 0.0.0.0: `--hostname 0.0.0.0`.
248+
</Note>

0 commit comments

Comments
 (0)