Files
pulse/lib/api/pagespeed.ts
Usman Baig d1af25266b fix(pagespeed): increase fetch timeout for manual PSI checks to 120s
PSI checks run mobile + desktop sequentially (up to 60s total).
The default 30s client timeout was causing false network errors.
2026-03-22 18:28:06 +01:00

92 lines
2.6 KiB
TypeScript

import apiRequest from './client'
// * Types for PageSpeed Insights monitoring
export interface PageSpeedConfig {
site_id: string
enabled: boolean
frequency: 'daily' | 'weekly' | 'monthly'
url: string | null
next_check_at: string | null
created_at: string
updated_at: string
}
export interface AuditSummary {
id: string
title: string
description: string
score: number | null
display_value?: string
savings_ms?: number
category: 'opportunity' | 'diagnostic' | 'passed'
}
export interface PageSpeedCheck {
id: string
site_id: string
strategy: 'mobile' | 'desktop'
performance_score: number | null
accessibility_score: number | null
best_practices_score: number | null
seo_score: number | null
lcp_ms: number | null
cls: number | null
tbt_ms: number | null
fcp_ms: number | null
si_ms: number | null
tti_ms: number | null
audits: AuditSummary[] | null
triggered_by: 'scheduled' | 'manual'
checked_at: string
}
export async function getPageSpeedConfig(siteId: string): Promise<PageSpeedConfig> {
return apiRequest<PageSpeedConfig>(`/sites/${siteId}/pagespeed/config`)
}
export async function updatePageSpeedConfig(
siteId: string,
config: { enabled: boolean; frequency: string; url?: string }
): Promise<PageSpeedConfig> {
return apiRequest<PageSpeedConfig>(`/sites/${siteId}/pagespeed/config`, {
method: 'PUT',
body: JSON.stringify(config),
})
}
export async function getPageSpeedLatest(siteId: string): Promise<PageSpeedCheck[]> {
const res = await apiRequest<{ checks: PageSpeedCheck[] }>(`/sites/${siteId}/pagespeed/latest`)
return res?.checks ?? []
}
export async function getPageSpeedHistory(
siteId: string,
strategy: 'mobile' | 'desktop' = 'mobile',
days = 90
): Promise<PageSpeedCheck[]> {
const res = await apiRequest<{ checks: PageSpeedCheck[] }>(
`/sites/${siteId}/pagespeed/history?strategy=${strategy}&days=${days}`
)
return res?.checks ?? []
}
export async function getPageSpeedCheck(siteId: string, checkId: string): Promise<PageSpeedCheck> {
return apiRequest<PageSpeedCheck>(`/sites/${siteId}/pagespeed/checks/${checkId}`)
}
export async function triggerPageSpeedCheck(siteId: string): Promise<PageSpeedCheck[]> {
// * PSI checks take 10-30s per strategy (mobile + desktop sequential = up to 60s)
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 120_000)
try {
const res = await apiRequest<{ checks: PageSpeedCheck[] }>(`/sites/${siteId}/pagespeed/check`, {
method: 'POST',
signal: controller.signal,
})
return res?.checks ?? []
} finally {
clearTimeout(timeoutId)
}
}