Skip to content

Commit 300a53e

Browse files
committed
fix: allow explicitly preferring lighthouse metrics
resolves #146
1 parent a3ccf7c commit 300a53e

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

server/routes/badge/[domain].get.ts

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ interface StyleConfig {
77
rect: string
88
}
99

10+
const lighthouseMetrics = new Set([
11+
'performance',
12+
'accessibility',
13+
'best-practices',
14+
'bestPractices',
15+
'seo',
16+
])
17+
1018
export default defineCachedEventHandler(async (event) => {
1119
// Get and validate the domain from the URL parameter
1220
const domain = await validateDomain(getRouterParam(event, 'domain'))
@@ -16,55 +24,44 @@ export default defineCachedEventHandler(async (event) => {
1624
const style = String(query.style || 'default') // default, flat, flat-square, plastic
1725
const metric = String(query.metric || 'cwv') // cwv, lcp, cls, inp, performance
1826

27+
setResponseHeader(event, 'Content-Type', 'image/svg+xml')
28+
1929
try {
30+
if (!lighthouseMetrics.has(metric)) {
2031
// Fetch the CrUX data using the existing API
21-
const cruxData = await $fetch(`/api/crux/${domain}`)
22-
23-
setResponseHeader(event, 'Content-Type', 'image/svg+xml')
32+
const cruxData = await $fetch(`/api/crux/${domain}`)
2433

25-
// Generate the badge with Core Web Vitals based on requested metric
26-
let svgBadge: string
34+
if (metric === 'cwv') {
35+
return generateCWVBadge(domain, cruxData, style)
36+
}
37+
if (['lcp', 'cls', 'inp'].includes(metric)) {
38+
return generateMetricBadge(domain, cruxData, metric, style)
39+
}
2740

28-
if (metric === 'cwv') {
29-
svgBadge = generateCWVBadge(domain, cruxData, style)
30-
}
31-
else if (['lcp', 'cls', 'inp'].includes(metric)) {
32-
svgBadge = generateMetricBadge(domain, cruxData, metric, style)
33-
}
34-
else {
3541
// Default to CWV badge if invalid metric specified
36-
svgBadge = generateCWVBadge(domain, cruxData, style)
42+
return generateCWVBadge(domain, cruxData, style)
3743
}
38-
39-
return svgBadge
4044
}
4145
catch {
4246
// If CrUX data is not available, try to fetch Lighthouse data
43-
try {
44-
const lighthouseData = await $fetch(`/api/run/${domain}`)
45-
46-
// Set content type header for SVG
47-
setResponseHeader(event, 'Content-Type', 'image/svg+xml')
48-
49-
let svgBadge: string
47+
}
5048

51-
// Check if user requested a Lighthouse metric specifically
52-
if (metric === 'performance' || !['cwv', 'lcp', 'cls', 'inp'].includes(metric)) {
53-
svgBadge = generateLighthouseBadge(domain, lighthouseData, metric, style)
54-
}
55-
else {
56-
// Generate default Lighthouse badge if CrUX data not available but CWV metric requested
57-
svgBadge = generateLighthouseBadge(domain, lighthouseData, 'performance', style)
58-
}
49+
try {
50+
const lighthouseData = await $fetch(`/api/run/${domain}`)
5951

60-
return svgBadge
61-
}
62-
catch {
63-
throw createError({
64-
statusCode: 404,
65-
message: 'No performance data available for this domain.',
66-
})
52+
// Check if user requested a Lighthouse metric specifically
53+
if (metric === 'performance' || !['cwv', 'lcp', 'cls', 'inp'].includes(metric)) {
54+
return generateLighthouseBadge(domain, lighthouseData, metric, style)
6755
}
56+
57+
// Generate default Lighthouse badge if CrUX data not available but CWV metric requested
58+
return generateLighthouseBadge(domain, lighthouseData, 'performance', style)
59+
}
60+
catch {
61+
throw createError({
62+
statusCode: 404,
63+
message: 'No performance data available for this domain.',
64+
})
6865
}
6966
}, {
7067
base: 'pagespeed',
@@ -75,7 +72,7 @@ export default defineCachedEventHandler(async (event) => {
7572
staleMaxAge: 24 * 60 * 60,
7673
})
7774

78-
function generateCWVBadge(domain: string, data: CruxData, style: string = 'default'): string {
75+
function generateCWVBadge(domain: string, data: CruxData, style = 'default'): string {
7976
// Color mappings for Core Web Vitals
8077
const getStatusColor = (pass: boolean) => pass ? '#23c55e' : '#ef4444'
8178
const overallStatus = data.cwv
@@ -107,7 +104,7 @@ function generateCWVBadge(domain: string, data: CruxData, style: string = 'defau
107104
</svg>`
108105
}
109106

110-
function generateMetricBadge(domain: string, data: CruxData, metric: string, style: string = 'default'): string {
107+
function generateMetricBadge(domain: string, data: CruxData, metric: string, style = 'default'): string {
111108
let metricLabel = ''
112109
let metricValue: string | number = ''
113110
let metricThresholds = { good: 0, needsImprovement: 0, unit: '' }
@@ -165,7 +162,7 @@ function generateMetricBadge(domain: string, data: CruxData, metric: string, sty
165162
</svg>`
166163
}
167164

168-
function generateLighthouseBadge(domain: string, data: LighthouseData, metric: string = 'performance', style: string = 'default'): string {
165+
function generateLighthouseBadge(domain: string, data: LighthouseData, metric = 'performance', style = 'default'): string {
169166
// Get performance score and determine color
170167
let metricLabel = ''
171168
let score = 0

0 commit comments

Comments
 (0)