App routerのNext.jsビルド時に、@next/bundle-anlyzerを利用してページあたりのデータ量をPull Requestのコメントとして表示することで、可視化します
こんな感じで、Pull Requestのコメントに1ページあたりの要求されるデータ量が表示されます
デプロイ前の統計量を確認するのに便利です 🐶

通常の場合は、以下のようにするとインストールが可能なのですが、どうやら標準のものは、App routerに非対応のようです (Pages routerには引き続き使えそうです)
npm i @next/bundle-analyzerこれに困っている人は多いようで、海外の方ですでに解決してくれている方がいました
このissueによれば raphaelbadia/report-bundle-size.js - GitHubGist のスクリプトを参考にすれば、使えそうです
ただし gzip-size が 7.0.0 だと、このままだと動作しないので、ESM形式に書き換えてみました。参考にどうぞー
raphaelbadia/report-bundle-size.js のESM形式版
以上を参考にGitHub Actions上に設定してみます
まずは、このスクリプトをダウンロードし report-bundle-size.js として適当な位置に保存します
私は scripts/report-bundle-size.js として保存しました
package.json に nextBundleAnalysis を追記します
budget は、1ページあたりの初回ロードの目標値(バイト)、minimumChangeThreshold は、既存ページに対してどの程度変化があった場合にレポートに表示するかを決定できます(こちらもバイト)
{"name": "folklore",..."nextBundleAnalysis": {"budget": 358400,"budgetPercentIncreaseRed": 20,"minimumChangeThreshold": 358,"showDetails": true}}
おそらく最適化の関係で、全く関係ないページを編集しても、多くのページに対して変更に関するレポートに載るので minimumChangeThreshold を設定しておくことをおすすめします (私は ±0.1%=358B で設定してます)
GitHub Actionsに組み込みます。ビルド後に先ほどの report-bundle-size.js を呼び出せばOKです
注意
デフォルトブランチ (多くの方は main だと思います) のartifactに差分データを保存し比較するので、過去にデフォルトブランチにマージされて生成された差分データがない場合は、エラーになります
つまり、導入初回のPull Requestはエラーになります
name: Deployon:push:branches:- mainpull_request:types: [opened, synchronize]jobs:deploy:name: Deployruns-on: ubuntu-lateststeps:- name: Checkoutuses: actions/checkout@v4- name: Setup Node.jsuses: actions/setup-node@v4with:node-version: '20'- name: Buildrun: npm build# - - - Analysis Report - - -- name: Analyze bundlerun: node scripts/report-bundle-size.js- name: Upload bundleuses: actions/upload-artifact@v3with:name: bundlepath: .next/analyze/__bundle_analysis.json- name: Download base branch bundle statsuses: dawidd6/action-download-artifact@v2if: success() && github.event.numberwith:workflow: production.ymlbranch: ${{ github.event.pull_request.base.ref }}path: .next/analyze/base- name: Compare with base branch bundleif: success() && github.event.numberrun: ls -laR .next/analyze/base && yarn dlx -p nextjs-bundle-analysis compare- name: Get Comment Bodyid: get-comment-bodyif: success() && github.event.number# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-stringsrun: |echo "body<<EOF" >> $GITHUB_OUTPUTecho "$(cat .next/analyze/__bundle_analysis_comment.txt)" >> $GITHUB_OUTPUTecho EOF >> $GITHUB_OUTPUT- name: Find Commentuses: peter-evans/find-comment@v2if: success() && github.event.numberid: fcwith:issue-number: ${{ github.event.number }}body-includes: '<!-- __NEXTJS_BUNDLE -->'- name: Create Commentuses: peter-evans/create-or-update-comment@v2if: success() && github.event.number && steps.fc.outputs.comment-id == 0with:issue-number: ${{ github.event.number }}body: ${{ steps.get-comment-body.outputs.body }}- name: Update Commentuses: peter-evans/create-or-update-comment@v2if: success() && github.event.number && steps.fc.outputs.comment-id != 0with:issue-number: ${{ github.event.number }}body: ${{ steps.get-comment-body.outputs.body }}comment-id: ${{ steps.fc.outputs.comment-id }}edit-mode: replace
30行目のスクリプトの呼び出しパスのみ留意すれば、動作するかなと思います 🎉
おわりに
長々になってしまいましたが、App routerの場合は一手間必要のようですね 🐶
話、全然変わりますけど11日あたりから強めの太陽フレアが地球にくるっぽいので、ご注意を 🌞
(おまけ) デフォルトブランチにマージしてもデプロイしない場合