From 99da8df3d16bbf2e12263ed79496ed9935959796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 13 Mar 2025 18:45:14 +0100 Subject: [PATCH] Resize and optimize ogimage and favicons (#2980) --- .changeset/nasty-ways-sip.md | 5 ++ .../src/components/SiteLayout/SiteLayout.tsx | 53 +++++++++++-------- .../src/components/SitePage/SitePage.tsx | 11 ++-- packages/gitbook/src/routes/icon.tsx | 18 ++++++- packages/gitbook/src/routes/ogimage.tsx | 10 +++- 5 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 .changeset/nasty-ways-sip.md diff --git a/.changeset/nasty-ways-sip.md b/.changeset/nasty-ways-sip.md new file mode 100644 index 000000000..fba8ad786 --- /dev/null +++ b/.changeset/nasty-ways-sip.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Optimize favicons and og:image using the image resizer diff --git a/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx b/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx index 1bc4ad828..870438a4f 100644 --- a/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx +++ b/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx @@ -13,6 +13,7 @@ import { buildVersion } from '@/lib/build'; import { isSiteIndexable } from '@/lib/seo'; import { GITBOOK_API_PUBLIC_URL, GITBOOK_ASSETS_URL, GITBOOK_ICONS_URL } from '@v2/lib/env'; +import { getResizedImageURL } from '@v2/lib/images'; import { ClientContexts } from './ClientContexts'; import { RocketLoaderDetector } from './RocketLoaderDetector'; @@ -96,33 +97,43 @@ export async function generateSiteLayoutViewport(context: GitBookSiteContext): P } export async function generateSiteLayoutMetadata(context: GitBookSiteContext): Promise { - const { site, customization, linker } = context; + const { site, customization, linker, imageResizer } = context; const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null; + const faviconSize = 48; + const icons = [ + { + url: customIcon?.light + ? await getResizedImageURL(imageResizer, customIcon.light, { + width: faviconSize, + height: faviconSize, + }) + : linker.toAbsoluteURL( + linker.toPathInContent('~gitbook/icon?size=small&theme=light') + ), + type: 'image/png', + media: '(prefers-color-scheme: light)', + }, + { + url: customIcon?.dark + ? await getResizedImageURL(imageResizer, customIcon.dark, { + width: faviconSize, + height: faviconSize, + }) + : linker.toAbsoluteURL( + linker.toPathInContent('~gitbook/icon?size=small&theme=dark') + ), + type: 'image/png', + media: '(prefers-color-scheme: dark)', + }, + ]; + return { title: site.title, generator: `GitBook (${buildVersion()})`, icons: { - icon: [ - { - url: - customIcon?.light ?? - linker.toAbsoluteURL( - linker.toPathInContent('~gitbook/icon?size=small&theme=light') - ), - type: 'image/png', - media: '(prefers-color-scheme: light)', - }, - { - url: - customIcon?.dark ?? - linker.toAbsoluteURL( - linker.toPathInContent('~gitbook/icon?size=small&theme=dark') - ), - type: 'image/png', - media: '(prefers-color-scheme: dark)', - }, - ], + icon: icons, + apple: icons, }, robots: (await isSiteIndexable(context)) ? 'index, follow' : 'noindex, nofollow', }; diff --git a/packages/gitbook/src/components/SitePage/SitePage.tsx b/packages/gitbook/src/components/SitePage/SitePage.tsx index 6da8b1c41..5959b7393 100644 --- a/packages/gitbook/src/components/SitePage/SitePage.tsx +++ b/packages/gitbook/src/components/SitePage/SitePage.tsx @@ -10,6 +10,7 @@ import { PageBody, PageCover } from '@/components/PageBody'; import { getPagePath } from '@/lib/pages'; import { isPageIndexable, isSiteIndexable } from '@/lib/seo'; +import { getResizedImageURL } from '@v2/lib/images'; import { PageClientLayout } from './PageClientLayout'; import { type PagePathParams, fetchPageData, getPathnameParam, normalizePathname } from './fetch'; @@ -118,7 +119,7 @@ export async function generateSitePageMetadata(props: SitePageProps): Promise