Skip to content

部署Next.js时自动生成的scf_bootstrap不支持Next.js12 #24

@c0per

Description

@c0per

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 配置
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);
    })
;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions