Skip to content

Commit 27fd79c

Browse files
Added next bundle analyzer
1 parent 46a7a7c commit 27fd79c

File tree

4 files changed

+323
-1
lines changed

4 files changed

+323
-1
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
name: 'Next.js Bundle Analysis'
5+
6+
on:
7+
pull_request:
8+
push:
9+
branches:
10+
- main
11+
workflow_dispatch:
12+
13+
defaults:
14+
run:
15+
# change this if your nextjs app does not live at the root of the repo
16+
working-directory: ./
17+
18+
permissions:
19+
contents: read # for checkout repository
20+
actions: read # for fetching base branch bundle stats
21+
pull-requests: write # for comments
22+
23+
jobs:
24+
analyze:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Install Node.js
30+
uses: actions/setup-node@v4
31+
with:
32+
node-version: lts/*
33+
34+
- name: Install dependencies
35+
uses: bahmutov/npm-install@v1
36+
37+
# If pnpm is used, you need to switch the previous step with the following one. pnpm does not create a package-lock.json
38+
# so the step above will fail to pull dependencies
39+
# - uses: pnpm/action-setup@v2
40+
# name: Install pnpm
41+
# id: pnpm-install
42+
# with:
43+
# version: 7
44+
# run_install: true
45+
46+
- name: Restore next build
47+
uses: actions/cache@v4
48+
id: restore-build-cache
49+
env:
50+
cache-name: cache-next-build
51+
with:
52+
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
53+
# ex: if your app builds to `dist`, replace `.next` with `dist`
54+
path: .next/cache
55+
# change this if you prefer a more strict cache
56+
key: ${{ runner.os }}-build-${{ env.cache-name }}
57+
58+
- name: Check data
59+
env:
60+
MY_SECRET: ${{ vars.AAA }}
61+
run: |
62+
echo "The value of MY_SECRET is: $MY_SECRET"
63+
64+
- name: Build next.js app
65+
env:
66+
NEXT_MONGO_DB_URL: ${{ secrets.NEXT_MONGO_DB_URL }}
67+
NEXT_PUBLIC_NODE_ENV: ${{ secrets.NEXT_PUBLIC_NODE_ENV }}
68+
run: npm run build:report
69+
70+
# Here's the first place where next-bundle-analysis' own script is used
71+
# This step pulls the raw bundle stats for the current bundle
72+
- name: Analyze bundle
73+
run: npx -p nextjs-bundle-analysis report
74+
75+
- name: Upload bundle
76+
uses: actions/upload-artifact@v4
77+
with:
78+
name: bundle
79+
path: .next/analyze/__bundle_analysis.json
80+
81+
- name: Download base branch bundle stats
82+
uses: dawidd6/action-download-artifact@v6
83+
if: success() && github.event.number
84+
with:
85+
workflow: nextjs_bundle_analysis.yml
86+
branch: ${{ github.event.pull_request.base.ref }}
87+
path: .next/analyze/base
88+
89+
# And here's the second place - this runs after we have both the current and
90+
# base branch bundle stats, and will compare them to determine what changed.
91+
# There are two configurable arguments that come from package.json:
92+
#
93+
# - budget: optional, set a budget (bytes) against which size changes are measured
94+
# it's set to 350kb here by default, as informed by the following piece:
95+
# https://infrequently.org/2021/03/the-performance-inequality-gap/
96+
#
97+
# - red-status-percentage: sets the percent size increase where you get a red
98+
# status indicator, defaults to 20%
99+
#
100+
# Either of these arguments can be changed or removed by editing the `nextBundleAnalysis`
101+
# entry in your package.json file.
102+
- name: Compare with base branch bundle
103+
if: success() && github.event.number
104+
run: ls -laR .next/analyze/base && npx -p nextjs-bundle-analysis compare
105+
106+
- name: Get Comment Body
107+
id: get-comment-body
108+
if: success() && github.event.number
109+
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
110+
run: |
111+
echo "body<<EOF" >> $GITHUB_OUTPUT
112+
echo "$(cat .next/analyze/__bundle_analysis_comment.txt)" >> $GITHUB_OUTPUT
113+
echo EOF >> $GITHUB_OUTPUT
114+
115+
- name: Find Comment
116+
uses: peter-evans/find-comment@v3
117+
if: success() && github.event.number
118+
id: fc
119+
with:
120+
issue-number: ${{ github.event.number }}
121+
body-includes: '<!-- __NEXTJS_BUNDLE -->'
122+
123+
- name: Create Comment
124+
uses: peter-evans/create-or-update-comment@v3
125+
if: success() && github.event.number && steps.fc.outputs.comment-id == 0
126+
with:
127+
issue-number: ${{ github.event.number }}
128+
body: ${{ steps.get-comment-body.outputs.body }}
129+
130+
- name: Update Comment
131+
uses: peter-evans/create-or-update-comment@v3
132+
if: success() && github.event.number && steps.fc.outputs.comment-id != 0
133+
with:
134+
issue-number: ${{ github.event.number }}
135+
body: ${{ steps.get-comment-body.outputs.body }}
136+
comment-id: ${{ steps.fc.outputs.comment-id }}
137+
edit-mode: replace

next.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import withBundleAnalyzer from '@next/bundle-analyzer'
2+
3+
14
/**
25
* @type {import('next').NextConfig}
36
*/
@@ -10,4 +13,8 @@ const nextConfig = {
1013
swcMinify: true,
1114
}
1215

13-
export default nextConfig
16+
const withNextBundleAnalyzer = withBundleAnalyzer({
17+
enabled: process.env.NEXT_BUNDLE_ANALYZER === 'true'
18+
})
19+
20+
export default withNextBundleAnalyzer(nextConfig)

0 commit comments

Comments
 (0)