-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-compute-first-load.js
54 lines (35 loc) · 1.29 KB
/
next-compute-first-load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env node
const fs = require('fs-extra');
const gzipSize = require('gzip-size');
const run = async () => {
const sumGzipSizes = (chunkList) => {
let sum = 0;
for (j = 0; j < chunkList.length; j++) {
const currentSize = gzipSize.fileSync(`.next/${chunkList[j]}`);
sum += currentSize;
};
return Math.round(sum/1000);
}
const manifest_json = JSON.parse(fs.readFileSync('.next/build-manifest.json',
'utf8'));
const build_id = fs.readFileSync('.next/BUILD_ID', 'utf8');
const pages = manifest_json.pages;
// Remove polyfill size check
const pagesKeys = Object.keys(pages).filter(key => key !== '/_polyfills');
let firstLoads = {};
for (i = 0; i < pagesKeys.length; i++) {
const currentPage = pagesKeys[i];
const pageBundlePath = `/static/${build_id}/pages${currentPage}.js`;
const appBundlePath = `/static/${build_id}/pages/_app.js`;
// Page first load = page chunks + app chunks + page bundle + app bundle
const pageChunks = [...new Set([...pages[currentPage], ...pages['/_app'], pageBundlePath, appBundlePath])];
firstLoads[currentPage] = sumGzipSizes(pageChunks);
}
// Write bundleStats.json
fs.writeFileSync(
'./first-load-stats.json',
JSON.stringify(firstLoads, null, 2) + "\n",
'utf-8'
);
};
run();