Skip to content

Commit 8721806

Browse files
conico974Nicolas Dorseuil
andauthored
Use Promise.all whenever possible (#3319)
Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent 8a3910e commit 8721806

File tree

6 files changed

+59
-46
lines changed

6 files changed

+59
-46
lines changed

packages/gitbook/src/components/Ads/AdClassicRendering.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export async function AdClassicRendering({
1919
insightsAd: SiteInsightsAd | null;
2020
context: GitBookBaseContext;
2121
}) {
22-
const smallImgSrc =
22+
const [smallImgSrc, logoSrc] = await Promise.all([
2323
'smallImage' in ad
24-
? await getResizedImageURL(context.imageResizer, ad.smallImage, { width: 192, dpr: 2 })
25-
: null;
26-
const logoSrc =
24+
? getResizedImageURL(context.imageResizer, ad.smallImage, { width: 192, dpr: 2 })
25+
: null,
2726
'logo' in ad
28-
? await getResizedImageURL(context.imageResizer, ad.logo, { width: 192 - 48, dpr: 2 })
29-
: null;
27+
? getResizedImageURL(context.imageResizer, ad.logo, { width: 192 - 48, dpr: 2 })
28+
: null,
29+
]);
3030
return (
3131
<Link
3232
rel="sponsored noopener"

packages/gitbook/src/components/Ads/renderAd.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ interface FetchPlaceholderAdOptions {
4343
* and properly access user-agent and IP.
4444
*/
4545
export async function renderAd(options: FetchAdOptions) {
46-
const context = isV2() ? await getServerActionBaseContext() : await getV1BaseContext();
46+
const [context, result] = await Promise.all([
47+
isV2() ? getServerActionBaseContext() : getV1BaseContext(),
48+
options.source === 'live' ? fetchAd(options) : getPlaceholderAd(),
49+
]);
4750

4851
const mode = options.source === 'live' ? options.mode : 'classic';
49-
const result = options.source === 'live' ? await fetchAd(options) : await getPlaceholderAd();
5052
if (!result || !result.ad.description || !result.ad.statlink) {
5153
return null;
5254
}

packages/gitbook/src/components/DocumentView/Table/RecordCard.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ export async function RecordCard(
2323
const coverFile = view.coverDefinition
2424
? getRecordValue<string[]>(record[1], view.coverDefinition)?.[0]
2525
: null;
26-
const cover =
27-
coverFile && context.contentContext
28-
? await resolveContentRef({ kind: 'file', file: coverFile }, context.contentContext)
29-
: null;
30-
3126
const targetRef = view.targetDefinition
3227
? (record[1].values[view.targetDefinition] as ContentRef)
3328
: null;
34-
const target =
29+
30+
const [cover, target] = await Promise.all([
31+
coverFile && context.contentContext
32+
? resolveContentRef({ kind: 'file', file: coverFile }, context.contentContext)
33+
: null,
3534
targetRef && context.contentContext
36-
? await resolveContentRef(targetRef, context.contentContext)
37-
: null;
35+
? resolveContentRef(targetRef, context.contentContext)
36+
: null,
37+
]);
3838

3939
const coverIsSquareOrPortrait =
4040
cover?.file?.dimensions &&

packages/gitbook/src/components/PageBody/PageCover.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ export async function PageCover(props: {
2121
context: GitBookSiteContext;
2222
}) {
2323
const { as, page, cover, context } = props;
24-
const resolved = cover.ref ? await resolveContentRef(cover.ref, context) : null;
25-
const resolvedDark = cover.refDark ? await resolveContentRef(cover.refDark, context) : null;
24+
const [resolved, resolvedDark] = await Promise.all([
25+
cover.ref ? resolveContentRef(cover.ref, context) : null,
26+
cover.refDark ? resolveContentRef(cover.refDark, context) : null,
27+
]);
2628

2729
return (
2830
<div

packages/gitbook/src/components/SiteLayout/SiteLayout.tsx

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,37 @@ export async function generateSiteLayoutMetadata(context: GitBookSiteContext): P
102102
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
103103

104104
const faviconSize = 48;
105-
const icons = [
106-
{
107-
url: customIcon?.light
108-
? await getResizedImageURL(imageResizer, customIcon.light, {
109-
width: faviconSize,
110-
height: faviconSize,
111-
})
112-
: linker.toAbsoluteURL(
113-
linker.toPathInSpace('~gitbook/icon?size=small&theme=light')
114-
),
115-
type: 'image/png',
116-
media: '(prefers-color-scheme: light)',
117-
},
118-
{
119-
url: customIcon?.dark
120-
? await getResizedImageURL(imageResizer, customIcon.dark, {
121-
width: faviconSize,
122-
height: faviconSize,
123-
})
124-
: linker.toAbsoluteURL(linker.toPathInSpace('~gitbook/icon?size=small&theme=dark')),
125-
type: 'image/png',
126-
media: '(prefers-color-scheme: dark)',
127-
},
128-
];
105+
const icons = await Promise.all(
106+
[
107+
{
108+
url: customIcon?.light
109+
? getResizedImageURL(imageResizer, customIcon.light, {
110+
width: faviconSize,
111+
height: faviconSize,
112+
})
113+
: linker.toAbsoluteURL(
114+
linker.toPathInSpace('~gitbook/icon?size=small&theme=light')
115+
),
116+
type: 'image/png',
117+
media: '(prefers-color-scheme: light)',
118+
},
119+
{
120+
url: customIcon?.dark
121+
? getResizedImageURL(imageResizer, customIcon.dark, {
122+
width: faviconSize,
123+
height: faviconSize,
124+
})
125+
: linker.toAbsoluteURL(
126+
linker.toPathInSpace('~gitbook/icon?size=small&theme=dark')
127+
),
128+
type: 'image/png',
129+
media: '(prefers-color-scheme: dark)',
130+
},
131+
].map(async (icon) => ({
132+
...icon,
133+
url: await icon.url,
134+
}))
135+
);
129136

130137
return {
131138
title: site.title,

packages/gitbook/src/routes/ogimage.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
4545
: '';
4646

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

8787
return { fontFamily: 'CustomFont', fonts };
88-
})();
88+
};
8989

9090
const theme = customization.themes.default;
9191
const useLightTheme = theme === 'light';
@@ -139,7 +139,7 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
139139
break;
140140
}
141141

142-
const favicon = await (async () => {
142+
const faviconLoader = async () => {
143143
if ('icon' in customization.favicon)
144144
return (
145145
<img
@@ -164,7 +164,9 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
164164
)
165165
);
166166
return <img src={src} alt="Icon" width={40} height={40} tw="mr-4" />;
167-
})();
167+
};
168+
169+
const [favicon, { fontFamily, fonts }] = await Promise.all([faviconLoader(), fontLoader()]);
168170

169171
return new ImageResponse(
170172
<div

0 commit comments

Comments
 (0)