Skip to content

Commit ae374a6

Browse files
committed
fix(cache): handle empty Performance API buffer gracefully
Adds an early exit with an explanatory warning when no resources are found, and guards the cache efficiency recommendation against a false positive when entries.length is zero.
1 parent 11dfa79 commit ae374a6

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

snippets/Loading/Cache-Strategy-Analysis.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,15 @@
424424

425425
// --- Phase 7: Console output ---
426426

427+
if (entries.length === 0) {
428+
console.group("%c🔍 Cache Strategy Analysis", "font-weight: bold; font-size: 14px;");
429+
console.warn("⚠️ No resources found in the Performance API buffer.");
430+
console.log(" This usually means the site called performance.clearResourceTimings() to manage memory.");
431+
console.log(" Try: reload the page and run this snippet immediately after load.");
432+
console.groupEnd();
433+
return { script: "Cache-Strategy-Analysis", status: "no-data", count: 0 };
434+
}
435+
427436
const allAntiPatterns = entries.flatMap((e) =>
428437
e.antiPatterns.map((ap) => ({ ...ap, resource: e.shortName, url: e.url }))
429438
);
@@ -703,7 +712,7 @@
703712
"🟡 Replace Expires headers with Cache-Control: max-age — Expires is an outdated mechanism and less reliable."
704713
);
705714
}
706-
if (cacheEfficiencyPercent < 50) {
715+
if (entries.length > 0 && cacheEfficiencyPercent < 50) {
707716
recommendations.push(
708717
`🔴 Cache efficiency is low (${cacheEfficiencyPercent}%). Review caching strategy for all static assets.`
709718
);

0 commit comments

Comments
 (0)