serverless / serverless-tencent

⚡️ 🐧 Serverless Tencent CLI 及中文讨论社区
https://cn.serverless.com
63 stars 26 forks source link

`http`组建部署`Next.js`时自动生成的`scf_bootstrap`不支持Next.js12 #204

Closed c0per closed 2 years ago

c0per commented 2 years ago

BUG 描述:

复现步骤:

使用http组建部署Next.js应用(serverless.yml如下),会自动生成如下的scf_bootstrap

#!/var/lang/node16/bin/node

const { nextStart } = require('next/dist/cli/next-start');
nextStart(['--port', '9000', '--hostname', '0.0.0.0']);

预期结果:

Next.js 正常启动,监听9000端口。

实际结果:

(使用Next.js12版本)在require('next/dist/cli/next-start')执行后,一个Next.js dev服务就已经启动。之后的nextStart启动的服务会因9000端口被占用而运行在9001。

调试信息:

serverless.yml 配置 ```yaml component: http name: test-demo stage: prod inputs: src: dist: ./ hook: npm run build exclude: - .env faas: runtime: Nodejs16.13 framework: nextjs name: ${name} apigw: protocols: - http - https ```

额外信息

怀疑是require的js代码'next/dist/cli/next-start'导入了其他什么文件,在require时自动执行了脚本,导致生成了一个dev服务器。

看了一下Next.js的代码(启动服务的部分),写出了一个调用更底层api(startServer而非nextStart)的scf_bootstrap,可以在Next.js12中正常使用,未对更低版本测试:

#!/var/lang/node16/bin/node

const { startServer } = require('next/dist/server/lib/start-server');
startServer({ hostname: '0.0.0.0', port: 9000 })
    .then((app) => app.prepare())
    .catch((_e) => {
        process.exit(1);
    })
;
c0per commented 2 years ago

我把这个issue提到serverless-components/tencent-http了,https://github.com/serverless-components/tencent-http/issues/24