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

[OPTIMISATION] fetch platform specific chrome image #518

Open
wants to merge 4 commits into
base: primary
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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# production | development
NODE_ENV=production
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ npm run dev
<sub><b>Gonçalo Morais</b></sub>
</a>
</td>
<td align="center">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI — this readme section was broken for a while and when we're at it, i'll probably remove it because it doesn't show all contributoes.

Your avatar will still be shown on tiny-helpers.dev though.

image

<a href="https://github.com/realityzero">
<img src="https://avatars.githubusercontent.com/u/33288462?v=4" width="75;" alt="realityzero"/>
<br />
<sub><b>Neshantt</b></sub>
</a>
</td>
</tr>
</table>

45 changes: 34 additions & 11 deletions api/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,41 @@ 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(isProd) {
let options;
console.log('exePath:', exePath);
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,
executablePath: exePath,
headless: 'new',
};
}
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 isProdEnv = process.env.NODE_ENV == 'production';
const options = await getOptions(isProdEnv);
return puppeteer.launch(options);
}

async function getPage() {
Expand Down