有大佬使用 nextjs 上添加 puppeteer 吗?有成功部署在 vercel 上的吗? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dalovelanlan
V2EX    Node.js

有大佬使用 nextjs 上添加 puppeteer 吗?有成功部署在 vercel 上的吗?

  •  
  •   dalovelanlan 2024-08-08 14:28:21 +08:00 2537 次点击
    这是一个创建于 427 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有大佬使用 nextjs 上添加 puppeteer 吗?有成功部署在 vercel 上的吗?,我这里 Vercel 的 log

    Error in getChangeTabPuppeteerDataUrl: Error: The input directory "/opt/nodejs/node_modules/@sparticuz/chromium/bin" does not exist. at u.executablePath (/var/task/.next/server/chunks/896.js:2:67422) at g (/var/task/.next/server/app/api/video/route.js:12:17631) at f (/var/task/.next/server/app/api/video/route.js:12:15627) at b (/var/task/.next/server/app/api/video/route.js:12:15556) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async c (/var/task/.next/server/app/api/video/route.js:1:1427) at async /var/task/node_modules/.pnpm/[email protected]_[email protected].1_[email protected].1_[email protected].1/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36258 at async eR.execute (/var/task/node_modules/.pnpm/[email protected]_[email protected].1_[email protected].1_[email protected].1/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:26874) at async eR.handle (/var/task/node_modules/.pnpm/[email protected]_[email protected].1_[email protected].1_[email protected].1/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:37512) at async es (/var/task/node_modules/.pnpm/[email protected]_[email protected].1_[email protected].1_[email protected].1/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25465)

    我之前找的都是让使用这个第三方库来启动 chromium @sparticuz/chromium ,在这个 github 的 issue 上也没找到成功的例子,来求助 v 站大佬

    17 条回复    2024-08-16 18:42:02 +08:00
    RoyRao
        1
    RoyRao  
       2024-08-08 15:17:55 +08:00
    运行路径设置没?

    ```
    browser = await this.puppeteer.launch({
    args: dev ? [] : chromium.args,
    defaultViewport: chromium.defaultViewport,
    executablePath: dev
    ? localChromiumPath
    : await chromium.executablePath(),
    headless: chromium.headless,
    });
    ```

    另外部署在边缘网络应该会有点问题...
    dalovelanlan
        2
    dalovelanlan  
    OP
       2024-08-08 15:19:33 +08:00
    @RoyRao 路径设置的是这个
    return puppeteer.launch(
    process?.platform === "darwin"
    ? {
    executablePath: localExecutablePath,
    }
    : {
    // args: [...chromium.args, "--hide-scrollbars", "--disable-web-security"],
    // defaultViewport: chromium.defaultViewport,
    // executablePath: await chromium.executablePath(
    // `https://github.com/Sparticuz/chromium/releases/download/v126.0.0/chromium-v126.0.0-pack.tar`
    // ),
    // headless: chromium.headless,
    // ignoreHTTPSErrors: true,
    args: chromium.args,
    defaultViewport: chromium.defaultViewport,
    executablePath: isDev
    ? localExecutablePath
    : await chromium.executablePath("/opt/nodejs/node_modules/@sparticuz/chromium/bin"),
    headless: chromium.headless,
    timeout: 60000,
    }
    );
    dalovelanlan
        3
    dalovelanlan  
    OP
       2024-08-08 15:21:16 +08:00
    @RoyRao 刚开始是按照你写的这部署的,运行会也会报找不到地址

    Error: The input directory "/var/task/.next/server/bin" does not exist.
    at u.executablePath (/var/task/.next/server/chunks/896.js:2:67422)
    RoyRao
        4
    RoyRao  
       2024-08-08 15:42:14 +08:00
    @dalovelanlan #3 Vercel 免费用户有 50MB 的上传大小限制,或者尝试使用`@sparticuz/chromium-min`然后把完整包传到自己 VPS 上。我看你尝试了
    ```
    await chromium.executablePath(
    `https://github.com/Sparticuz/chromium/releases/download/v126.0.0/chromium-v126.0.0-pack.tar`
    )
    ```
    不知道你注释掉是因为 Vercel 访问不了 GitHub 还是啥?之前有个项目本来准备部署到 Vercel 的,后面发现太麻烦了,然后项目搁置了...
    dalovelanlan
        5
    dalovelanlan  
    OP
       2024-08-08 15:45:19 +08:00
    @RoyRao 注释掉是因为 vercel 会有个超时限制,免费用户 60s ,每次都会超时,我不知道是不是因为这个,这个库也没有说如何打印下载成功之类,提了 issue 也没有人回答, 你说的边缘网络是什么意思,我第一次用这个 vercel ,不太熟悉
    RoyRao
        6
    RoyRao  
       2024-08-08 16:05:38 +08:00
    @dalovelanlan #5

    https://vercel.com/docs/edge-network/overview

    不知道会不会默认部署到边缘网络。之前我尝试部署过,需要新建 vercel.json ,单独配置 @vercel/node ,指定到你 puppeteer 相关的 js 文件。

    https://stackoverflow.com/questions/61808973/is-it-possible-to-deploy-a-nodejs-app-in-vercel

    https://github.com/browserless/vercel-puppeteer

    https://dev.to/joelgriffith/vercel-puppeteer-4l7c

    具体可以参考一下这些,之前因为项目搁置,也没成功部署过,提供不了太多帮助... 如果你搞定了,也可以分享学习一下
    dalovelanlan
        7
    dalovelanlan  
    OP
       2024-08-08 16:28:59 +08:00
    @RoyRao 这个「 browserless/vercel-puppeteer 」我看过,太贵了,200$每月,好的,感谢回复,我再研究研究
    runlongyao2
        8
    runlongyao2  
       2024-08-08 16:36:04 +08:00
    chromium 应该是在环境里提前安装好的,然后 executablePath 指向安装位置。我记得我做 docker image 的时候是这样的
    dalovelanlan
        9
    dalovelanlan  
    OP
       2024-08-08 17:42:32 +08:00
    @runlongyao2 docker 的话,可以直接下载 chromium 了,这个是部署 vercel 上,可以是使用 docker 吗
    dalovelanlan
        10
    dalovelanlan  
    OP
       2024-08-13 09:26:36 +08:00
    这种方法是可行的,executablePath: await chromium.executablePath(
    `https://github.com/Sparticuz/chromium/releases/download/v126.0.0/chromium-v126.0.0-pack.tar`
    ), 但是,仅支持简单的操作,比如生成 pdf 、获取简单的数据,稍微复杂一点耗时的,就很容易超时
    wktline
        11
    wktline  
       2024-08-14 17:46:06 +08:00
    nextjs 内核是不是只支持一个简单的 nodejs api
    dalovelanlan
        12
    dalovelanlan  
    OP
       2024-08-14 17:53:54 +08:00
    @wktline 不能说是跟 nextjs 的关系,准确的说是跟 vercel 这个 serverless 服务商,你要在 nextjs 里进行耗时操作,那就不能把 nextjs 项目部署 vercel 上了
    wktline
        13
    wktline  
       2024-08-14 19:13:19 +08:00
    @dalovelanlan 个人建议,puppeteer 单独搞一个服务,别和 nodejs 耦合到一起,然后用 generic-pool 优化实例开销
    testdoge
        14
    testdoge  
       2024-08-14 23:20:18 +08:00
    直接用 vercel serverless 部署 puppeteer-core + @sparticuz/chromium 不就完事了吗?
    dalovelanlan
        15
    dalovelanlan  
    OP
       2024-08-15 09:33:29 +08:00
    @testdoge 可以是可以,但是我要进行的爬虫比较耗时,vercel 的免费用户超时时间最长 60s
    dalovelanlan
        16
    dalovelanlan  
    OP
       2024-08-15 09:35:59 +08:00
    @wktline 主要是想白嫖 vercel ,不想单独花钱搞一个服务,毕竟项目是自己的小工具项目,能免费就免费,不能免费就本地起服务
    testdoge
        17
    testdoge  
       2024-08-16 18:42:02 +08:00
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3983 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 04:10 PVG 12:10 LAX 21:10 JFK 00:10
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86