6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Next.js】ビルドIDの設定が必要なとき

Last updated at Posted at 2025-03-26

はじめに

Next.jsのアプリケーションをビルド(next build)すると一定の識別子(build_id)が自動的に生成され、アプリケーションのバージョンが識別されます。通常ならばNext.jsの自動生成に任せておいて問題はないのですが、下記のように複数サーバ構成かつ各サーバで next build が行われるパターンでは注意が必要です。

サーバ1とサーバ2で build_id が異なっていると動作に支障が生じる可能性もあるため、両者の build_id が一致するように設定しなければなりません。

ビルドIDの設定

next.config.jsgenerateBuildId 関数を追加します。この関数の返り値が build_id となります。例示したコードでは各サーバが共通して参照する環境変数に build_id を定義し、その値を呼び出しています。
なお、ビルド時には別の値にする必要がある点に注意が必要です。最新のコミットハッシュを使ったりするケースが多いようですね。

next.config.js
module.exports = {
+ generateBuildId: async () => {
+   // このコードでは環境変数でbuild_idを管理 ビルド時に環境変数は変更
+   return process.env.BUILD_ID
+ }
}

まとめ

複数サーバ構成を組んでいる場合は、同じ build_id が割り当てられるように注意をしましょう。特に「開発環境では大丈夫だったのに検証/本番環境で動かなくなった」といったケースの場合、もしかしたら冗長構成が取られていることによりサーバ間で build_id が異なってしまった結果かもしれません。

参考

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?