diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..3ec857ac --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-03-24 - Optimize Gateway API Fetch Concurrency in Astro API routes +**Learning:** Initiating slow external network requests sequentially after local database queries and filesystem checks creates a bottleneck in Astro API route response times. +**Action:** When an API handler requires both external fetches and local data, start the external fetch as a Promise at the very beginning of the handler (with a dummy catch to prevent unhandled rejection crashes), execute the local operations, and then `await` the Promise at the end to parallelize the workloads. \ No newline at end of file diff --git a/src/pages/api/dashboard-projects-health.ts b/src/pages/api/dashboard-projects-health.ts index c9947031..22c15362 100644 --- a/src/pages/api/dashboard-projects-health.ts +++ b/src/pages/api/dashboard-projects-health.ts @@ -30,6 +30,10 @@ function mapSessionToRunState(raw: Record): { running: boolean /** GET — état synthétique pour la carte Projets du dashboard (poll léger). */ export const GET: APIRoute = async ({ locals }) => { const email = locals.user?.email as string | undefined; + const zimaosSessionPromise = fetchZimaOSSessionsPayload(email); + // Attach a dummy catch handler immediately to prevent UnhandledPromiseRejection + // if the fetch fails before we await it later in the final try/catch block. + zimaosSessionPromise.catch(() => {}); const payload: { projects: Array<{ @@ -150,7 +154,7 @@ export const GET: APIRoute = async ({ locals }) => { } try { - const oc = await fetchZimaOSSessionsPayload(email); + const oc = await zimaosSessionPromise; payload.swarm.zimaosOk = oc.ok; const sessions = oc.ok ? (normalizeZimaOSSessions(oc.data) as Record[])