Skip to content

Commit eab7931

Browse files
authored
Parallelize and remove useless Promise.all (#2581)
1 parent 99b6c92 commit eab7931

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

packages/gitbook/src/lib/api.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,11 @@ export async function getSpaceContentData(
874874
spaceId: pointer.spaceId,
875875
revisionId: changeRequest?.revision ?? pointer.revisionId ?? space.revision,
876876
};
877-
const [pages] = await Promise.all([
878-
getRevisionPages(space.id, contentTarget.revisionId, {
879-
// We only care about the Git metadata when the Git sync is enabled
880-
// otherwise we can optimize performance by not fetching it
881-
metadata: !!space.gitSync,
882-
}),
883-
]);
877+
const pages = await getRevisionPages(space.id, contentTarget.revisionId, {
878+
// We only care about the Git metadata when the Git sync is enabled
879+
// otherwise we can optimize performance by not fetching it
880+
metadata: !!space.gitSync,
881+
});
884882

885883
return {
886884
space,

packages/gitbook/src/middleware.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -161,28 +161,31 @@ export async function middleware(request: NextRequest) {
161161
userAgent: userAgent(),
162162
}),
163163
async () => {
164-
// Start fetching everything as soon as possible, but do not block the middleware on it
165-
// the cache will handle concurrent calls
166-
await waitUntil(
167-
getSpaceContentData(
168-
{
169-
spaceId: resolved.space,
170-
changeRequestId: resolved.changeRequest,
171-
revisionId: resolved.revision,
172-
},
173-
'site' in resolved ? resolved.shareKey : undefined,
164+
const [siteData] = await Promise.all([
165+
'site' in resolved
166+
? getSiteData({
167+
organizationId: resolved.organization,
168+
siteId: resolved.site,
169+
siteSectionId: resolved.siteSection,
170+
siteSpaceId: resolved.siteSpace,
171+
siteShareKey: resolved.shareKey,
172+
})
173+
: null,
174+
// Start fetching everything as soon as possible, but do not block the middleware on it
175+
// the cache will handle concurrent calls
176+
waitUntil(
177+
getSpaceContentData(
178+
{
179+
spaceId: resolved.space,
180+
changeRequestId: resolved.changeRequest,
181+
revisionId: resolved.revision,
182+
},
183+
'site' in resolved ? resolved.shareKey : undefined,
184+
),
174185
),
175-
);
176-
177-
const { scripts } = await ('site' in resolved
178-
? getSiteData({
179-
organizationId: resolved.organization,
180-
siteId: resolved.site,
181-
siteSectionId: resolved.siteSection,
182-
siteSpaceId: resolved.siteSpace,
183-
siteShareKey: resolved.shareKey,
184-
})
185-
: { scripts: [] });
186+
]);
187+
188+
const scripts = siteData?.scripts ?? [];
186189
return getContentSecurityPolicy(scripts, nonce);
187190
},
188191
);

0 commit comments

Comments
 (0)