Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit c726a6b

Browse files
authored
Merge pull request #11 from fiqryq/chore/next-bundle-bot
chore: add nextjs-bundle-analysis workflows
2 parents 26fe492 + 8a9df45 commit c726a6b

2 files changed

Lines changed: 120 additions & 1 deletion

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: "Next.js Bundle Analysis"
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main # change this if your default branch is named differently
8+
workflow_dispatch:
9+
10+
defaults:
11+
run:
12+
# change this if your nextjs app does not live at the root of the repo
13+
working-directory: ./
14+
15+
jobs:
16+
analyze:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
21+
- name: Set up node
22+
uses: actions/setup-node@v1
23+
with:
24+
node-version: "14.x"
25+
26+
- name: Install dependencies
27+
uses: bahmutov/npm-install@v1
28+
29+
- name: Restore next build
30+
uses: actions/cache@v2
31+
id: restore-build-cache
32+
env:
33+
cache-name: cache-next-build
34+
with:
35+
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
36+
# ex: if your app builds to `dist`, replace `.next` with `dist`
37+
path: .next/cache
38+
# change this if you prefer a more strict cache
39+
key: ${{ runner.os }}-build-${{ env.cache-name }}
40+
41+
- name: Build next.js app
42+
# change this if your site requires a custom build command
43+
run: ./node_modules/.bin/next build
44+
45+
# Here's the first place where next-bundle-analysis' own script is used
46+
# This step pulls the raw bundle stats for the current bundle
47+
- name: Analyze bundle
48+
run: npx -p nextjs-bundle-analysis report
49+
50+
- name: Upload bundle
51+
uses: actions/upload-artifact@v2
52+
with:
53+
name: bundle
54+
path: .next/analyze/__bundle_analysis.json
55+
56+
- name: Download base branch bundle stats
57+
uses: dawidd6/action-download-artifact@v2
58+
continue-on-error: true
59+
if: success() && github.event.number
60+
with:
61+
workflow: nextjs_bundle_analysis.yml
62+
branch: ${{ github.event.pull_request.base.ref }}
63+
path: .next/analyze/base
64+
65+
# And here's the second place - this runs after we have both the current and
66+
# base branch bundle stats, and will compare them to determine what changed.
67+
# There are two configurable arguments that come from package.json:
68+
#
69+
# - budget: optional, set a budget (bytes) against which size changes are measured
70+
# it's set to 350kb here by default, as informed by the following piece:
71+
# https://infrequently.org/2021/03/the-performance-inequality-gap/
72+
#
73+
# - red-status-percentage: sets the percent size increase where you get a red
74+
# status indicator, defaults to 20%
75+
#
76+
# Either of these arguments can be changed or removed by editing the `nextBundleAnalysis`
77+
# entry in your package.json file.
78+
- name: Compare with base branch bundle
79+
if: success() && github.event.number
80+
run: ls -laR .next/analyze/base && npx -p nextjs-bundle-analysis compare
81+
82+
- name: Get comment body
83+
id: get-comment-body
84+
if: success() && github.event.number
85+
run: |
86+
body=$(cat .next/analyze/__bundle_analysis_comment.txt)
87+
body="${body//'%'/'%25'}"
88+
body="${body//$'\n'/'%0A'}"
89+
body="${body//$'\r'/'%0D'}"
90+
echo ::set-output name=body::$body
91+
92+
- name: Find Comment
93+
uses: peter-evans/find-comment@v1
94+
if: success() && github.event.number
95+
id: fc
96+
with:
97+
issue-number: ${{ github.event.number }}
98+
body-includes: "<!-- __NEXTJS_BUNDLE -->"
99+
100+
- name: Create Comment
101+
uses: peter-evans/create-or-update-comment@v1.4.4
102+
if: success() && github.event.number && steps.fc.outputs.comment-id == 0
103+
with:
104+
issue-number: ${{ github.event.number }}
105+
body: ${{ steps.get-comment-body.outputs.body }}
106+
107+
- name: Update Comment
108+
uses: peter-evans/create-or-update-comment@v1.4.4
109+
if: success() && github.event.number && steps.fc.outputs.comment-id != 0
110+
with:
111+
issue-number: ${{ github.event.number }}
112+
body: ${{ steps.get-comment-body.outputs.body }}
113+
comment-id: ${{ steps.fc.outputs.comment-id }}
114+
edit-mode: replace

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@
3131
"prettier-plugin-tailwindcss": "^0.1.8",
3232
"tailwindcss": "^3.2.1",
3333
"typescript": "4.6.3"
34+
},
35+
"nextBundleAnalysis": {
36+
"budget": 358400,
37+
"budgetPercentIncreaseRed": 20,
38+
"showDetails": true
3439
}
35-
}
40+
}

0 commit comments

Comments
 (0)