@rankcli/agent-runtime 0.0.8 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -196
- package/dist/analyzer-GMURJADU.mjs +7 -0
- package/dist/chunk-2JADKV3Z.mjs +244 -0
- package/dist/chunk-3ZSCLNTW.mjs +557 -0
- package/dist/chunk-4E4MQOSP.mjs +374 -0
- package/dist/chunk-6BWS3CLP.mjs +16 -0
- package/dist/chunk-AK2IC22C.mjs +206 -0
- package/dist/chunk-K6VSXDD6.mjs +293 -0
- package/dist/chunk-M27NQCWW.mjs +303 -0
- package/dist/{chunk-YNZYHEYM.mjs → chunk-PJLNXOLN.mjs} +0 -14
- package/dist/chunk-VSQD74I7.mjs +474 -0
- package/dist/core-web-vitals-analyzer-TE6LQJMS.mjs +7 -0
- package/dist/geo-analyzer-D47LTMMA.mjs +25 -0
- package/dist/image-optimization-analyzer-XP4OQGRP.mjs +9 -0
- package/dist/index.d.mts +1523 -17
- package/dist/index.d.ts +1523 -17
- package/dist/index.js +9582 -2664
- package/dist/index.mjs +4812 -380
- package/dist/internal-linking-analyzer-MRMBV7NM.mjs +9 -0
- package/dist/mobile-seo-analyzer-67HNQ7IO.mjs +7 -0
- package/dist/security-headers-analyzer-3ZUQARS5.mjs +9 -0
- package/dist/structured-data-analyzer-2I4NQAUP.mjs +9 -0
- package/package.json +2 -2
- package/src/analyzers/core-web-vitals-analyzer.test.ts +236 -0
- package/src/analyzers/core-web-vitals-analyzer.ts +557 -0
- package/src/analyzers/geo-analyzer.test.ts +310 -0
- package/src/analyzers/geo-analyzer.ts +814 -0
- package/src/analyzers/image-optimization-analyzer.test.ts +145 -0
- package/src/analyzers/image-optimization-analyzer.ts +348 -0
- package/src/analyzers/index.ts +233 -0
- package/src/analyzers/internal-linking-analyzer.test.ts +141 -0
- package/src/analyzers/internal-linking-analyzer.ts +419 -0
- package/src/analyzers/mobile-seo-analyzer.test.ts +140 -0
- package/src/analyzers/mobile-seo-analyzer.ts +455 -0
- package/src/analyzers/security-headers-analyzer.test.ts +115 -0
- package/src/analyzers/security-headers-analyzer.ts +318 -0
- package/src/analyzers/structured-data-analyzer.test.ts +210 -0
- package/src/analyzers/structured-data-analyzer.ts +590 -0
- package/src/audit/engine.ts +3 -3
- package/src/audit/types.ts +3 -2
- package/src/fixer/framework-fixes.test.ts +489 -0
- package/src/fixer/framework-fixes.ts +3418 -0
- package/src/fixer/index.ts +1 -0
- package/src/fixer/schemas.ts +971 -0
- package/src/frameworks/detector.ts +642 -114
- package/src/frameworks/suggestion-engine.ts +38 -1
- package/src/index.ts +6 -0
- package/src/types.ts +15 -1
- package/dist/analyzer-2CSWIQGD.mjs +0 -6
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core Web Vitals Analyzer
|
|
3
|
+
*
|
|
4
|
+
* Estimates Core Web Vitals from HTML analysis without actual page load.
|
|
5
|
+
* Identifies common issues that impact LCP, FID, CLS, INP, and TTFB.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as cheerio from 'cheerio';
|
|
9
|
+
import type { AuditIssue } from '../audit/types.js';
|
|
10
|
+
|
|
11
|
+
export interface CoreWebVitalsEstimate {
|
|
12
|
+
lcp: {
|
|
13
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
14
|
+
issues: string[];
|
|
15
|
+
recommendations: string[];
|
|
16
|
+
};
|
|
17
|
+
fid: {
|
|
18
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
19
|
+
issues: string[];
|
|
20
|
+
recommendations: string[];
|
|
21
|
+
};
|
|
22
|
+
cls: {
|
|
23
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
24
|
+
issues: string[];
|
|
25
|
+
recommendations: string[];
|
|
26
|
+
};
|
|
27
|
+
inp: {
|
|
28
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
29
|
+
issues: string[];
|
|
30
|
+
recommendations: string[];
|
|
31
|
+
};
|
|
32
|
+
ttfb: {
|
|
33
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
34
|
+
issues: string[];
|
|
35
|
+
recommendations: string[];
|
|
36
|
+
};
|
|
37
|
+
overallScore: number;
|
|
38
|
+
issues: AuditIssue[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Analyze HTML for LCP (Largest Contentful Paint) issues
|
|
43
|
+
*/
|
|
44
|
+
function analyzeLCP(html: string, $: cheerio.CheerioAPI): {
|
|
45
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
46
|
+
issues: string[];
|
|
47
|
+
recommendations: string[];
|
|
48
|
+
} {
|
|
49
|
+
const issues: string[] = [];
|
|
50
|
+
const recommendations: string[] = [];
|
|
51
|
+
let score = 100;
|
|
52
|
+
|
|
53
|
+
// Check for hero images without optimization
|
|
54
|
+
const heroImages = $('img').filter((_, el) => {
|
|
55
|
+
const parent = $(el).parent();
|
|
56
|
+
const isHero = parent.is('header, .hero, [class*="hero"], [class*="banner"], section:first-of-type');
|
|
57
|
+
const isLarge = (parseInt($(el).attr('width') || '0') > 400) ||
|
|
58
|
+
($(el).attr('class')?.includes('full') ?? false);
|
|
59
|
+
return isHero || isLarge;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
heroImages.each((_, img) => {
|
|
63
|
+
const $img = $(img);
|
|
64
|
+
const src = $img.attr('src') || '';
|
|
65
|
+
|
|
66
|
+
// Check loading attribute
|
|
67
|
+
if ($img.attr('loading') === 'lazy') {
|
|
68
|
+
issues.push('Hero/LCP image has loading="lazy" - delays rendering');
|
|
69
|
+
score -= 20;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Check for preload
|
|
73
|
+
const preloads = $('link[rel="preload"][as="image"]');
|
|
74
|
+
const isPreloaded = preloads.toArray().some(p =>
|
|
75
|
+
$(p).attr('href')?.includes(src.split('/').pop() || '')
|
|
76
|
+
);
|
|
77
|
+
if (!isPreloaded && src) {
|
|
78
|
+
issues.push('Hero image not preloaded');
|
|
79
|
+
recommendations.push(`Add: <link rel="preload" as="image" href="${src}">`);
|
|
80
|
+
score -= 15;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Check for width/height attributes (prevents layout shift)
|
|
84
|
+
if (!$img.attr('width') || !$img.attr('height')) {
|
|
85
|
+
issues.push('Hero image missing width/height attributes');
|
|
86
|
+
score -= 10;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Check for modern formats
|
|
90
|
+
const format = src.split('.').pop()?.toLowerCase();
|
|
91
|
+
if (format && !['webp', 'avif'].includes(format)) {
|
|
92
|
+
recommendations.push('Consider using WebP or AVIF format for hero image');
|
|
93
|
+
score -= 5;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Check for render-blocking resources
|
|
98
|
+
const blockingCSS = $('link[rel="stylesheet"]').filter((_, el) => {
|
|
99
|
+
const media = $(el).attr('media');
|
|
100
|
+
return !media || media === 'all' || media === 'screen';
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (blockingCSS.length > 3) {
|
|
104
|
+
issues.push(`${blockingCSS.length} render-blocking stylesheets`);
|
|
105
|
+
recommendations.push('Inline critical CSS and defer non-critical stylesheets');
|
|
106
|
+
score -= 15;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Check for blocking scripts in head
|
|
110
|
+
const blockingScripts = $('head script:not([async]):not([defer]):not([type="module"])').filter((_, el) => {
|
|
111
|
+
return $(el).attr('src') !== undefined;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
if (blockingScripts.length > 0) {
|
|
115
|
+
issues.push(`${blockingScripts.length} render-blocking scripts in <head>`);
|
|
116
|
+
recommendations.push('Add async or defer attribute to scripts');
|
|
117
|
+
score -= 20;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Check for web fonts
|
|
121
|
+
const fontLinks = $('link[rel="preload"][as="font"], link[href*="fonts.googleapis.com"], link[href*="fonts.gstatic.com"]');
|
|
122
|
+
const fontPreconnects = $('link[rel="preconnect"][href*="fonts"]');
|
|
123
|
+
|
|
124
|
+
if (fontLinks.length > 0 && fontPreconnects.length === 0) {
|
|
125
|
+
issues.push('Web fonts without preconnect');
|
|
126
|
+
recommendations.push('Add <link rel="preconnect" href="https://fonts.googleapis.com">');
|
|
127
|
+
score -= 10;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Check for large inline styles
|
|
131
|
+
const inlineStyles = $('style').text();
|
|
132
|
+
if (inlineStyles.length > 50000) {
|
|
133
|
+
issues.push('Very large inline CSS (>50KB)');
|
|
134
|
+
score -= 10;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown' = 'unknown';
|
|
138
|
+
if (score >= 80) estimate = 'good';
|
|
139
|
+
else if (score >= 50) estimate = 'needs-improvement';
|
|
140
|
+
else estimate = 'poor';
|
|
141
|
+
|
|
142
|
+
return { estimate, issues, recommendations };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Analyze HTML for FID (First Input Delay) / INP issues
|
|
147
|
+
*/
|
|
148
|
+
function analyzeFID_INP(html: string, $: cheerio.CheerioAPI): {
|
|
149
|
+
fidEstimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
150
|
+
inpEstimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
151
|
+
issues: string[];
|
|
152
|
+
recommendations: string[];
|
|
153
|
+
} {
|
|
154
|
+
const issues: string[] = [];
|
|
155
|
+
const recommendations: string[] = [];
|
|
156
|
+
let score = 100;
|
|
157
|
+
|
|
158
|
+
// Count total JavaScript size (approximate from script tags)
|
|
159
|
+
let totalJSIndicators = 0;
|
|
160
|
+
|
|
161
|
+
// Check for large JS bundles
|
|
162
|
+
$('script[src]').each((_, el) => {
|
|
163
|
+
const src = $(el).attr('src') || '';
|
|
164
|
+
if (src.includes('bundle') || src.includes('vendor') || src.includes('chunk')) {
|
|
165
|
+
totalJSIndicators++;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
if (totalJSIndicators > 5) {
|
|
170
|
+
issues.push('Many JavaScript bundle files detected');
|
|
171
|
+
recommendations.push('Consider code splitting and lazy loading');
|
|
172
|
+
score -= 15;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Check for heavy frameworks
|
|
176
|
+
const hasHeavyFramework = html.includes('angular') ||
|
|
177
|
+
html.includes('__NUXT__') ||
|
|
178
|
+
(html.includes('react') && !html.includes('preact'));
|
|
179
|
+
|
|
180
|
+
// Check for inline scripts (potential long tasks)
|
|
181
|
+
const inlineScriptContent = $('script:not([src])').text();
|
|
182
|
+
if (inlineScriptContent.length > 10000) {
|
|
183
|
+
issues.push('Large inline JavaScript (>10KB)');
|
|
184
|
+
recommendations.push('Move large scripts to external files with async/defer');
|
|
185
|
+
score -= 15;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Check for event handlers in HTML
|
|
189
|
+
const inlineHandlers = $('[onclick], [onchange], [onsubmit], [onload], [onerror]');
|
|
190
|
+
if (inlineHandlers.length > 10) {
|
|
191
|
+
issues.push(`${inlineHandlers.length} inline event handlers`);
|
|
192
|
+
recommendations.push('Use addEventListener instead of inline handlers');
|
|
193
|
+
score -= 10;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Check for third-party scripts
|
|
197
|
+
const thirdPartyScripts = $('script[src]').filter((_, el) => {
|
|
198
|
+
const src = $(el).attr('src') || '';
|
|
199
|
+
return src.includes('//') && !src.includes('localhost');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const heavyThirdParties = ['analytics', 'facebook', 'twitter', 'linkedin', 'hotjar', 'intercom', 'drift', 'hubspot'];
|
|
203
|
+
let heavyScriptCount = 0;
|
|
204
|
+
|
|
205
|
+
thirdPartyScripts.each((_, el) => {
|
|
206
|
+
const src = $(el).attr('src') || '';
|
|
207
|
+
if (heavyThirdParties.some(tp => src.toLowerCase().includes(tp))) {
|
|
208
|
+
heavyScriptCount++;
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
if (heavyScriptCount > 3) {
|
|
213
|
+
issues.push(`${heavyScriptCount} heavy third-party scripts`);
|
|
214
|
+
recommendations.push('Lazy load third-party scripts after page interaction');
|
|
215
|
+
score -= 20;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Check for synchronous XHR patterns
|
|
219
|
+
if (html.includes('XMLHttpRequest') && !html.includes('async')) {
|
|
220
|
+
issues.push('Potential synchronous XHR detected');
|
|
221
|
+
score -= 10;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown' = 'unknown';
|
|
225
|
+
if (score >= 80) estimate = 'good';
|
|
226
|
+
else if (score >= 50) estimate = 'needs-improvement';
|
|
227
|
+
else estimate = 'poor';
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
fidEstimate: estimate,
|
|
231
|
+
inpEstimate: estimate, // Similar factors affect both
|
|
232
|
+
issues,
|
|
233
|
+
recommendations
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Analyze HTML for CLS (Cumulative Layout Shift) issues
|
|
239
|
+
*/
|
|
240
|
+
function analyzeCLS(html: string, $: cheerio.CheerioAPI): {
|
|
241
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
242
|
+
issues: string[];
|
|
243
|
+
recommendations: string[];
|
|
244
|
+
} {
|
|
245
|
+
const issues: string[] = [];
|
|
246
|
+
const recommendations: string[] = [];
|
|
247
|
+
let score = 100;
|
|
248
|
+
|
|
249
|
+
// Check images without dimensions
|
|
250
|
+
const imagesWithoutDimensions = $('img').filter((_, el) => {
|
|
251
|
+
const width = $(el).attr('width');
|
|
252
|
+
const height = $(el).attr('height');
|
|
253
|
+
const style = $(el).attr('style') || '';
|
|
254
|
+
const hasStyleDimensions = style.includes('width') && style.includes('height');
|
|
255
|
+
return !width || !height || (!hasStyleDimensions && width === 'auto');
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
if (imagesWithoutDimensions.length > 0) {
|
|
259
|
+
issues.push(`${imagesWithoutDimensions.length} images without width/height`);
|
|
260
|
+
recommendations.push('Add explicit width and height attributes to all images');
|
|
261
|
+
score -= Math.min(imagesWithoutDimensions.length * 5, 25);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Check videos/iframes without dimensions
|
|
265
|
+
const mediaWithoutDimensions = $('video, iframe').filter((_, el) => {
|
|
266
|
+
const width = $(el).attr('width');
|
|
267
|
+
const height = $(el).attr('height');
|
|
268
|
+
return !width || !height;
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
if (mediaWithoutDimensions.length > 0) {
|
|
272
|
+
issues.push(`${mediaWithoutDimensions.length} videos/iframes without dimensions`);
|
|
273
|
+
recommendations.push('Add explicit width and height to all video and iframe elements');
|
|
274
|
+
score -= Math.min(mediaWithoutDimensions.length * 10, 20);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Check for ads (common CLS culprit)
|
|
278
|
+
const adContainers = $('[class*="ad-"], [class*="ads-"], [id*="ad-"], [data-ad], ins.adsbygoogle');
|
|
279
|
+
if (adContainers.length > 0) {
|
|
280
|
+
const hasReservedSpace = adContainers.filter((_, el) => {
|
|
281
|
+
const style = $(el).attr('style') || '';
|
|
282
|
+
return style.includes('min-height') || style.includes('height');
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
if (hasReservedSpace.length < adContainers.length) {
|
|
286
|
+
issues.push('Ad containers without reserved space');
|
|
287
|
+
recommendations.push('Reserve space for ads with min-height CSS');
|
|
288
|
+
score -= 20;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Check for dynamic content insertion points
|
|
293
|
+
const dynamicContainers = $('[data-loading], [class*="skeleton"], [class*="placeholder"]');
|
|
294
|
+
if (dynamicContainers.length > 0) {
|
|
295
|
+
// This is actually good - they're using skeletons
|
|
296
|
+
score += 5;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Check for web fonts with potential FOUT
|
|
300
|
+
const webFonts = $('link[href*="fonts"], style').filter((_, el) => {
|
|
301
|
+
const content = $(el).attr('href') || $(el).text();
|
|
302
|
+
return content.includes('font-face') || content.includes('fonts.googleapis');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
if (webFonts.length > 0) {
|
|
306
|
+
const hasFontDisplay = html.includes('font-display');
|
|
307
|
+
if (!hasFontDisplay) {
|
|
308
|
+
issues.push('Web fonts without font-display property');
|
|
309
|
+
recommendations.push('Add font-display: swap or optional to prevent FOUT');
|
|
310
|
+
score -= 10;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Check for fixed headers/footers that might cause shifts
|
|
315
|
+
const stickyElements = $('[class*="sticky"], [class*="fixed"], [style*="position: fixed"], [style*="position: sticky"]');
|
|
316
|
+
// This is informational - sticky headers are common and acceptable
|
|
317
|
+
|
|
318
|
+
// Check for aspect ratio containers
|
|
319
|
+
const aspectRatioContainers = $('[style*="aspect-ratio"], [class*="aspect-"]');
|
|
320
|
+
if (aspectRatioContainers.length > 0) {
|
|
321
|
+
score += 5; // Good practice
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown' = 'unknown';
|
|
325
|
+
if (score >= 80) estimate = 'good';
|
|
326
|
+
else if (score >= 50) estimate = 'needs-improvement';
|
|
327
|
+
else estimate = 'poor';
|
|
328
|
+
|
|
329
|
+
return { estimate, issues, recommendations };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Analyze for TTFB (Time to First Byte) indicators
|
|
334
|
+
*/
|
|
335
|
+
function analyzeTTFB($: cheerio.CheerioAPI, headers?: Record<string, string>): {
|
|
336
|
+
estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown';
|
|
337
|
+
issues: string[];
|
|
338
|
+
recommendations: string[];
|
|
339
|
+
} {
|
|
340
|
+
const issues: string[] = [];
|
|
341
|
+
const recommendations: string[] = [];
|
|
342
|
+
let score = 100;
|
|
343
|
+
|
|
344
|
+
// Check for caching headers (if provided)
|
|
345
|
+
if (headers) {
|
|
346
|
+
const cacheControl = headers['cache-control'];
|
|
347
|
+
if (!cacheControl) {
|
|
348
|
+
issues.push('No Cache-Control header');
|
|
349
|
+
recommendations.push('Add Cache-Control header for static assets');
|
|
350
|
+
score -= 10;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const hasCompression = headers['content-encoding']?.includes('gzip') ||
|
|
354
|
+
headers['content-encoding']?.includes('br');
|
|
355
|
+
if (!hasCompression) {
|
|
356
|
+
issues.push('Response not compressed (no gzip/brotli)');
|
|
357
|
+
recommendations.push('Enable Brotli or Gzip compression');
|
|
358
|
+
score -= 15;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Check for hints that suggest server-side processing
|
|
363
|
+
const hasDynamicIndicators = $('[data-server-rendered], [data-user-id], [data-session]').length > 0;
|
|
364
|
+
if (hasDynamicIndicators) {
|
|
365
|
+
recommendations.push('Consider edge caching or CDN for dynamic pages');
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Check for preconnect/dns-prefetch hints
|
|
369
|
+
const preconnects = $('link[rel="preconnect"], link[rel="dns-prefetch"]');
|
|
370
|
+
if (preconnects.length === 0) {
|
|
371
|
+
issues.push('No preconnect hints for external origins');
|
|
372
|
+
recommendations.push('Add preconnect for critical external origins (fonts, CDN, API)');
|
|
373
|
+
score -= 10;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Check for early hints / resource hints
|
|
377
|
+
const resourceHints = $('link[rel="preload"], link[rel="prefetch"], link[rel="modulepreload"]');
|
|
378
|
+
if (resourceHints.length === 0) {
|
|
379
|
+
recommendations.push('Consider adding preload hints for critical resources');
|
|
380
|
+
score -= 5;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
let estimate: 'good' | 'needs-improvement' | 'poor' | 'unknown' = 'unknown';
|
|
384
|
+
if (score >= 80) estimate = 'good';
|
|
385
|
+
else if (score >= 50) estimate = 'needs-improvement';
|
|
386
|
+
else estimate = 'poor';
|
|
387
|
+
|
|
388
|
+
return { estimate, issues, recommendations };
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Generate audit issues from Core Web Vitals analysis
|
|
393
|
+
*/
|
|
394
|
+
function generateCWVIssues(
|
|
395
|
+
lcp: ReturnType<typeof analyzeLCP>,
|
|
396
|
+
fidInp: ReturnType<typeof analyzeFID_INP>,
|
|
397
|
+
cls: ReturnType<typeof analyzeCLS>,
|
|
398
|
+
ttfb: ReturnType<typeof analyzeTTFB>,
|
|
399
|
+
url: string
|
|
400
|
+
): AuditIssue[] {
|
|
401
|
+
const issues: AuditIssue[] = [];
|
|
402
|
+
|
|
403
|
+
// LCP Issues
|
|
404
|
+
if (lcp.estimate === 'poor') {
|
|
405
|
+
issues.push({
|
|
406
|
+
code: 'CWV_LCP_POOR',
|
|
407
|
+
severity: 'critical',
|
|
408
|
+
category: 'performance',
|
|
409
|
+
title: 'Largest Contentful Paint likely poor',
|
|
410
|
+
description: `LCP issues detected: ${lcp.issues.join('; ')}`,
|
|
411
|
+
impact: 'Poor LCP hurts rankings and user experience. Google uses LCP as a ranking factor.',
|
|
412
|
+
howToFix: lcp.recommendations.join('\n'),
|
|
413
|
+
affectedUrls: [url],
|
|
414
|
+
});
|
|
415
|
+
} else if (lcp.estimate === 'needs-improvement' && lcp.issues.length > 0) {
|
|
416
|
+
issues.push({
|
|
417
|
+
code: 'CWV_LCP_NEEDS_IMPROVEMENT',
|
|
418
|
+
severity: 'warning',
|
|
419
|
+
category: 'performance',
|
|
420
|
+
title: 'Largest Contentful Paint needs improvement',
|
|
421
|
+
description: `LCP issues: ${lcp.issues.join('; ')}`,
|
|
422
|
+
impact: 'LCP affects Core Web Vitals score and rankings',
|
|
423
|
+
howToFix: lcp.recommendations.join('\n'),
|
|
424
|
+
affectedUrls: [url],
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// FID/INP Issues
|
|
429
|
+
if (fidInp.fidEstimate === 'poor') {
|
|
430
|
+
issues.push({
|
|
431
|
+
code: 'CWV_INP_POOR',
|
|
432
|
+
severity: 'critical',
|
|
433
|
+
category: 'performance',
|
|
434
|
+
title: 'Interaction to Next Paint likely poor',
|
|
435
|
+
description: `INP/FID issues detected: ${fidInp.issues.join('; ')}`,
|
|
436
|
+
impact: 'Poor interactivity hurts user experience and rankings',
|
|
437
|
+
howToFix: fidInp.recommendations.join('\n'),
|
|
438
|
+
affectedUrls: [url],
|
|
439
|
+
});
|
|
440
|
+
} else if (fidInp.fidEstimate === 'needs-improvement' && fidInp.issues.length > 0) {
|
|
441
|
+
issues.push({
|
|
442
|
+
code: 'CWV_INP_NEEDS_IMPROVEMENT',
|
|
443
|
+
severity: 'warning',
|
|
444
|
+
category: 'performance',
|
|
445
|
+
title: 'Interaction responsiveness needs improvement',
|
|
446
|
+
description: `INP issues: ${fidInp.issues.join('; ')}`,
|
|
447
|
+
impact: 'Affects user experience and Core Web Vitals',
|
|
448
|
+
howToFix: fidInp.recommendations.join('\n'),
|
|
449
|
+
affectedUrls: [url],
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// CLS Issues
|
|
454
|
+
if (cls.estimate === 'poor') {
|
|
455
|
+
issues.push({
|
|
456
|
+
code: 'CWV_CLS_POOR',
|
|
457
|
+
severity: 'critical',
|
|
458
|
+
category: 'performance',
|
|
459
|
+
title: 'Cumulative Layout Shift likely poor',
|
|
460
|
+
description: `CLS issues detected: ${cls.issues.join('; ')}`,
|
|
461
|
+
impact: 'Visual instability frustrates users and hurts rankings',
|
|
462
|
+
howToFix: cls.recommendations.join('\n'),
|
|
463
|
+
affectedUrls: [url],
|
|
464
|
+
});
|
|
465
|
+
} else if (cls.estimate === 'needs-improvement' && cls.issues.length > 0) {
|
|
466
|
+
issues.push({
|
|
467
|
+
code: 'CWV_CLS_NEEDS_IMPROVEMENT',
|
|
468
|
+
severity: 'warning',
|
|
469
|
+
category: 'performance',
|
|
470
|
+
title: 'Layout stability needs improvement',
|
|
471
|
+
description: `CLS issues: ${cls.issues.join('; ')}`,
|
|
472
|
+
impact: 'Layout shifts degrade user experience',
|
|
473
|
+
howToFix: cls.recommendations.join('\n'),
|
|
474
|
+
affectedUrls: [url],
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// TTFB Issues
|
|
479
|
+
if (ttfb.issues.length > 0) {
|
|
480
|
+
issues.push({
|
|
481
|
+
code: 'CWV_TTFB_ISSUES',
|
|
482
|
+
severity: 'info',
|
|
483
|
+
category: 'performance',
|
|
484
|
+
title: 'Server response optimizations available',
|
|
485
|
+
description: `TTFB issues: ${ttfb.issues.join('; ')}`,
|
|
486
|
+
impact: 'Slow server response delays all other metrics',
|
|
487
|
+
howToFix: ttfb.recommendations.join('\n'),
|
|
488
|
+
affectedUrls: [url],
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return issues;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Main Core Web Vitals analysis function
|
|
497
|
+
*/
|
|
498
|
+
export function analyzeCoreWebVitals(
|
|
499
|
+
html: string,
|
|
500
|
+
url: string,
|
|
501
|
+
headers?: Record<string, string>
|
|
502
|
+
): CoreWebVitalsEstimate {
|
|
503
|
+
const $ = cheerio.load(html);
|
|
504
|
+
|
|
505
|
+
const lcp = analyzeLCP(html, $);
|
|
506
|
+
const fidInp = analyzeFID_INP(html, $);
|
|
507
|
+
const cls = analyzeCLS(html, $);
|
|
508
|
+
const ttfb = analyzeTTFB($, headers);
|
|
509
|
+
|
|
510
|
+
const issues = generateCWVIssues(lcp, fidInp, cls, ttfb, url);
|
|
511
|
+
|
|
512
|
+
// Calculate overall score
|
|
513
|
+
const scores = {
|
|
514
|
+
lcp: lcp.estimate === 'good' ? 100 : lcp.estimate === 'needs-improvement' ? 60 : 30,
|
|
515
|
+
fid: fidInp.fidEstimate === 'good' ? 100 : fidInp.fidEstimate === 'needs-improvement' ? 60 : 30,
|
|
516
|
+
cls: cls.estimate === 'good' ? 100 : cls.estimate === 'needs-improvement' ? 60 : 30,
|
|
517
|
+
ttfb: ttfb.estimate === 'good' ? 100 : ttfb.estimate === 'needs-improvement' ? 60 : 30,
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// Weighted average (LCP and CLS are most impactful)
|
|
521
|
+
const overallScore = Math.round(
|
|
522
|
+
(scores.lcp * 0.30) +
|
|
523
|
+
(scores.fid * 0.20) +
|
|
524
|
+
(scores.cls * 0.30) +
|
|
525
|
+
(scores.ttfb * 0.20)
|
|
526
|
+
);
|
|
527
|
+
|
|
528
|
+
return {
|
|
529
|
+
lcp: {
|
|
530
|
+
estimate: lcp.estimate,
|
|
531
|
+
issues: lcp.issues,
|
|
532
|
+
recommendations: lcp.recommendations,
|
|
533
|
+
},
|
|
534
|
+
fid: {
|
|
535
|
+
estimate: fidInp.fidEstimate,
|
|
536
|
+
issues: fidInp.issues,
|
|
537
|
+
recommendations: fidInp.recommendations,
|
|
538
|
+
},
|
|
539
|
+
cls: {
|
|
540
|
+
estimate: cls.estimate,
|
|
541
|
+
issues: cls.issues,
|
|
542
|
+
recommendations: cls.recommendations,
|
|
543
|
+
},
|
|
544
|
+
inp: {
|
|
545
|
+
estimate: fidInp.inpEstimate,
|
|
546
|
+
issues: fidInp.issues,
|
|
547
|
+
recommendations: fidInp.recommendations,
|
|
548
|
+
},
|
|
549
|
+
ttfb: {
|
|
550
|
+
estimate: ttfb.estimate,
|
|
551
|
+
issues: ttfb.issues,
|
|
552
|
+
recommendations: ttfb.recommendations,
|
|
553
|
+
},
|
|
554
|
+
overallScore,
|
|
555
|
+
issues,
|
|
556
|
+
};
|
|
557
|
+
}
|