Skip to content

Commit

Permalink
Parallelize and remove useless Promise.all (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Nov 22, 2024
1 parent 99b6c92 commit eab7931
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
12 changes: 5 additions & 7 deletions packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,11 @@ export async function getSpaceContentData(
spaceId: pointer.spaceId,
revisionId: changeRequest?.revision ?? pointer.revisionId ?? space.revision,
};
const [pages] = await Promise.all([
getRevisionPages(space.id, contentTarget.revisionId, {
// We only care about the Git metadata when the Git sync is enabled
// otherwise we can optimize performance by not fetching it
metadata: !!space.gitSync,
}),
]);
const pages = await getRevisionPages(space.id, contentTarget.revisionId, {
// We only care about the Git metadata when the Git sync is enabled
// otherwise we can optimize performance by not fetching it
metadata: !!space.gitSync,
});

return {
space,
Expand Down
45 changes: 24 additions & 21 deletions packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,31 @@ export async function middleware(request: NextRequest) {
userAgent: userAgent(),
}),
async () => {
// Start fetching everything as soon as possible, but do not block the middleware on it
// the cache will handle concurrent calls
await waitUntil(
getSpaceContentData(
{
spaceId: resolved.space,
changeRequestId: resolved.changeRequest,
revisionId: resolved.revision,
},
'site' in resolved ? resolved.shareKey : undefined,
const [siteData] = await Promise.all([
'site' in resolved
? getSiteData({
organizationId: resolved.organization,
siteId: resolved.site,
siteSectionId: resolved.siteSection,
siteSpaceId: resolved.siteSpace,
siteShareKey: resolved.shareKey,
})
: null,
// Start fetching everything as soon as possible, but do not block the middleware on it
// the cache will handle concurrent calls
waitUntil(
getSpaceContentData(
{
spaceId: resolved.space,
changeRequestId: resolved.changeRequest,
revisionId: resolved.revision,
},
'site' in resolved ? resolved.shareKey : undefined,
),
),
);

const { scripts } = await ('site' in resolved
? getSiteData({
organizationId: resolved.organization,
siteId: resolved.site,
siteSectionId: resolved.siteSection,
siteSpaceId: resolved.siteSpace,
siteShareKey: resolved.shareKey,
})
: { scripts: [] });
]);

const scripts = siteData?.scripts ?? [];
return getContentSecurityPolicy(scripts, nonce);
},
);
Expand Down

0 comments on commit eab7931

Please sign in to comment.