-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathCritical-CSS-Detection.js
More file actions
329 lines (298 loc) · 10.8 KB
/
Critical-CSS-Detection.js
File metadata and controls
329 lines (298 loc) · 10.8 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Critical CSS Detection
// Analyzes render-blocking stylesheets, inline CSS budget, and loading strategy
// https://webperf-snippets.nucliweb.net
(() => {
const formatBytes = (bytes) => {
if (bytes === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toFixed(2) + " " + sizes[i];
};
const criticalBudget = 14 * 1024; // 14 KB (first TCP round-trip)
// --- External stylesheets ---
const linkStylesheets = Array.from(
document.querySelectorAll('link[rel="stylesheet"]')
);
const perfEntries = performance.getEntriesByType("resource");
const externalCSSData = linkStylesheets.map((link) => {
const href = link.href;
const media = link.getAttribute("media") || "all";
const perfEntry = perfEntries.find((e) => e.name === href);
// Prefer browser-reported render-blocking status (PerformanceResourceTiming API,
// Chrome 107+). This correctly handles the media="print" onload="this.media='all'"
// deferral pattern — by page load time the media attribute has already changed to
// "all", so a DOM-only check would produce false positives.
let isRenderBlocking;
if (perfEntry && perfEntry.renderBlockingStatus !== undefined) {
isRenderBlocking = perfEntry.renderBlockingStatus === "blocking";
} else {
// Fallback for browsers without renderBlockingStatus support.
// Note: may produce false positives when the media/onload deferral pattern is used.
isRenderBlocking =
media === "all" ||
media === "" ||
media === "screen" ||
(media.toLowerCase().includes("screen") &&
!media.toLowerCase().includes("print"));
}
const transferSize = perfEntry ? perfEntry.transferSize : 0;
const decodedSize = perfEntry ? perfEntry.decodedBodySize : 0;
const preloadLink = document.querySelector(
`link[rel="preload"][as="style"][href="${href}"]`
);
let ruleCount = 0;
let corsBlocked = false;
try {
const sheet = Array.from(document.styleSheets).find(
(s) => s.href === href
);
if (sheet && sheet.cssRules) ruleCount = sheet.cssRules.length;
} catch {
corsBlocked = true;
}
return {
filename: href.split("/").pop().split("?")[0] || href,
href,
media,
isRenderBlocking,
transferSize,
decodedSize,
ruleCount,
corsBlocked,
preloaded: preloadLink !== null,
};
});
// --- Inline styles ---
const inlineStyles = Array.from(document.querySelectorAll("style")).map(
(style, i) => {
const content = style.innerHTML;
const size = new Blob([content]).size;
const ruleCount = (content.match(/\{[^}]*\}/g) || []).length;
return {
index: i + 1,
size,
ruleCount,
inHead: style.closest("head") !== null,
};
}
);
// --- Totals ---
const renderBlockingCSS = externalCSSData.filter((s) => s.isRenderBlocking);
const nonBlockingCSS = externalCSSData.filter((s) => !s.isRenderBlocking);
const totalInlineBytes = inlineStyles.reduce((sum, s) => sum + s.size, 0);
const totalRenderBlockingDecoded = renderBlockingCSS.reduce(
(sum, s) => sum + s.decodedSize,
0
);
const totalExternalTransfer = externalCSSData.reduce(
(sum, s) => sum + s.transferSize,
0
);
// --- Output ---
console.group(
"%c🎯 Critical CSS Detection",
"font-weight: bold; font-size: 14px;"
);
console.log("");
console.log("%cPage CSS Summary:", "font-weight: bold;");
console.log(` External stylesheets: ${externalCSSData.length}`);
console.log(` Render-blocking: ${renderBlockingCSS.length}`);
console.log(` Non-blocking (media query): ${nonBlockingCSS.length}`);
console.log(` Inline <style> blocks: ${inlineStyles.length}`);
console.log(` Total external CSS (wire): ${formatBytes(totalExternalTransfer)}`);
console.log(` Total inline CSS: ${formatBytes(totalInlineBytes)}`);
// Render-blocking stylesheets
if (renderBlockingCSS.length > 0) {
console.log("");
console.group(
`%c🚫 Render-Blocking Stylesheets (${renderBlockingCSS.length})`,
"color: #ef4444; font-weight: bold;"
);
console.table(
renderBlockingCSS.map((s) => ({
File: s.filename,
Media: s.media,
"Transfer Size": formatBytes(s.transferSize),
"Decoded Size": formatBytes(s.decodedSize),
Rules: s.corsBlocked ? "CORS blocked" : s.ruleCount,
Preloaded: s.preloaded ? "✅ Yes" : "❌ No",
}))
);
const budgetPct = Math.min(
(totalRenderBlockingDecoded / criticalBudget) * 100,
200
);
const barWidth = Math.min(Math.round(budgetPct / 10), 20);
const bar = "█".repeat(barWidth) + "░".repeat(20 - barWidth);
console.log(
`\n Total render-blocking: ${formatBytes(totalRenderBlockingDecoded)}`
);
console.log(` vs. 14 KB budget: [${bar}] ${budgetPct.toFixed(0)}%`);
console.groupEnd();
} else {
console.log("");
console.log(
"%c✅ No render-blocking stylesheets detected.",
"color: #22c55e; font-weight: bold;"
);
}
// Non-blocking stylesheets
if (nonBlockingCSS.length > 0) {
console.log("");
console.group(
`%c✅ Non-Blocking Stylesheets (${nonBlockingCSS.length})`,
"color: #22c55e; font-weight: bold;"
);
console.table(
nonBlockingCSS.map((s) => ({
File: s.filename,
Media: s.media,
"Transfer Size": formatBytes(s.transferSize),
Rules: s.corsBlocked ? "CORS blocked" : s.ruleCount,
}))
);
console.groupEnd();
}
// Inline styles
if (inlineStyles.length > 0) {
console.log("");
const inlineBudgetPct = ((totalInlineBytes / criticalBudget) * 100).toFixed(0);
const inlineRating =
totalInlineBytes <= criticalBudget * 0.5
? { label: "Good", color: "#22c55e" }
: totalInlineBytes <= criticalBudget
? { label: "Acceptable", color: "#f59e0b" }
: { label: "Too Large", color: "#ef4444" };
console.group(
`%c📝 Inline CSS (${inlineStyles.length} block${inlineStyles.length > 1 ? "s" : ""})`,
"color: #3b82f6; font-weight: bold;"
);
console.log(` Total size: ${formatBytes(totalInlineBytes)}`);
console.log(` Budget used: ${inlineBudgetPct}% of 14 KB`);
console.log(
` Rating: %c${inlineRating.label}`,
`color: ${inlineRating.color}; font-weight: bold;`
);
console.table(
inlineStyles.map((s) => ({
"#": s.index,
Location: s.inHead ? "head" : "body",
Size: formatBytes(s.size),
"Rules (~)": s.ruleCount,
}))
);
console.groupEnd();
}
// Issues
const issues = [];
if (renderBlockingCSS.length > 0 && inlineStyles.length === 0) {
issues.push({
severity: "high",
message: "No critical CSS inlined — all CSS loads via render-blocking requests",
suggestion:
'Extract above-the-fold styles into a <style> tag in <head> and defer the rest',
});
}
if (totalRenderBlockingDecoded > criticalBudget) {
issues.push({
severity: "high",
message: `Render-blocking CSS (${formatBytes(totalRenderBlockingDecoded)}) exceeds the 14 KB critical budget`,
suggestion:
"Inline critical CSS and defer non-critical stylesheets using the media/onload pattern",
});
}
const largeUnpreloaded = renderBlockingCSS.filter(
(s) => !s.preloaded && s.transferSize > 10 * 1024
);
if (largeUnpreloaded.length > 0) {
issues.push({
severity: "medium",
message: `${largeUnpreloaded.length} large render-blocking stylesheet(s) without preload`,
details: largeUnpreloaded.map(
(s) => `• ${s.filename} (${formatBytes(s.transferSize)})`
),
suggestion: 'Add <link rel="preload" as="style" href="..."> to start download earlier',
});
}
if (totalInlineBytes > criticalBudget) {
issues.push({
severity: "high",
message: `Inline CSS (${formatBytes(totalInlineBytes)}) exceeds the 14 KB critical budget`,
suggestion:
"Keep inline critical CSS under 14 KB. Move non-critical styles to an external deferred file.",
});
}
const bodyInlineStyles = inlineStyles.filter((s) => !s.inHead);
if (bodyInlineStyles.length > 0) {
issues.push({
severity: "medium",
message: `${bodyInlineStyles.length} inline style block(s) found in <body>`,
suggestion:
"Move critical styles to <head> to avoid potential render-blocking reflows",
});
}
if (issues.length > 0) {
console.log("");
console.group("%c⚠️ Issues Found", "color: #f59e0b; font-weight: bold;");
issues.forEach((issue) => {
const icon =
issue.severity === "high" ? "🔴" : issue.severity === "medium" ? "🟡" : "🔵";
console.log(`\n${icon} ${issue.message}`);
if (issue.details) issue.details.forEach((d) => console.log(` ${d}`));
console.log(` → ${issue.suggestion}`);
});
console.groupEnd();
} else {
console.log("");
console.log(
"%c✅ No critical CSS issues detected.",
"color: #22c55e; font-weight: bold;"
);
}
// Best practices
console.log("");
console.group(
"%c📖 Critical CSS Best Practices",
"color: #3b82f6; font-weight: bold;"
);
console.log("");
console.log("• Inline critical above-the-fold CSS in <head> (target: < 14 KB)");
console.log("• Defer non-critical CSS:");
console.log(
' <link rel="stylesheet" href="styles.css" media="print" onload="this.media=\'all\'">'
);
console.log('• Preload critical external CSS: <link rel="preload" as="style" href="...">');
console.log("• Use Chrome DevTools Coverage tab (Ctrl+Shift+P → Coverage) to find unused CSS");
console.log("• Automate critical CSS extraction with tools like critical or Critters");
console.groupEnd();
console.groupEnd();
return {
script: "Critical-CSS-Detection",
status: "ok",
count: externalCSSData.length,
details: {
renderBlockingCount: renderBlockingCSS.length,
nonBlockingCount: nonBlockingCSS.length,
inlineCount: inlineStyles.length,
totalExternalTransferBytes: totalExternalTransfer,
renderBlockingDecodedBytes: totalRenderBlockingDecoded,
inlineBytes: totalInlineBytes,
criticalBudgetBytes: criticalBudget,
},
items: externalCSSData.map(s => ({
filename: s.filename,
media: s.media,
isRenderBlocking: s.isRenderBlocking,
transferBytes: s.transferSize,
decodedBytes: s.decodedSize,
ruleCount: s.ruleCount,
preloaded: s.preloaded,
corsBlocked: s.corsBlocked,
})),
issues: issues.map(i => ({
severity: i.severity === 'high' ? 'error' : 'warning',
message: i.message,
})),
};
})();