Skip to content

Commit 477e169

Browse files
committed
feat: lightpanda - add 3rd example
1 parent c0c2425 commit 477e169

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

docs/guides/examples/lightpanda.mdx

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,40 +68,58 @@ _Example using a German IP :_
6868
```wss://cloud.lightpanda.io/ws?proxy=datacenter&country=de&token=TOKEN```
6969

7070

71-
7271
### Session
7372
A session is alive until you close it or the connection is closed. The max time duration of a session is 15 min.
7473

7574

76-
## Example \#2 - Launch and use a Lightpanda CDP server
75+
## Example \#2 - Get a webpage using Lightpanda
7776

78-
This task initialises a Lightpanda CDP server to allow you to scrape directly via Trigger.dev.
77+
Using the Lightpanda binary we will dump the HTML for a provided URL.
78+
You will have to pass the URL as a payload when triggering the task.
79+
80+
81+
### Prerequisites
82+
- Setup the [Lightpanda build extension](/config/extensions/lightpanda)
7983

80-
### Configuration
84+
### Task
85+
```ts trigger/lightpanda-lightpanda-fetch.ts
86+
import { logger, task } from '@trigger.dev/sdk/v3'
87+
import { execSync } from 'node:child_process'
88+
89+
export const lightpandaFetch = task({
90+
id: 'lightpanda-fetch',
91+
machine: {
92+
preset: "micro",
93+
},
94+
run: async (payload: { url: string }, { ctx }) => {
95+
logger.log("Lets get a page's content with Lightpanda!", { payload, ctx })
8196

82-
To use this example, you will need to add these build settings to your `trigger.config.ts` file:
97+
if (!payload.url) {
98+
logger.warn('Please define the payload url')
99+
throw new Error('payload.url is undefined')
100+
}
83101

84-
```ts trigger.config.ts
85-
import { defineConfig } from "@trigger.dev/sdk/v3";
86-
import { lightpanda } from "@trigger.dev/build/extensions/lightpanda";
102+
const e = execSync(`${process.env.LIGHTPANDA_BROWSER_PATH} fetch --dump ${payload.url}`)
87103

88-
export default defineConfig({
89-
project: "<project ref>",
90-
// Your other config settings...
91-
build: {
92-
// This is required to use the Puppeteer library
93-
extensions: [lightpanda()],
104+
return {
105+
message: e.toString(),
106+
}
94107
},
95-
});
108+
})
96109
```
97-
That will set a `LIGHTPANDA_BROWSER_PATH` env variable that will be needed to get access to the binary.
98110

99-
### Task
111+
## Example \#3 - Launch and use a Lightpanda CDP server
112+
113+
This task initialises a Lightpanda CDP server to allow you to scrape directly via Trigger.dev.
114+
115+
### Prerequisites
116+
- Setup the [Lightpanda build extension](/config/extensions/lightpanda)
100117

118+
### Task
101119
Your task will have to launch a child process in order to have the websocket available to scrape using Puppeteer.
102120

103121
```ts trigger/lightpandaLaunch.ts
104-
import puppeteer from "puppeteer";
122+
import puppeteer from "puppeteer"
105123

106124
export const lightpandaLaunch = task({
107125
id: "lightpanda-launch",
@@ -112,11 +130,11 @@ export const lightpandaLaunch = task({
112130
browserWSEndpoint: "ws://127.0.0.1:9222",
113131
})
114132

115-
const page = await browser.newPage();
133+
const page = await browser.newPage()
116134

117135
return {
118136
data: scrapeResult,
119-
};
137+
}
120138
},
121-
});
139+
})
122140
```

0 commit comments

Comments
 (0)