Skip to content

Use Promise.all whenever possible #3319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/gitbook/src/components/Ads/AdClassicRendering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export async function AdClassicRendering({
insightsAd: SiteInsightsAd | null;
context: GitBookBaseContext;
}) {
const smallImgSrc =
const [smallImgSrc, logoSrc] = await Promise.all([
'smallImage' in ad
? await getResizedImageURL(context.imageResizer, ad.smallImage, { width: 192, dpr: 2 })
: null;
const logoSrc =
? getResizedImageURL(context.imageResizer, ad.smallImage, { width: 192, dpr: 2 })
: null,
'logo' in ad
? await getResizedImageURL(context.imageResizer, ad.logo, { width: 192 - 48, dpr: 2 })
: null;
? getResizedImageURL(context.imageResizer, ad.logo, { width: 192 - 48, dpr: 2 })
: null,
]);
return (
<Link
rel="sponsored noopener"
Expand Down
6 changes: 4 additions & 2 deletions packages/gitbook/src/components/Ads/renderAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ interface FetchPlaceholderAdOptions {
* and properly access user-agent and IP.
*/
export async function renderAd(options: FetchAdOptions) {
const context = isV2() ? await getServerActionBaseContext() : await getV1BaseContext();
const [context, result] = await Promise.all([
isV2() ? getServerActionBaseContext() : getV1BaseContext(),
options.source === 'live' ? fetchAd(options) : getPlaceholderAd(),
]);

const mode = options.source === 'live' ? options.mode : 'classic';
const result = options.source === 'live' ? await fetchAd(options) : await getPlaceholderAd();
if (!result || !result.ad.description || !result.ad.statlink) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ export async function RecordCard(
const coverFile = view.coverDefinition
? getRecordValue<string[]>(record[1], view.coverDefinition)?.[0]
: null;
const cover =
coverFile && context.contentContext
? await resolveContentRef({ kind: 'file', file: coverFile }, context.contentContext)
: null;

const targetRef = view.targetDefinition
? (record[1].values[view.targetDefinition] as ContentRef)
: null;
const target =

const [cover, target] = await Promise.all([
coverFile && context.contentContext
? resolveContentRef({ kind: 'file', file: coverFile }, context.contentContext)
: null,
targetRef && context.contentContext
? await resolveContentRef(targetRef, context.contentContext)
: null;
? resolveContentRef(targetRef, context.contentContext)
: null,
]);

const coverIsSquareOrPortrait =
cover?.file?.dimensions &&
Expand Down
6 changes: 4 additions & 2 deletions packages/gitbook/src/components/PageBody/PageCover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ export async function PageCover(props: {
context: GitBookSiteContext;
}) {
const { as, page, cover, context } = props;
const resolved = cover.ref ? await resolveContentRef(cover.ref, context) : null;
const resolvedDark = cover.refDark ? await resolveContentRef(cover.refDark, context) : null;
const [resolved, resolvedDark] = await Promise.all([
cover.ref ? resolveContentRef(cover.ref, context) : null,
cover.refDark ? resolveContentRef(cover.refDark, context) : null,
]);

return (
<div
Expand Down
4 changes: 2 additions & 2 deletions packages/gitbook/src/components/SiteLayout/SiteLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export async function generateSiteLayoutMetadata(context: GitBookSiteContext): P
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;

const faviconSize = 48;
const icons = [
const icons = await Promise.all([
{
url: customIcon?.light
? await getResizedImageURL(imageResizer, customIcon.light, {
Expand All @@ -125,7 +125,7 @@ export async function generateSiteLayoutMetadata(context: GitBookSiteContext): P
type: 'image/png',
media: '(prefers-color-scheme: dark)',
},
];
]);

return {
title: site.title,
Expand Down
10 changes: 6 additions & 4 deletions packages/gitbook/src/routes/ogimage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
: '';

// Load the fonts
const { fontFamily, fonts } = await (async () => {
const fontLoader = async () => {
// google fonts
if (typeof customization.styling.font === 'string') {
const fontFamily = customization.styling.font ?? CustomizationDefaultFont.Inter;
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
).filter(filterOutNullable);

return { fontFamily: 'CustomFont', fonts };
})();
};

const theme = customization.themes.default;
const useLightTheme = theme === 'light';
Expand Down Expand Up @@ -139,7 +139,7 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
break;
}

const favicon = await (async () => {
const faviconLoader = async () => {
if ('icon' in customization.favicon)
return (
<img
Expand All @@ -164,7 +164,9 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
)
);
return <img src={src} alt="Icon" width={40} height={40} tw="mr-4" />;
})();
};

const [favicon, { fontFamily, fonts }] = await Promise.all([faviconLoader(), fontLoader()]);

return new ImageResponse(
<div
Expand Down