Skip to content

Commit f07982d

Browse files
authored
Investigate issue where icons and ogimage would occasionally fail. (#3099)
1 parent b92ecfa commit f07982d

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

packages/gitbook-v2/src/lib/links.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ export function createLinker(
7272

7373
const siteBasePath = withTrailingSlash(withLeadingSlash(servedOn.siteBasePath));
7474
const spaceBasePath = withTrailingSlash(withLeadingSlash(servedOn.spaceBasePath));
75+
const protocol = (() => {
76+
if (servedOn.protocol) {
77+
return servedOn.protocol;
78+
}
79+
80+
if (servedOn.host) {
81+
return servedOn.host.startsWith('localhost') ? 'http:' : 'https:';
82+
}
83+
84+
return 'https:';
85+
})();
7586

7687
const linker: GitBookLinker = {
7788
toPathInSpace(relativePath: string): string {
@@ -97,7 +108,7 @@ export function createLinker(
97108
return absolutePath;
98109
}
99110

100-
return `${servedOn.protocol ?? 'https:'}//${joinPaths(servedOn.host, absolutePath)}`;
111+
return `${protocol}//${joinPaths(servedOn.host, absolutePath)}`;
101112
},
102113

103114
toPathForPage({ pages, page, anchor }) {

packages/gitbook/src/app/middleware/(site)/(core)/~gitbook/icon/route.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@ import { serveIcon } from '@/routes/icon';
77
export const runtime = 'edge';
88

99
export async function GET(req: NextRequest) {
10-
const pointer = await getSiteContentPointer();
11-
const context = await fetchV1ContextForSitePointer(pointer);
10+
try {
11+
console.log('icon: getSiteContentPointer');
12+
const pointer = await getSiteContentPointer();
13+
console.log('icon: fetchV1context');
14+
const context = await fetchV1ContextForSitePointer(pointer);
1215

13-
return serveIcon(context, req);
16+
// biome-ignore lint/suspicious/noConsole: we want to log here
17+
console.log(`serving icon for ${context.site.id}`);
18+
19+
return await serveIcon(context, req);
20+
} catch (err) {
21+
if (err instanceof Error) {
22+
console.error(`icon: ${err.toString()}`, err.stack);
23+
}
24+
25+
throw err;
26+
}
1427
}

packages/gitbook/src/lib/cache/cloudflare-do.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ export const cloudflareDOCache: CacheBackend = {
2626
return null;
2727
}
2828

29-
return (await stub.get<CacheEntry>(key)) ?? null;
29+
try {
30+
return (await stub.get<CacheEntry>(key)) ?? null;
31+
} catch (err) {
32+
console.error('cloudflareDO.get', err);
33+
return null;
34+
}
3035
}
3136
);
3237
},

packages/gitbook/src/lib/v1.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
285285
export async function fetchV1ContextForSitePointer(pointer: SiteContentPointer) {
286286
const baseContext = await getV1BaseContext();
287287

288+
console.log('icon: baseContext success');
288289
const context = await fetchSiteContextByIds(baseContext, {
289290
organization: pointer.organizationId,
290291
site: pointer.siteId,
@@ -295,8 +296,10 @@ export async function fetchV1ContextForSitePointer(pointer: SiteContentPointer)
295296
changeRequest: pointer.changeRequestId,
296297
revision: pointer.revisionId,
297298
});
299+
console.log('icon: context inner success');
298300

299301
context.customization = await getDynamicCustomizationSettings(context.customization);
302+
console.log('icon: customization success');
300303

301304
return context;
302305
}

packages/gitbook/src/routes/icon.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const SIZES = {
2727
* Generate an icon for a site content.
2828
*/
2929
export async function serveIcon(context: GitBookSiteContext, req: Request) {
30+
console.log('icon: serveIcon', req.url);
3031
const options = getOptions(req.url);
3132
const size = SIZES[options.size];
3233

0 commit comments

Comments
 (0)