Open10

Next.jsのプロセス分離についてしらべる

mugimugi

next/testmodeの導入の発端となったらしいIssue

https://github.com/mswjs/msw/issues/1644

の中に次の記述がある

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:

これがどういうものなのかあんまり理解できてないので調べる

mugimugi

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 とあるので、開発時だけの話?
  • ネストした部分の描画でプロセス分けてる箇所がある?
mugimugi

そもそも小さいケースでプロセス分離を確認してみる

mugimugi

router-server # intialize の中で、worker-server とやらも起動してる
https://github.com/vercel/next.js/blob/7ac6bb1a88395953a0dc2706269be696e98ac460/packages/next/src/server/lib/router-server.ts#L220-L224

next-render-worker-app とかの名前つけてる場所
https://github.com/vercel/next.js/blob/7ac6bb1a88395953a0dc2706269be696e98ac460/packages/next/src/server/lib/render-server.ts#L83-L87

packages/next/src/server/lib/setup-server-worker.ts # initializeServerWorker が実体

http モジュールの createServer で得られた server に対して、port:0 でlistenしてる。
https://github.com/vercel/next.js/blob/7ac6bb1a88395953a0dc2706269be696e98ac460/packages/next/src/server/lib/setup-server-worker.ts#L119