Skip to content

Revert debugging of ogimage errors #3105

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 6 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 10 additions & 53 deletions bun.lock

Large diffs are not rendered by default.

13 changes: 1 addition & 12 deletions packages/gitbook-v2/src/lib/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ export function createLinker(

const siteBasePath = withTrailingSlash(withLeadingSlash(servedOn.siteBasePath));
const spaceBasePath = withTrailingSlash(withLeadingSlash(servedOn.spaceBasePath));
const protocol = (() => {
if (servedOn.protocol) {
return servedOn.protocol;
}

if (servedOn.host) {
return servedOn.host.startsWith('localhost') ? 'http:' : 'https:';
}

return 'https:';
})();

const linker: GitBookLinker = {
toPathInSpace(relativePath: string): string {
Expand All @@ -108,7 +97,7 @@ export function createLinker(
return absolutePath;
}

return `${protocol}//${joinPaths(servedOn.host, absolutePath)}`;
return `${servedOn.protocol ?? 'https:'}//${joinPaths(servedOn.host, absolutePath)}`;
},

toPathForPage({ pages, page, anchor }) {
Expand Down
1 change: 0 additions & 1 deletion packages/gitbook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@sindresorhus/fnv1a": "^3.1.0",
"@tailwindcss/container-queries": "^0.1.1",
"@tailwindcss/typography": "^0.5.16",
"@vercel/og": "0.6.8.",
"ai": "^4.2.2",
"assert-never": "^1.2.1",
"bun-types": "^1.1.20",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,103 +1,14 @@
import { ImageResponse } from '@vercel/og';
import { notFound, redirect } from 'next/navigation';
import type { NextRequest } from 'next/server';

import { getEmojiForCode } from '@/lib/emojis';
import { getSiteContentPointer } from '@/lib/pointer';
import { tcls } from '@/lib/tailwind';
import { fetchV1ContextForSitePointer } from '@/lib/v1';
import { getResizedImageURL } from '@v2/lib/images';
import { serveIcon } from '@/routes/icon';

export const runtime = 'edge';

const SIZES = {
/** Size for a favicon */
small: {
width: 48,
height: 48,
textSize: 'text-[32px]',
boxStyle: 'rounded-[8px]',
},
/** Size for display as an app icon or in the header */
medium: {
width: 256,
height: 256,
textSize: 'text-[164px]',
boxStyle: 'rounded-[32px]',
},
};

/**
* Generate an icon for a site content.
*/
export async function GET(req: NextRequest) {
const options = getOptions(req.url);
const pointer = await getSiteContentPointer();
const context = await fetchV1ContextForSitePointer(pointer);

const size = SIZES[options.size];
const { site, customization } = context;
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;

// If the site has a custom icon, redirect to it
if (customIcon) {
const iconUrl = options.theme === 'light' ? customIcon.light : customIcon.dark;
redirect(
await getResizedImageURL(context.imageResizer, iconUrl, {
width: size.width,
height: size.height,
})
);
}

const contentTitle = site.title;
return new ImageResponse(
<div
tw={tcls(options.theme === 'light' ? 'bg-white' : 'bg-black', size.boxStyle)}
style={{
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<h2
tw={tcls(
size.textSize,
'font-bold',
'tracking-tight',
options.theme === 'light' ? 'text-black' : 'text-white'
)}
>
{'emoji' in customization.favicon
? getEmojiForCode(customization.favicon.emoji)
: contentTitle.slice(0, 1).toUpperCase()}
</h2>
</div>,
{
width: size.width,
height: size.height,
}
);
}

function getOptions(inputUrl: string): {
size: keyof typeof SIZES;
theme: 'light' | 'dark';
} {
const url = new URL(inputUrl);
const sizeParam = (url.searchParams.get('size') ?? 'small') as keyof typeof SIZES;
const themeParam = url.searchParams.get('theme') ?? 'light';

if (!SIZES[sizeParam] || !['light', 'dark'].includes(themeParam)) {
notFound();
}

return {
// @ts-ignore
size: sizeParam,
// @ts-ignore
theme: themeParam,
};
return serveIcon(context, req);
}
7 changes: 1 addition & 6 deletions packages/gitbook/src/lib/cache/cloudflare-do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ export const cloudflareDOCache: CacheBackend = {
return null;
}

try {
return (await stub.get<CacheEntry>(key)) ?? null;
} catch (err) {
console.error('cloudflareDO.get', err);
return null;
}
return (await stub.get<CacheEntry>(key)) ?? null;
}
);
},
Expand Down
4 changes: 3 additions & 1 deletion packages/gitbook/src/routes/icon.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ImageResponse } from '@vercel/og';
import { notFound, redirect } from 'next/navigation';
import { ImageResponse } from 'next/og';

import { getEmojiForCode } from '@/lib/emojis';
import { tcls } from '@/lib/tailwind';
Expand Down Expand Up @@ -32,6 +32,7 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {

const { site, customization } = context;
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;

// If the site has a custom icon, redirect to it
if (customIcon) {
const iconUrl = options.theme === 'light' ? customIcon.light : customIcon.dark;
Expand All @@ -44,6 +45,7 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
}

const contentTitle = site.title;

return new ImageResponse(
<div
tw={tcls(options.theme === 'light' ? 'bg-white' : 'bg-black', size.boxStyle)}
Expand Down