From ab92eb4d0ba5e7cfad6e619e3da477d65bd86daf Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 6 Oct 2022 18:33:39 +0100 Subject: [PATCH] feat: use promise all to make it faster --- src/components/site/SiteLayout.server.tsx | 66 ++++++++++++----------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/components/site/SiteLayout.server.tsx b/src/components/site/SiteLayout.server.tsx index 96d11aee..9be2d546 100644 --- a/src/components/site/SiteLayout.server.tsx +++ b/src/components/site/SiteLayout.server.tsx @@ -16,41 +16,45 @@ export const getServerSideProps = async ( const pageSlug = ctx.params!.page as string const tag = ctx.params!.tag as string - await prefetchGetSite(domainOrSubdomain, queryClient) - await prefetchGetSiteSubscriptions( - { - siteId: domainOrSubdomain, - }, - queryClient, - ) - - if (pageSlug) { - const page = await fetchGetPage( + await Promise.all([ + prefetchGetSite(domainOrSubdomain, queryClient), + prefetchGetSiteSubscriptions( { - site: domainOrSubdomain, - page: pageSlug, - render: true, - includeAuthors: true, + siteId: domainOrSubdomain, }, queryClient, - ) + ), + new Promise(async (resolve) => { + if (pageSlug) { + const page = await fetchGetPage( + { + site: domainOrSubdomain, + page: pageSlug, + render: true, + includeAuthors: true, + }, + queryClient, + ) - if (new Date(page!.date_published) > new Date()) { - throw notFound() - } - } else { - await prefetchGetPagesBySite( - { - site: domainOrSubdomain, - take: 1000, - type: "post", - visibility: PageVisibilityEnum.Published, - render: true, - ...(tag && { tags: [tag] }), - }, - queryClient, - ) - } + if (new Date(page!.date_published) > new Date()) { + throw notFound() + } + } else { + await prefetchGetPagesBySite( + { + site: domainOrSubdomain, + take: 1000, + type: "post", + visibility: PageVisibilityEnum.Published, + render: true, + ...(tag && { tags: [tag] }), + }, + queryClient, + ) + } + resolve(null) + }), + ]) return { props: {