Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 64x 64x 64x 64x 26x 64x 21x 38x 17x 17x 64x 1x 1x 1x 1x 27x 27x 26x 26x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 8x 32x 32x 17x 7x 32x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 10x 10x 10x 10x 4x 4x 4x 6x 6x 6x 6x 6x 10x 3x 3x 3x 3x 3x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 3x 3x 3x 1x 3x 1x 3x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 21x 21x 2x 2x 21x 3x 3x 5x 13x 13x 1x 1x 13x 2x 2x 2x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 6x | /**
* Web Vitals Calculation Utilities
*
* Functions for calculating and categorizing Core Web Vitals metrics.
*/
/**
* Web Vitals metric names
*/
export type WebVitalName = 'LCP' | 'CLS' | 'FID' | 'INP' | 'FCP' | 'TTFB';
/**
* Web Vitals rating categories
*/
export type WebVitalRating = 'good' | 'needs-improvement' | 'poor';
/**
* Thresholds for each Web Vital metric
* Based on Google's Core Web Vitals recommendations
*/
export const WEB_VITALS_THRESHOLDS: Record<
WebVitalName,
{ good: number; needsImprovement: number; unit: string; label: string }
> = {
LCP: { good: 2500, needsImprovement: 4000, unit: 'ms', label: 'Largest Contentful Paint' },
CLS: { good: 0.1, needsImprovement: 0.25, unit: '', label: 'Cumulative Layout Shift' },
FID: { good: 100, needsImprovement: 300, unit: 'ms', label: 'First Input Delay' },
INP: { good: 200, needsImprovement: 500, unit: 'ms', label: 'Interaction to Next Paint' },
FCP: { good: 1800, needsImprovement: 3000, unit: 'ms', label: 'First Contentful Paint' },
TTFB: { good: 800, needsImprovement: 1800, unit: 'ms', label: 'Time to First Byte' },
};
/**
* Get the rating for a Web Vital value
*/
export function getWebVitalRating(metric: WebVitalName, value: number): WebVitalRating {
const thresholds = WEB_VITALS_THRESHOLDS[metric];
if (value <= thresholds.good) {
return 'good';
} else if (value <= thresholds.needsImprovement) {
return 'needs-improvement';
} else {
return 'poor';
}
}
/**
* Calculate percentile from sorted array of values
*/
export function calculatePercentile(sortedValues: number[], percentile: number): number {
if (sortedValues.length === 0) return 0;
const index = Math.ceil((percentile / 100) * sortedValues.length) - 1;
return sortedValues[Math.max(0, index)];
}
/**
* Aggregated Web Vital statistics
*/
export interface WebVitalStats {
metric: WebVitalName;
label: string;
unit: string;
p50: number;
p75: number;
p95: number;
good: number;
needsImprovement: number;
poor: number;
total: number;
rating: WebVitalRating;
}
/**
* Calculate statistics for a Web Vital metric
*/
export function calculateWebVitalStats(
metric: WebVitalName,
values: number[]
): WebVitalStats {
const thresholds = WEB_VITALS_THRESHOLDS[metric];
if (values.length === 0) {
return {
metric,
label: thresholds.label,
unit: thresholds.unit,
p50: 0,
p75: 0,
p95: 0,
good: 0,
needsImprovement: 0,
poor: 0,
total: 0,
rating: 'good',
};
}
// Sort values for percentile calculation
const sorted = [...values].sort((a, b) => a - b);
// Calculate percentiles
const p50 = calculatePercentile(sorted, 50);
const p75 = calculatePercentile(sorted, 75);
const p95 = calculatePercentile(sorted, 95);
// Count by rating
let good = 0;
let needsImprovement = 0;
let poor = 0;
for (const value of values) {
const rating = getWebVitalRating(metric, value);
if (rating === 'good') good++;
else if (rating === 'needs-improvement') needsImprovement++;
else poor++;
}
// Overall rating based on p75 (Google's standard)
const overallRating = getWebVitalRating(metric, p75);
return {
metric,
label: thresholds.label,
unit: thresholds.unit,
p50,
p75,
p95,
good,
needsImprovement,
poor,
total: values.length,
rating: overallRating,
};
}
/**
* Format a Web Vital value for display
*/
export function formatWebVitalValue(metric: WebVitalName, value: number): string {
const thresholds = WEB_VITALS_THRESHOLDS[metric];
if (metric === 'CLS') {
// CLS is unitless, show 2 decimal places
return value.toFixed(2);
}
// Round to whole number for ms values
const rounded = Math.round(value);
// Format with thousand separators for large values
if (rounded >= 1000) {
return `${(rounded / 1000).toFixed(1)}${thresholds.unit === 'ms' ? 's' : thresholds.unit}`;
}
return `${rounded}${thresholds.unit}`;
}
/**
* Get rating color class for Tailwind
*/
export function getRatingColorClass(rating: WebVitalRating): {
bg: string;
text: string;
border: string;
progress: string;
} {
switch (rating) {
case 'good':
return {
bg: 'bg-green-100 dark:bg-green-900/30',
text: 'text-green-800 dark:text-green-300',
border: 'border-green-500',
progress: 'bg-green-500',
};
case 'needs-improvement':
return {
bg: 'bg-yellow-100 dark:bg-yellow-900/30',
text: 'text-yellow-800 dark:text-yellow-300',
border: 'border-yellow-500',
progress: 'bg-yellow-500',
};
case 'poor':
return {
bg: 'bg-red-100 dark:bg-red-900/30',
text: 'text-red-800 dark:text-red-300',
border: 'border-red-500',
progress: 'bg-red-500',
};
}
}
/**
* Get rating label
*/
export function getRatingLabel(rating: WebVitalRating): string {
switch (rating) {
case 'good':
return 'Good';
case 'needs-improvement':
return 'Needs Improvement';
case 'poor':
return 'Poor';
}
}
/**
* Page-level Web Vitals data
*/
export interface PageWebVitals {
url: string;
metrics: Partial<Record<WebVitalName, WebVitalStats>>;
worstMetric?: {
name: WebVitalName;
rating: WebVitalRating;
value: number;
};
}
/**
* Identify the worst performing metric for a page
*/
export function getWorstMetric(
metrics: Partial<Record<WebVitalName, WebVitalStats>>
): PageWebVitals['worstMetric'] | undefined {
const metricPriority: WebVitalName[] = ['LCP', 'CLS', 'INP', 'FID', 'FCP', 'TTFB'];
// First check for poor metrics
for (const name of metricPriority) {
const stats = metrics[name];
if (stats && stats.rating === 'poor') {
return { name, rating: 'poor', value: stats.p75 };
}
}
// Then check for needs-improvement
for (const name of metricPriority) {
const stats = metrics[name];
if (stats && stats.rating === 'needs-improvement') {
return { name, rating: 'needs-improvement', value: stats.p75 };
}
}
return undefined;
}
/**
* Extract normalized page path from URL
*/
export function normalizePagePath(url: string): string {
try {
const parsed = new URL(url);
// Replace dynamic segments like IDs with placeholders
return parsed.pathname
// UUID format: 8-4-4-4-12 hex characters with dashes
.replace(/\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/gi, '/[uuid]')
// Long alphanumeric IDs (20+ chars)
.replace(/\/[a-z0-9]{20,}/gi, '/[id]')
// Numeric IDs
.replace(/\/\d+/g, '/[id]');
} catch {
return url;
}
}
|