From 2425bb0013f0074b90410dbb779057bc3e95bd29 Mon Sep 17 00:00:00 2001 From: Aziz Becha Date: Wed, 29 Apr 2026 11:41:53 +0100 Subject: [PATCH] parallelize SSR fetches on the home page The home page was awaiting five ssrFetch calls sequentially in getServerSideProps, then awaiting each .json() one at a time. Fan them out with Promise.all so the waterfall collapses into a single roundtrip's worth of latency. --- pages/index.tsx | 85 ++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index 534f9b60..78940605 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -22,47 +22,58 @@ export async function getServerSideProps(ctx: GetServerSidePropsContext) { }; } - const mostDownloadedResponse = await ssrFetch( - '/libraries', - { - order: 'downloads', - limit: LIMIT.toString(), - isMaintained: 'true', - hasNativeCode: 'true', - }, - ctx - ); - const recentlyAddedResponse = await ssrFetch( - '/libraries', - { order: 'added', limit: LIMIT.toString(), isMaintained: 'true' }, - ctx - ); - const recentlyUpdatedResponse = await ssrFetch( - '/libraries', - { order: 'updated', limit: LIMIT.toString(), isMaintained: 'true' }, - ctx - ); - const popularResponse = await ssrFetch( - '/libraries', - { - order: 'popularity', - limit: LIMIT.toString(), - isMaintained: 'true', - isPopular: 'true', - wasRecentlyUpdated: 'true', - }, - ctx - ); + const [ + mostDownloadedResponse, + recentlyAddedResponse, + recentlyUpdatedResponse, + popularResponse, + statisticResponse, + ] = await Promise.all([ + ssrFetch( + '/libraries', + { + order: 'downloads', + limit: LIMIT.toString(), + isMaintained: 'true', + hasNativeCode: 'true', + }, + ctx + ), + ssrFetch('/libraries', { order: 'added', limit: LIMIT.toString(), isMaintained: 'true' }, ctx), + ssrFetch( + '/libraries', + { order: 'updated', limit: LIMIT.toString(), isMaintained: 'true' }, + ctx + ), + ssrFetch( + '/libraries', + { + order: 'popularity', + limit: LIMIT.toString(), + isMaintained: 'true', + isPopular: 'true', + wasRecentlyUpdated: 'true', + }, + ctx + ), + ssrFetch('/libraries/statistic', {}, ctx), + ]); - const statisticResponse = await ssrFetch('/libraries/statistic', {}, ctx); + const [mostDownloaded, recentlyAdded, recentlyUpdated, popular, statistic] = await Promise.all([ + mostDownloadedResponse.json(), + recentlyAddedResponse.json(), + recentlyUpdatedResponse.json(), + popularResponse.json(), + statisticResponse.json(), + ]); return { props: { - mostDownloaded: await mostDownloadedResponse.json(), - recentlyAdded: await recentlyAddedResponse.json(), - recentlyUpdated: await recentlyUpdatedResponse.json(), - popular: await popularResponse.json(), - statistic: await statisticResponse.json(), + mostDownloaded, + recentlyAdded, + recentlyUpdated, + popular, + statistic, }, }; }