stefanjudis / tiny-helpers

A collection of useful online web development tools
https://tiny-helpers.dev
MIT License
954 stars 319 forks source link

[OPTIMISATION] fetch platform specific chrome image #518

Open realityzero opened 8 months ago

realityzero commented 8 months ago

desc: Environment specific chromium image selection on development environment

vercel[bot] commented 8 months ago

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
tiny-helpers βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Mar 14, 2024 8:29am
stefanjudis commented 8 months ago

@realityzero Welcome to TH and thank you so much for contributing. πŸ’―

This topic has been on the list for ages so I'm exited that someone is tackling it. I've left some comments and on the way and will test is locally once we discussed these.

Thanks again and have a great day!

realityzero commented 7 months ago

@realityzero Welcome to TH and thank you so much for contributing. πŸ’―

This topic has been on the list for ages so I'm exited that someone is tackling it. I've left some comments and on the way and will test is locally once we discussed these.

Thanks again and have a great day!

woahh @stefanjudis this made my day

stefanjudis commented 7 months ago

@realityzero I just checked out the branch and have run vercel dev (I should document this πŸ€¦β€β™‚οΈ ).

Unfortunately, it's not working locally and I'm running in target closed errors.

Error log

> Ready! Available at http://localhost:3000
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
TargetCloseError: Protocol error (Page.captureScreenshot): Target closed
    at CallbackRegistry.clear (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/Connection.ts:186:30)
    at CDPSessionImpl._onClosed (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/Connection.ts:611:21)
    at Connection.#onClose (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/Connection.ts:363:15)
    at WebSocket. (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/NodeWebSocketTransport.ts:60:22)
    at callListener (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/node_modules/ws/lib/event-target.js:290:14)
    at WebSocket.onClose (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/node_modules/ws/lib/event-target.js:220:9)
    at WebSocket.emit (node:events:517:28)
    at WebSocket.emit (node:domain:489:12)
    at WebSocket.emitClose (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/node_modules/ws/lib/websocket.js:258:10)
    at Socket.socketOnClose (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/node_modules/ws/lib/websocket.js:1264:15)
Error: Navigation failed because browser has disconnected!
    at new LifecycleWatcher (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/LifecycleWatcher.ts:109:11)
    at Frame.goto (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/Frame.ts:108:21)
    at CDPPage.goto (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/Page.ts:923:35)
    at getScreenshot (/Users/stefanjudis/Projects/github.com/tiny-helpers/api/screenshot.js:62:14)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async module.exports (/Users/stefanjudis/Project
    ...
    ...

What's your local setup to test this?

realityzero commented 7 months ago

@stefanjudis Apologies for getting onto this quite late. I couldn't test the solution end to end locally from tiny-helpers project. However, I followed the same abstracted approach of picking platform specific chromium in one of my recent projects: ScrapifyX.

That's my bad.

Could you please try this out: Stackoverflow

Or just tell me how to invoke screenshot.js after running this project. I was unable to find any references from UI.

stefanjudis commented 7 months ago

@realityzero

Apologies for getting onto this quite late.

No worries. I'm still very grateful that you're taking this on! πŸŽ‰

Or just tell me how to invoke screenshot.js after running this project.

That's my bad. As this project is deployed to vercel I run it via the CLI which I have installed globally.

$ vercel dev
Vercel CLI 33.6.0
> Running Dev Command β€œnpx @11ty/eleventy --serve --watch --port $PORT”
Writing _site/index.html from ./site/index.html.
Writing _site/home/latest/index.html from ./site/tag-sorted-by-date.njk.
Writing _site/home/index.html from ./site/tag.njk.
...
...
[Browsersync] Access URLs:
 ----------------------------------------
       Local: http://localhost:3000
    External: http://192.168.178.88:3000
 ----------------------------------------
          UI: http://localhost:3001
 UI External: http://localhost:3001
 ----------------------------------------
[Browsersync] Serving files from: _site
> Ready! Available at http://localhost:3000
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
exePath: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
TargetCloseError: Protocol error (Page.captureScreenshot): Target closed
    at CallbackRegistry.clear (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/common/CallbackRegistry.ts:93:30)
    at CdpCDPSession._onClosed (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/cdp/CDPSession.ts:149:21)
    at Connection.#onClose (/Users/stefanjudis/Projects/github.com/tiny-helpers/node_modules/puppeteer-core/src/cdp/Connection.ts:205:15)
...

I also looked briefly into this and no SO magic helped out for the local screenshoting. :/

realityzero commented 7 months ago

@stefanjudis Whenever I've read TargetCloseError or Target closed in Puppeteer, it is almost always a memory issue. Can you try adding adding following args:

  1. Let's just add this first --disable-dev-shm-usage
  2. If it still breaks, add one more argument --shm-size=3gb

Ref:

https://github.com/puppeteer/puppeteer/issues/1790 https://github.com/puppeteer/puppeteer/issues/1175

stefanjudis commented 7 months ago

@realityzero Unfortunately, neither of the options work.

I'm still running into the same exceptions. Did you try the Vercel CLI to see how to tackle it?

realityzero commented 7 months ago

Hey @stefanjudis ! Apologies man for such a late reply. Certain life challenges.

I did some debugging locally in two different stages.

  1. Look at this diff (github wasn't allowing me to upload a .patch file)
    
    diff --git a/api/screenshot.js b/api/screenshot.js
    index c285f0c..8be5f87 100644
    --- a/api/screenshot.js
    +++ b/api/screenshot.js
    @@ -27,7 +27,7 @@ async function getOptions(isProd) {
         options = {
           args,
           executablePath: exePath,
    -          headless: 'new',
    +          headless: false,
         };
     }
     return options;
    @@ -57,7 +57,7 @@ function checkUrl(string) {
    return true;
    }

-export async function getScreenshot(url, ratio = 1) { +async function getScreenshot(url, ratio = 1) { const page = await getPage(); await page.goto(url, { waitUntil: 'domcontentloaded', @@ -68,6 +68,8 @@ export async function getScreenshot(url, ratio = 1) { devicePixelRatio: ratio, }); const file = await page.screenshot();

@@ -92,3 +94,6 @@ module.exports = async (req, res) => { ); } }; + + +getScreenshot('https://news.ycombinator.com/item?id=39894820#39916444', 1); \ No newline at end of file


I simply executed ```node screenshot.js```. Here I was able to get a base64 and used some base64 to image converted online and it went fine.
2. I tried ```vercel dev``` command to replicate the same connection level thing of deployment. However I couldn't navigate to a page that takes screenshots. I might need help in that. But it triggered ```screenshot.js``` behind the scenes in terminal and executed same thing as point 1.

How about we get over a call for this in the coming week?
stefanjudis commented 5 months ago

@realityzero Hey, so sorry for letting this slip through. :/ Life's pretty wild on my end.

But in the meantime, something changed on vercel's side and things broke entirely. I think I'll move the screenshots off serverless fns. It's just to briddle. ;/

Sorry for wasting your time.