Next.js Discord

Discord Forum

Publish build size on Github every after commit

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
Hi everyone, I'm looking at publishing on github as a comment of the PR the size of my nextjs app bundle everytime I do a commit. Do any of you ever made this?
If it's possible, do you know any github actions that enables this?

4 Replies

American Wirehair
Answer
American Wirehair
name: "Bundle Size"
on:
  pull_request:
    branches: [ main ]
  push:
    branches: [ main ]

jobs:
    check:
      name: Analyze Bundle Size
      runs-on: ubuntu-latest

      permissions:
        contents: read
        issues: write

      steps:
        - name: Checkout Repository
          uses: actions/checkout@v4

        - name: Cache Next.js build
          uses: actions/cache@v4
          with:
            # See here for caching with `yarn`, `bun` or other package managers https://github.com/actions/cache/blob/main/examples.md or you can leverage caching with actions/setup-node https://github.com/actions/setup-node
            path: |
              ~/.npm
              ${{ github.workspace }}/.next/cache
            # Generate a new cache whenever packages or source files change.
            key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
            # If source files changed but packages didn't, rebuild from a prior cache.
            restore-keys: |
              ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-

        - name: Setup Node.js
          uses: actions/setup-node@v4
          with:
            node-version: '22'
            cache: 'npm'

        - name: Install Dependencies
          run: npm ci

        - name: Build Project
          run: npm run build

        - name: Analyze bundle sizes
          uses: transferwise/actions-next-bundle-analyzer@v2
works a charm
Spectacled bearOP
Thanks!