From bf8d046c2c14267bf9a5a15e3f3418c2e50fe095 Mon Sep 17 00:00:00 2001 From: realityzero Date: Mon, 4 Mar 2024 23:37:33 +0530 Subject: [PATCH 1/4] fetch platform specific chrome image --- .env.example | 1 + api/screenshot.js | 44 +++++++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..083c815f --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +NODE_ENV=development \ No newline at end of file diff --git a/api/screenshot.js b/api/screenshot.js index 50169426..4f553bee 100644 --- a/api/screenshot.js +++ b/api/screenshot.js @@ -2,18 +2,40 @@ const chromium = require('@sparticuz/chromium-min'); const puppeteer = require('puppeteer-core'); let _page; +// get platform specific chrome executable for local environment +const exePath = process.platform === "win32" + ? "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" + : process.platform === "linux" + ? "/usr/bin/google-chrome" + : "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; + +async function getOptions(isDev) { + let options; + console.log('exePath:', exePath); + if (isDev) { + options = { + args: [], + executablePath: exePath, + headless: 'new', + }; + } else { + options = { + args: [...chromium.args, '--hide-scrollbars', '--disable-web-security'], + defaultViewport: chromium.defaultViewport, + executablePath: await chromium.executablePath( + `https://github.com/Sparticuz/chromium/releases/download/v116.0.0/chromium-v116.0.0-pack.tar` + ), + headless: chromium.headless, + ignoreHTTPSErrors: true, + }; + } + return options; +} + async function getBrowser() { - // local development is broken for this đŸ‘‡ - // but it works in vercel so I'm not gonna touch it - return puppeteer.launch({ - args: [...chromium.args, '--hide-scrollbars', '--disable-web-security'], - defaultViewport: chromium.defaultViewport, - executablePath: await chromium.executablePath( - `https://github.com/Sparticuz/chromium/releases/download/v116.0.0/chromium-v116.0.0-pack.tar` - ), - headless: chromium.headless, - ignoreHTTPSErrors: true, - }); + const isDevEnv = process.env.NODE_ENV == 'development'; + const options = await getOptions(isDevEnv); + return puppeteer.launch(options); } async function getPage() { From bbc7893ff1e730303b96f5f3daf547d0cc771702 Mon Sep 17 00:00:00 2001 From: realityzero Date: Mon, 4 Mar 2024 23:54:29 +0530 Subject: [PATCH 2/4] contributors update --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 09fd2cf1..72376008 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,13 @@ npm run dev Gonçalo Morais + + + realityzero +
+ Neshantt +
+ From 322f1c61fef32b256fe046454ebdf9f73863822f Mon Sep 17 00:00:00 2001 From: realityzero Date: Thu, 14 Mar 2024 13:51:35 +0530 Subject: [PATCH 3/4] chrome same args for dev --- api/screenshot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/screenshot.js b/api/screenshot.js index 4f553bee..a5f4118c 100644 --- a/api/screenshot.js +++ b/api/screenshot.js @@ -14,7 +14,7 @@ async function getOptions(isDev) { console.log('exePath:', exePath); if (isDev) { options = { - args: [], + args: [...chromium.args, '--hide-scrollbars', '--disable-web-security'], executablePath: exePath, headless: 'new', }; From ee693dd7ed2c5b8e20a882314cff87e3660592b3 Mon Sep 17 00:00:00 2001 From: realityzero Date: Thu, 14 Mar 2024 13:59:16 +0530 Subject: [PATCH 4/4] env check defaults to dev --- .env.example | 3 ++- api/screenshot.js | 33 +++++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.env.example b/.env.example index 083c815f..9347f80c 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -NODE_ENV=development \ No newline at end of file +# production | development +NODE_ENV=production \ No newline at end of file diff --git a/api/screenshot.js b/api/screenshot.js index a5f4118c..c285f0c1 100644 --- a/api/screenshot.js +++ b/api/screenshot.js @@ -9,32 +9,33 @@ const exePath = process.platform === "win32" ? "/usr/bin/google-chrome" : "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; -async function getOptions(isDev) { +async function getOptions(isProd) { let options; console.log('exePath:', exePath); - if (isDev) { - options = { - args: [...chromium.args, '--hide-scrollbars', '--disable-web-security'], - executablePath: exePath, - headless: 'new', - }; + const args = [...chromium.args, '--hide-scrollbars', '--disable-web-security']; + if (isProd) { + options = { + args, + defaultViewport: chromium.defaultViewport, + executablePath: await chromium.executablePath( + `https://github.com/Sparticuz/chromium/releases/download/v116.0.0/chromium-v116.0.0-pack.tar` + ), + headless: chromium.headless, + ignoreHTTPSErrors: true, + }; } else { options = { - args: [...chromium.args, '--hide-scrollbars', '--disable-web-security'], - defaultViewport: chromium.defaultViewport, - executablePath: await chromium.executablePath( - `https://github.com/Sparticuz/chromium/releases/download/v116.0.0/chromium-v116.0.0-pack.tar` - ), - headless: chromium.headless, - ignoreHTTPSErrors: true, + args, + executablePath: exePath, + headless: 'new', }; } return options; } async function getBrowser() { - const isDevEnv = process.env.NODE_ENV == 'development'; - const options = await getOptions(isDevEnv); + const isProdEnv = process.env.NODE_ENV == 'production'; + const options = await getOptions(isProdEnv); return puppeteer.launch(options); }