forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowcase.js
128 lines (115 loc) · 3.59 KB
/
showcase.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable no-console */
const fs = require('fs');
const puppeteer = require('puppeteer');
const pages = require('./pages.json');
const OUTPUT_JSON = 'src/routes/showcase/generated-pages.json';
async function captureMultipleScreenshots() {
if (!fs.existsSync('public/showcases')) {
fs.mkdirSync('public/showcases');
}
let browser = null;
const output = [];
try {
// launch headless Chromium browser
browser = await puppeteer.launch({
headless: true,
});
// create new page object
const page = await browser.newPage();
// set viewport width and height
await page.setViewport({
width: 1440,
height: 980,
});
let existingJson = [];
try {
const data = fs.readFileSync(OUTPUT_JSON, 'utf8');
existingJson = JSON.parse(data);
} catch (e) {
// ignore
}
for (const { url, size } of pages) {
const existing = existingJson.find((item) => item.href === url);
if (existing) {
console.log('Skipping page', url);
output.push({
...existing,
size,
});
continue;
}
console.log('Opening page', url);
await page.goto(url);
const title = await page.title();
const filename = url
.replace('https://', '')
.replace('/', '_')
.replace('.', '_')
.replace('.', '_')
.toLowerCase();
const path = `public/showcases/${filename}.webp`;
const [pagespeedOutput, _] = await Promise.all([
getPagespeedData(url),
page.screenshot({
path: path,
type: 'webp',
quality: 50,
}),
]);
const fcpDisplay =
pagespeedOutput.lighthouseResult?.audits?.['first-contentful-paint']?.displayValue;
const fcpScore = pagespeedOutput?.lighthouseResult?.audits?.['first-contentful-paint']?.score;
const lcpDisplay =
pagespeedOutput?.lighthouseResult?.audits?.['largest-contentful-paint']?.displayValue;
const lcpScore =
pagespeedOutput?.lighthouseResult?.audits?.['largest-contentful-paint']?.score;
const ttiDisplay = pagespeedOutput?.lighthouseResult?.audits?.interactive?.displayValue;
const ttiScore = pagespeedOutput?.lighthouseResult?.audits?.interactive?.score;
const ttiTime = pagespeedOutput?.lighthouseResult?.audits?.interactive?.numericValue;
const score = pagespeedOutput?.lighthouseResult?.categories?.performance?.score;
const perf = {
score,
fcpDisplay,
fcpScore,
lcpDisplay,
lcpScore,
ttiDisplay,
ttiScore,
ttiTime,
};
output.push({
title,
href: url,
imgSrc: `/showcases/${filename}.webp`,
perf,
size,
});
console.log(`✅ ${title} - (${url})`);
}
} catch (err) {
console.log(`❌ Error: ${err.message}`);
} finally {
if (browser) {
await browser.close();
}
console.log(`\n🎉 ${pages.length} screenshots captured.`);
}
fs.writeFileSync(OUTPUT_JSON, JSON.stringify(output, undefined, 2));
}
async function getPagespeedData(url) {
const { default: fetch } = await import('node-fetch');
const requestURL = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=${encodeURIComponent(
url
)}&key=AIzaSyApBC9gblaCzWrtEBgHnZkd_B37OF49BfM&category=PERFORMANCE&strategy=MOBILE`;
return await fetch(requestURL, {
headers: {
referer: 'https://www.builder.io/',
},
}).then(async (res) => {
if (!res.ok) {
throw new Error(await res.text());
}
return res.json();
});
}
captureMultipleScreenshots();