Next.jsのプロセス分離についてしらべる
next/testmodeの導入の発端となったらしいIssue
の中に次の記述がある
I looked into this a month ago and discovered that there's difficulty to establish any process-wide logic in Next.js 13 due to the way Next itself is implemented internally. Very briefly, Next keeps two Node.js processes while it runs:
これがどういうものなのかあんまり理解できてないので調べる
When a hot update is issued for a nested layout, i.e. a certain page's layout, the intermittent process gets killed, then a new one spawns at its place (on a different port), likely evaluates the update, and keeps pending.
- hot update とあるので、開発時だけの話?
- ネストした部分の描画でプロセス分けてる箇所がある?
そもそも小さいケースでプロセス分離を確認してみる
なんか3つある
next-router-worker
next start
↓
startServer - initialize
router-server # intialize の中で、worker-server とやらも起動してる
next-render-worker-app
とかの名前つけてる場所
packages/next/src/server/lib/setup-server-worker.ts
# initializeServerWorker
が実体
http
モジュールの createServer
で得られた server に対して、port:0 でlistenしてる。
listen時のportに関しての説明はこのあたり
If port is omitted or is 0, the operating system will assign an arbitrary unused port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.
0指定の場合は、OS側で自動的に割り当てられる