Skip to content

Commit db0c7df

Browse files
committed
Merge remote-tracking branch 'origin/main' into pr/1777
2 parents 2aa3ccd + 21c0dc6 commit db0c7df

File tree

15 files changed

+722
-24
lines changed

15 files changed

+722
-24
lines changed

.changeset/itchy-frogs-care.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trigger.dev": patch
3+
---
4+
5+
Log images sizes for self-hosted deploys

.changeset/witty-donkeys-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/build": patch
3+
---
4+
5+
Add playwright extension

docs/config/extensions/overview.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Trigger.dev provides a set of built-in extensions that you can use to customize
5050
| :-------------------------------------------------------------------- | :----------------------------------------------------------------------------- |
5151
| [prismaExtension](/config/extensions/prismaExtension) | Using prisma in your Trigger.dev tasks |
5252
| [pythonExtension](/config/extensions/pythonExtension) | Execute Python scripts in your project |
53+
| [playwright](/config/extensions/playwright) | Use Playwright in your Trigger.dev tasks |
5354
| [puppeteer](/config/extensions/puppeteer) | Use Puppeteer in your Trigger.dev tasks |
5455
| [ffmpeg](/config/extensions/ffmpeg) | Use FFmpeg in your Trigger.dev tasks |
5556
| [aptGet](/config/extensions/aptGet) | Install system packages in your build image |

docs/config/extensions/playwright.mdx

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
title: "Playwright"
3+
sidebarTitle: "playwright"
4+
description: "Use the playwright build extension to use Playwright with Trigger.dev"
5+
---
6+
7+
If you are using [Playwright](https://playwright.dev/), you should use the Playwright build extension.
8+
9+
- Automatically installs Playwright and required browser dependencies
10+
- Allows you to specify which browsers to install (chromium, firefox, webkit)
11+
- Supports headless or non-headless mode
12+
- Lets you specify the Playwright version, or auto-detects it
13+
- Installs only the dependencies needed for the selected browsers to optimize build time and image size
14+
15+
<Note>
16+
This extension only affects the build and deploy process, not the `dev` command.
17+
</Note>
18+
19+
You can use it for a simple Playwright setup like this:
20+
21+
```ts
22+
import { defineConfig } from "@trigger.dev/sdk/v3";
23+
import { playwright } from "@trigger.dev/build/extensions/playwright";
24+
25+
export default defineConfig({
26+
project: "<project ref>",
27+
// Your other config settings...
28+
build: {
29+
extensions: [
30+
playwright(),
31+
],
32+
},
33+
});
34+
```
35+
36+
### Options
37+
38+
- `browsers`: Array of browsers to install. Valid values: `"chromium"`, `"firefox"`, `"webkit"`. Default: `["chromium"]`.
39+
- `headless`: Run browsers in headless mode. Default: `true`. If set to `false`, a virtual display (Xvfb) will be set up automatically.
40+
- `version`: Playwright version to install. If not provided, the version will be auto-detected from your dependencies (recommended).
41+
42+
<Warning>
43+
Using a different version in your app than specified here will break things. We recommend not setting this option to automatically detect the version.
44+
</Warning>
45+
46+
### Custom browsers and version
47+
48+
```ts
49+
import { defineConfig } from "@trigger.dev/sdk/v3";
50+
import { playwright } from "@trigger.dev/build/extensions/playwright";
51+
52+
export default defineConfig({
53+
project: "<project ref>",
54+
build: {
55+
extensions: [
56+
playwright({
57+
browsers: ["chromium", "webkit"], // optional, will use ["chromium"] if not provided
58+
version: "1.43.1", // optional, will automatically detect the version if not provided
59+
}),
60+
],
61+
},
62+
});
63+
```
64+
65+
### Headless mode
66+
67+
By default, browsers are run in headless mode. If you need to run browsers with a UI (for example, for debugging), set `headless: false`. This will automatically set up a virtual display using Xvfb.
68+
69+
```ts
70+
import { defineConfig } from "@trigger.dev/sdk/v3";
71+
import { playwright } from "@trigger.dev/build/extensions/playwright";
72+
73+
export default defineConfig({
74+
project: "<project ref>",
75+
build: {
76+
extensions: [
77+
playwright({
78+
headless: false,
79+
}),
80+
],
81+
},
82+
});
83+
```
84+
85+
### Environment variables
86+
87+
The extension sets the following environment variables during the build:
88+
89+
- `PLAYWRIGHT_BROWSERS_PATH`: Set to `/ms-playwright` so Playwright finds the installed browsers
90+
- `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD`: Set to `1` to skip browser download at runtime
91+
- `PLAYWRIGHT_SKIP_BROWSER_VALIDATION`: Set to `1` to skip browser validation at runtime
92+
- `DISPLAY`: Set to `:99` if `headless: false` (for Xvfb)
93+
94+
## Managing browser instances
95+
96+
To prevent issues with waits and resumes, you can use middleware and locals to manage the browser instance. This will ensure the browser is available for the whole run, and is properly cleaned up on waits, resumes, and after the run completes.
97+
98+
Here's an example using `chromium`, but you can adapt it for other browsers:
99+
100+
```ts
101+
import { logger, tasks, locals } from "@trigger.dev/sdk";
102+
import { chromium, type Browser } from "playwright";
103+
104+
// Create a locals key for the browser instance
105+
const PlaywrightBrowserLocal = locals.create<{ browser: Browser }>("playwright-browser");
106+
107+
export function getBrowser() {
108+
return locals.getOrThrow(PlaywrightBrowserLocal).browser;
109+
}
110+
111+
export function setBrowser(browser: Browser) {
112+
locals.set(PlaywrightBrowserLocal, { browser });
113+
}
114+
115+
tasks.middleware("playwright-browser", async ({ next }) => {
116+
// Launch the browser before the task runs
117+
const browser = await chromium.launch();
118+
setBrowser(browser);
119+
logger.log("[chromium]: Browser launched (middleware)");
120+
121+
try {
122+
await next();
123+
} finally {
124+
// Always close the browser after the task completes
125+
await browser.close();
126+
logger.log("[chromium]: Browser closed (middleware)");
127+
}
128+
});
129+
130+
tasks.onWait("playwright-browser", async () => {
131+
// Close the browser when the run is waiting
132+
const browser = getBrowser();
133+
await browser.close();
134+
logger.log("[chromium]: Browser closed (onWait)");
135+
});
136+
137+
tasks.onResume("playwright-browser", async () => {
138+
// Relaunch the browser when the run resumes
139+
// Note: You will have to have to manually get a new browser instance in the run function
140+
const browser = await chromium.launch();
141+
setBrowser(browser);
142+
logger.log("[chromium]: Browser launched (onResume)");
143+
});
144+
```
145+
146+
You can then use `getBrowser()` in your task's `run` function to access the browser instance:
147+
148+
```ts
149+
export const playwrightTestTask = task({
150+
id: "playwright-test",
151+
run: async () => {
152+
const browser = getBrowser();
153+
const page = await browser.newPage();
154+
await page.goto("https://google.com");
155+
await page.screenshot({ path: "screenshot.png" });
156+
await page.close();
157+
158+
// Waits will gracefully close the browser
159+
await wait.for({ seconds: 10 });
160+
161+
// On resume, we will re-launch the browser but you will have to manually get the new instance
162+
const newBrowser = getBrowser();
163+
const newPage = await newBrowser.newPage();
164+
await newPage.goto("https://playwright.dev");
165+
await newPage.screenshot({ path: "screenshot2.png" });
166+
await newPage.close();
167+
},
168+
});
169+
```

docs/docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"pages": [
7474
"config/extensions/prismaExtension",
7575
"config/extensions/pythonExtension",
76+
"config/extensions/playwright",
7677
"config/extensions/puppeteer",
7778
"config/extensions/ffmpeg",
7879
"config/extensions/aptGet",

docs/upgrade-to-v4.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,6 @@ pnpm dlx trigger.dev@v4-beta dev
609609

610610
During the beta we will be tracking issues and releasing regular fixes.
611611

612-
**ISSUE:** Runs not continuing after a child run(s) have completed.
613-
614612
## Deprecations
615613

616614
We've deprecated the following APIs:

packages/build/package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"./extensions/prisma": "./src/extensions/prisma.ts",
3030
"./extensions/audioWaveform": "./src/extensions/audioWaveform.ts",
3131
"./extensions/typescript": "./src/extensions/typescript.ts",
32-
"./extensions/puppeteer": "./src/extensions/puppeteer.ts"
32+
"./extensions/puppeteer": "./src/extensions/puppeteer.ts",
33+
"./extensions/playwright": "./src/extensions/playwright.ts"
3334
},
3435
"sourceDialects": [
3536
"@triggerdotdev/source"
@@ -57,6 +58,9 @@
5758
],
5859
"extensions/puppeteer": [
5960
"dist/commonjs/extensions/puppeteer.d.ts"
61+
],
62+
"extensions/playwright": [
63+
"dist/commonjs/extensions/playwright.d.ts"
6064
]
6165
}
6266
},
@@ -173,6 +177,17 @@
173177
"types": "./dist/commonjs/extensions/puppeteer.d.ts",
174178
"default": "./dist/commonjs/extensions/puppeteer.js"
175179
}
180+
},
181+
"./extensions/playwright": {
182+
"import": {
183+
"@triggerdotdev/source": "./src/extensions/playwright.ts",
184+
"types": "./dist/esm/extensions/playwright.d.ts",
185+
"default": "./dist/esm/extensions/playwright.js"
186+
},
187+
"require": {
188+
"types": "./dist/commonjs/extensions/playwright.d.ts",
189+
"default": "./dist/commonjs/extensions/playwright.js"
190+
}
176191
}
177192
},
178193
"main": "./dist/commonjs/index.js",

0 commit comments

Comments
 (0)