Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: a chromium bug where headless mode was set to false from chromium.headless #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ RUN yarn build --production
# Final stage
FROM node:${NODE_VERSION}-${DEBIAN_VERSION} AS final

# Set the environment variable HEADLESS to true
ENV HEADLESS=true

WORKDIR /app

COPY package.json yarn.lock ./
Expand Down
41 changes: 22 additions & 19 deletions api/screenshot.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
import puppeteer from 'puppeteer-core';
import chromium from 'chrome-aws-lambda';
import middleware from './_common/middleware.js';
import puppeteer from "puppeteer-core";
import chromium from "chrome-aws-lambda";
import middleware from "./_common/middleware.js";

const screenshotHandler = async (targetUrl) => {

if (!targetUrl) {
throw new Error('URL is missing from queryStringParameters');
throw new Error("URL is missing from queryStringParameters");
}

if (!targetUrl.startsWith('http://') && !targetUrl.startsWith('https://')) {
targetUrl = 'http://' + targetUrl;
if (!targetUrl.startsWith("http://") && !targetUrl.startsWith("https://")) {
targetUrl = "http://" + targetUrl;
}

try {
new URL(targetUrl);
} catch (error) {
throw new Error('URL provided is invalid');
throw new Error("URL provided is invalid");
}

let browser = null;
try {
browser = await puppeteer.launch({
args: [...chromium.args, '--no-sandbox'], // Add --no-sandbox flag
browser = await puppeteer.launch({
args: [...chromium.args, "--no-sandbox"], // Add --no-sandbox flag
defaultViewport: { width: 800, height: 600 },
executablePath: process.env.CHROME_PATH || await chromium.executablePath,
headless: chromium.headless,
executablePath:
process.env.CHROME_PATH || (await chromium.executablePath),
headless: process.env.HEADLESS ? true : chromium.headless,
ignoreHTTPSErrors: true,
ignoreDefaultArgs: ['--disable-extensions'],
ignoreDefaultArgs: ["--disable-extensions"],
});

let page = await browser.newPage();

await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'dark' }]);
await page.emulateMediaFeatures([
{ name: "prefers-color-scheme", value: "dark" },
]);
page.setDefaultNavigationTimeout(8000);
await page.goto(targetUrl, { waitUntil: 'domcontentloaded' });
await page.goto(targetUrl, { waitUntil: "domcontentloaded" });

await page.evaluate(() => {
const selector = 'body';
const selector = "body";
return new Promise((resolve, reject) => {
const element = document.querySelector(selector);
if (!element) {
reject(new Error(`Error: No element found with selector: ${selector}`));
reject(
new Error(`Error: No element found with selector: ${selector}`)
);
}
resolve();
});
});

const screenshotBuffer = await page.screenshot();
const base64Screenshot = screenshotBuffer.toString('base64');
const base64Screenshot = screenshotBuffer.toString("base64");

return { image: base64Screenshot };

} finally {
if (browser !== null) {
await browser.close();
Expand Down