Always check timing data even on fetch error (4xx/5xx)
Resource Timing API records latency even for error responses. Now we check for timing data regardless of fetch success/failure, only reporting unreachable if no timing data is available.
This commit is contained in:
parent
8be7002ad9
commit
0038f23460
72
src/main.js
72
src/main.js
@ -32,29 +32,9 @@ const hostState = HOSTS.map(host => ({
|
|||||||
status: 'pending', // 'online', 'offline', 'pending', 'error'
|
status: 'pending', // 'online', 'offline', 'pending', 'error'
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Measure latency using HEAD request with Resource Timing API
|
// Extract timing from Resource Timing API
|
||||||
async function measureLatency(url) {
|
function getTimingForUrl(url) {
|
||||||
const controller = new AbortController()
|
const entries = performance.getEntriesByName(url, 'resource')
|
||||||
const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT)
|
|
||||||
|
|
||||||
// Add cache-busting parameter
|
|
||||||
const targetUrl = new URL(url)
|
|
||||||
const cacheBuster = `${Date.now()}-${Math.random().toString(36).slice(2)}`
|
|
||||||
targetUrl.searchParams.set('_cb', cacheBuster)
|
|
||||||
const finalUrl = targetUrl.toString()
|
|
||||||
|
|
||||||
try {
|
|
||||||
await fetch(finalUrl, {
|
|
||||||
method: 'HEAD',
|
|
||||||
mode: 'no-cors',
|
|
||||||
cache: 'no-store',
|
|
||||||
signal: controller.signal,
|
|
||||||
})
|
|
||||||
|
|
||||||
clearTimeout(timeoutId)
|
|
||||||
|
|
||||||
// Get accurate timing from Resource Timing API
|
|
||||||
const entries = performance.getEntriesByName(finalUrl, 'resource')
|
|
||||||
if (entries.length > 0) {
|
if (entries.length > 0) {
|
||||||
const entry = entries[entries.length - 1]
|
const entry = entries[entries.length - 1]
|
||||||
// Use responseEnd - requestStart for actual network time
|
// Use responseEnd - requestStart for actual network time
|
||||||
@ -66,23 +46,57 @@ async function measureLatency(url) {
|
|||||||
// For opaque responses, duration includes queuing time but is still more accurate
|
// For opaque responses, duration includes queuing time but is still more accurate
|
||||||
latency = entry.duration
|
latency = entry.duration
|
||||||
}
|
}
|
||||||
// Clean up the entry
|
return Math.round(latency)
|
||||||
performance.clearResourceTimings()
|
}
|
||||||
return { latency: Math.round(latency), error: null }
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback if no timing entry found
|
// Measure latency using HEAD request with Resource Timing API
|
||||||
return { latency: null, error: null }
|
async function measureLatency(url) {
|
||||||
|
const controller = new AbortController()
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT)
|
||||||
|
|
||||||
|
// Add cache-busting parameter
|
||||||
|
const targetUrl = new URL(url)
|
||||||
|
const cacheBuster = `${Date.now()}-${Math.random().toString(36).slice(2)}`
|
||||||
|
targetUrl.searchParams.set('_cb', cacheBuster)
|
||||||
|
const finalUrl = targetUrl.toString()
|
||||||
|
|
||||||
|
let fetchError = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fetch(finalUrl, {
|
||||||
|
method: 'HEAD',
|
||||||
|
mode: 'no-cors',
|
||||||
|
cache: 'no-store',
|
||||||
|
signal: controller.signal,
|
||||||
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
fetchError = err
|
||||||
|
}
|
||||||
|
|
||||||
clearTimeout(timeoutId)
|
clearTimeout(timeoutId)
|
||||||
|
|
||||||
|
// Always check timing - even 4xx/5xx responses have timing data
|
||||||
|
const latency = getTimingForUrl(finalUrl)
|
||||||
performance.clearResourceTimings()
|
performance.clearResourceTimings()
|
||||||
|
|
||||||
if (err.name === 'AbortError') {
|
// If we got timing data, return it regardless of fetch error
|
||||||
|
if (latency !== null) {
|
||||||
|
return { latency, error: null }
|
||||||
|
}
|
||||||
|
|
||||||
|
// No timing data - determine error type
|
||||||
|
if (fetchError?.name === 'AbortError') {
|
||||||
return { latency: null, error: 'timeout' }
|
return { latency: null, error: 'timeout' }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fetchError) {
|
||||||
return { latency: null, error: 'unreachable' }
|
return { latency: null, error: 'unreachable' }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch succeeded but no timing (shouldn't happen)
|
||||||
|
return { latency: null, error: null }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get latency color based on value
|
// Get latency color based on value
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user