-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathSSR-Hydration-Data-Analysis.js
More file actions
364 lines (326 loc) Β· 11.9 KB
/
SSR-Hydration-Data-Analysis.js
File metadata and controls
364 lines (326 loc) Β· 11.9 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// SSR Framework Hydration Data Analysis
// 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];
};
// Framework detection patterns
const frameworks = [
{
name: "Next.js",
selector: "#__NEXT_DATA__",
type: "json",
threshold: 128 * 1024, // 128 KB official warning threshold
docs: "https://nextjs.org/docs/messages/large-page-data",
},
{
name: "Nuxt 3",
selector: "#__NUXT_DATA__",
type: "json-array",
threshold: 100 * 1024,
docs: "https://nuxt.com/docs/api/composables/use-hydration",
},
{
name: "Nuxt 2",
pattern: /window\.__NUXT__\s*=/,
type: "script",
threshold: 100 * 1024,
docs: "https://nuxtjs.org/",
},
{
name: "Remix",
selector: "#__remixContext",
type: "script",
threshold: 100 * 1024,
docs: "https://remix.run/docs/en/main/guides/performance",
},
{
name: "Gatsby",
selector: "#___gatsby",
pattern: /window\.___/,
type: "script",
threshold: 100 * 1024,
docs: "https://www.gatsbyjs.com/docs/",
},
{
name: "SvelteKit",
selector: "[data-sveltekit-hydrate]",
type: "json",
threshold: 100 * 1024,
docs: "https://kit.svelte.dev/docs/performance",
},
{
name: "Astro",
selector: "astro-island",
type: "props",
threshold: 50 * 1024,
docs: "https://docs.astro.build/en/concepts/islands/",
},
];
// Find all hydration scripts
const detected = [];
const allInlineScripts = Array.from(
document.querySelectorAll("script:not([src])")
).filter((s) => s.innerHTML.trim().length > 0);
// Check each framework
frameworks.forEach((fw) => {
let elements = [];
let content = "";
let size = 0;
if (fw.selector) {
const el = document.querySelector(fw.selector);
if (el) {
elements = [el];
content = el.innerHTML || el.textContent || "";
size = new Blob([content]).size;
}
}
if (fw.pattern && elements.length === 0) {
allInlineScripts.forEach((script) => {
if (fw.pattern.test(script.innerHTML)) {
elements.push(script);
content = script.innerHTML;
size += new Blob([content]).size;
}
});
}
// Special handling for Astro islands
if (fw.name === "Astro") {
const islands = document.querySelectorAll("astro-island");
if (islands.length > 0) {
elements = Array.from(islands);
let totalProps = 0;
islands.forEach((island) => {
const props = island.getAttribute("props");
if (props) totalProps += new Blob([props]).size;
});
size = totalProps;
content = `${islands.length} islands with props`;
}
}
if (elements.length > 0) {
detected.push({
...fw,
elements,
content,
size,
exceedsThreshold: size > fw.threshold,
});
}
});
// Calculate other inline scripts
const frameworkScripts = new Set(detected.flatMap((d) => d.elements));
const otherScripts = allInlineScripts.filter((s) => !frameworkScripts.has(s));
const otherSize = otherScripts.reduce(
(sum, s) => sum + new Blob([s.innerHTML]).size,
0
);
// Display results
console.group(
"%cπ SSR Framework Hydration Analysis",
"font-weight: bold; font-size: 14px;"
);
if (detected.length === 0) {
console.log("");
console.log(
"%cπ No SSR framework hydration data detected.",
"color: #6b7280; font-weight: bold;"
);
console.log("This page may be:");
console.log(" β’ A static HTML page");
console.log(" β’ A client-side rendered SPA");
console.log(" β’ Using a framework not yet supported by this snippet");
console.log("");
console.log(
`Found ${otherScripts.length} other inline scripts (${formatBytes(otherSize)})`
);
console.groupEnd();
return { script: "SSR-Hydration-Data-Analysis", status: "ok", count: 0, details: { frameworksFound: 0, totalHydrationBytes: 0, otherInlineBytes: otherSize }, items: [], issues: [] };
}
// Summary
console.log("");
console.log("%cDetected Framework(s):", "font-weight: bold;");
detected.forEach((fw) => {
const status = fw.exceedsThreshold ? "π΄" : "π’";
console.log(
` ${status} ${fw.name}: ${formatBytes(fw.size)} (threshold: ${formatBytes(fw.threshold)})`
);
});
const totalHydrationSize = detected.reduce((sum, d) => sum + d.size, 0);
console.log("");
console.log(` Total hydration data: ${formatBytes(totalHydrationSize)}`);
console.log(` Other inline scripts: ${formatBytes(otherSize)}`);
// Detailed analysis for each framework
detected.forEach((fw) => {
console.log("");
console.group(
`%c${fw.exceedsThreshold ? "π΄" : "π’"} ${fw.name} Analysis`,
`color: ${fw.exceedsThreshold ? "#ef4444" : "#22c55e"}; font-weight: bold;`
);
console.log(`Size: ${formatBytes(fw.size)}`);
console.log(`Threshold: ${formatBytes(fw.threshold)}`);
console.log(
`Status: ${fw.exceedsThreshold ? "β οΈ Exceeds recommended limit" : "β
Within limits"}`
);
// Parse and analyze content
if (fw.type === "json" && fw.content) {
try {
const data = JSON.parse(fw.content);
console.log("");
console.log("%cData Structure:", "font-weight: bold;");
if (fw.name === "Next.js" && data.props) {
const pageProps = data.props?.pageProps || {};
const pagePropsSize = new Blob([JSON.stringify(pageProps)]).size;
console.log(` Build ID: ${data.buildId || "N/A"}`);
console.log(` Page: ${data.page || "N/A"}`);
console.log(` pageProps size: ${formatBytes(pagePropsSize)}`);
// Analyze pageProps keys
const propsKeys = Object.keys(pageProps);
if (propsKeys.length > 0) {
console.log("");
console.log("%c pageProps breakdown:", "font-weight: bold;");
const propSizes = propsKeys.map((key) => ({
key,
size: new Blob([JSON.stringify(pageProps[key])]).size,
}));
propSizes.sort((a, b) => b.size - a.size);
propSizes.slice(0, 10).forEach((prop) => {
const pct = ((prop.size / pagePropsSize) * 100).toFixed(1);
const bar = "β".repeat(Math.min(Math.round(parseFloat(pct) / 5), 20));
console.log(
` ${prop.key}: ${formatBytes(prop.size)} (${pct}%) ${bar}`
);
});
if (propsKeys.length > 10) {
console.log(` ... and ${propsKeys.length - 10} more props`);
}
}
// Check for common issues
console.log("");
console.log("%cPotential Issues:", "font-weight: bold;");
// Large arrays
const largeArrays = propsKeys.filter((key) => {
const val = pageProps[key];
return Array.isArray(val) && val.length > 50;
});
if (largeArrays.length > 0) {
console.log(
` β οΈ Large arrays: ${largeArrays.join(", ")} (consider pagination)`
);
}
// Deeply nested objects
const checkDepth = (obj, depth = 0) => {
if (depth > 5) return true;
if (typeof obj !== "object" || obj === null) return false;
return Object.values(obj).some((v) => checkDepth(v, depth + 1));
};
if (checkDepth(pageProps)) {
console.log(" β οΈ Deeply nested data (> 5 levels)");
}
// Potential sensitive data patterns
const sensitivePatterns = /password|secret|token|apikey|api_key|private/i;
const propsString = JSON.stringify(pageProps);
if (sensitivePatterns.test(propsString)) {
console.log(
" π¨ Possible sensitive data detected - review prop names"
);
}
if (
largeArrays.length === 0 &&
!checkDepth(pageProps) &&
!sensitivePatterns.test(propsString)
) {
console.log(" β
No obvious issues detected");
}
}
// Raw data reference
console.log("");
console.log("%cπ¦ Raw data object:", "font-weight: bold;");
console.log(data);
} catch {
console.log("Could not parse JSON content");
}
}
if (fw.name === "Astro") {
console.log("");
console.log(`Islands found: ${fw.elements.length}`);
const islandTable = fw.elements.map((island, i) => {
const props = island.getAttribute("props");
const clientDirective =
island.getAttribute("client") ||
Array.from(island.attributes)
.find((a) => a.name.startsWith("client:"))
?.name.replace("client:", "") ||
"unknown";
return {
"#": i + 1,
Component: island.getAttribute("component-url")?.split("/").pop() || "Unknown",
Client: clientDirective,
"Props Size": props ? formatBytes(new Blob([props]).size) : "0 B",
};
});
console.table(islandTable);
}
// Element reference
console.log("");
console.log("%cπ Element(s):", "font-weight: bold;");
fw.elements.forEach((el, i) => {
console.log(`${i + 1}.`, el);
});
console.log("");
console.log(`π Docs: ${fw.docs}`);
console.groupEnd();
});
// Recommendations
const hasIssues = detected.some((d) => d.exceedsThreshold);
if (hasIssues) {
console.log("");
console.group("%cπ Recommendations", "color: #3b82f6; font-weight: bold;");
console.log("");
console.log("%cTo reduce hydration data size:", "font-weight: bold;");
console.log("");
console.log("1. Only fetch data you actually render");
console.log(" β Remove unused fields from API responses");
console.log(" β Use GraphQL or tRPC for precise data fetching");
console.log("");
console.log("2. Paginate large lists");
console.log(" β Don't send 100+ items in initial props");
console.log(" β Implement infinite scroll or pagination");
console.log("");
console.log("3. Defer non-critical data");
console.log(" β Fetch some data client-side after hydration");
console.log(" β Use React Query, SWR, or similar");
console.log("");
console.log("4. Transform data before sending");
console.log(" β Remove unnecessary nested data");
console.log(" β Flatten structures where possible");
console.log("");
const nextJs = detected.find((d) => d.name === "Next.js");
if (nextJs) {
console.log("%cNext.js specific:", "font-weight: bold;");
console.log(
'%c// In getServerSideProps or getStaticProps:\nreturn {\n props: {\n // Only include what the page renders\n items: items.slice(0, 10), // Paginate\n // Omit unused fields\n user: { name: user.name, avatar: user.avatar },\n },\n};',
"font-family: monospace; color: #22c55e;"
);
}
console.groupEnd();
}
console.groupEnd();
return {
script: "SSR-Hydration-Data-Analysis",
status: "ok",
count: detected.length,
details: {
frameworksFound: detected.length,
totalHydrationBytes: totalHydrationSize,
otherInlineBytes: otherSize,
hasExceedingThreshold: detected.some(d => d.exceedsThreshold),
},
items: detected.map(fw => ({ name: fw.name, sizeBytes: fw.size, thresholdBytes: fw.threshold, exceedsThreshold: fw.exceedsThreshold })),
issues: detected.filter(fw => fw.exceedsThreshold).map(fw => ({ severity: "warning", message: `${fw.name} hydration data (${Math.round(fw.size / 1024)} KB) exceeds ${Math.round(fw.threshold / 1024)} KB threshold` })),
};
})();