Skip to Content

Puppeteer examples with Trigger.dev

These examples demonstrate how to use Puppeteer with Trigger.dev.

Prerequisites

Overview

There are 3 example tasks to follow on this page:

  1. Basic example
  2. Generate a PDF from a web page
  3. Scrape content from a web page

WEB SCRAPING: When web scraping, you MUST use a proxy to comply with our terms of service. Direct scraping of third-party websites without the site owner's permission using Trigger.dev Cloud is prohibited and will result in account suspension. See this example which uses a proxy.

Build configuration

To use all examples on this page, you'll first need to add these build settings to your trigger.config.ts file:

trigger.config.ts
import {  } from "@trigger.dev/sdk";
import {  } from "@trigger.dev/build/extensions/puppeteer";

export default ({
  : "<project ref>",
  // Your other config settings...
  build: {
    // This is required to use the Puppeteer library
    : [()],
  },
});

Learn more about the trigger.config.ts file including setting default retry settings, customizing the build environment, and more.

Environment variables

The extension installs Chrome and sets PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable for deployed builds automatically, so you do not need to set it. Don't set it in your Dev environment: the extension does nothing in Dev, and locally Puppeteer uses the browser it downloads on install.

Basic example

Overview

In this example we use Puppeteer to log out the title of a web page, in this case from the Trigger.dev landing page.

Task code

trigger/puppeteer-basic-example.ts
import { ,  } from "@trigger.dev/sdk";
import  from "puppeteer";

export const puppeteerTask = ({
  : "puppeteer-log-title",
  : async () => {
    const browser = await .launch();
    const page = await browser.newPage();

    await page.goto("https://trigger.dev");

    const content = await page.title();
    .info("Content", { content });

    await browser.close();
  },
});

Testing your task

There's no payload required for this task so you can just click "Run test" from the Test page in the dashboard. Learn more about testing tasks here.

Generate a PDF from a web page

Overview

In this example we use Puppeteer to generate a PDF from the Trigger.dev landing page and upload it to Cloudflare R2.

Task code

trigger/puppeteer-generate-pdf.ts
import { ,  } from "@trigger.dev/sdk";
import  from "puppeteer";
import { ,  } from "@aws-sdk/client-s3";

// Initialize S3 client
const s3Client = new ({
  region: "auto",
  endpoint: process..S3_ENDPOINT,
  credentials: {
    accessKeyId: process..R2_ACCESS_KEY_ID ?? "",
    secretAccessKey: process..R2_SECRET_ACCESS_KEY ?? "",
  },
});

export const puppeteerWebpageToPDF = ({
  : "puppeteer-webpage-to-pdf",
  : async () => {
    const browser = await .launch();
    const page = await browser.newPage();
    const response = await page.goto("https://trigger.dev");
    const url = response?.url() ?? "No URL found";

    // Generate PDF from the web page
    const generatePdf = await page.pdf();

    .info("PDF generated from URL", { url });

    await browser.close();

    // Upload to R2
    const s3Key = `pdfs/test.pdf`;
    const uploadParams = {
      Bucket: process..S3_BUCKET,
      Key: s3Key,
      Body: generatePdf,
      ContentType: "application/pdf",
    };

    .log("Uploading to R2 with params", uploadParams);

    // Upload the PDF to R2 and return the URL.
    await s3Client.send(new (uploadParams));
    const s3Url = `https://${process..S3_BUCKET}.s3.amazonaws.com/${s3Key}`;
    .log("PDF uploaded to R2", { url: s3Url });
    return { pdfUrl: s3Url };
  },
});

Testing your task

There's no payload required for this task so you can just click "Run test" from the Test page in the dashboard. Learn more about testing tasks here.

Scrape content from a web page

Overview

In this example we use Puppeteer with a BrowserBase proxy to scrape the GitHub stars count from the Trigger.dev landing page and log it out. See this list for more proxying services we recommend.

When web scraping, you MUST use the technique below which uses a proxy with Puppeteer. Direct scraping without using browserWSEndpoint is prohibited and will result in account suspension. Screenshots are also prohibited when scraping.

Task code

trigger/scrape-website.ts
import { ,  } from "@trigger.dev/sdk";
import  from "puppeteer-core";

export const puppeteerScrapeWithProxy = ({
  : "puppeteer-scrape-with-proxy",
  : async () => {
    const browser = await .connect({
      browserWSEndpoint: `wss://connect.browserbase.com?apiKey=${process..BROWSERBASE_API_KEY}`,
    });

    const page = await browser.newPage();

    try {
      // Navigate to the target website
      await page.goto("https://trigger.dev", { waitUntil: "networkidle0" });

      // Scrape the GitHub stars count
      const starCount = await page.evaluate(() => {
        const starElement = .(".github-star-count");
        const text = starElement?. ?? "0";
        const numberText = text.(/[^0-9]/g, "");
        return (numberText);
      });

      .info("GitHub star count", { starCount });

      return { starCount };
    } catch (error) {
      .error("Error during scraping", {
        error: error instanceof Error ? error.message : (error),
      });
      throw error;
    } finally {
      await browser.close();
    }
  },
});

Testing your task

There's no payload required for this task so you can just click "Run test" from the Test page in the dashboard. Learn more about testing tasks here.

Local development

To test this example task locally, be sure to install any packages from the build extensions you added to your trigger.config.ts file to your local machine. In this case, you need to install the Puppeteer library..

Proxying

If you're using Trigger.dev Cloud and Puppeteer or any other tool to scrape content from websites you don't own, you'll need to proxy your requests. If you don't you'll risk getting our IP address blocked and we will ban you from our service. You must always have permission from the website owner to scrape their content.

Here are a list of proxy services we recommend:

Was this page helpful?

Last updated

Edit on GitHub

On this page