Skip to content

Commit 3a4caf0

Browse files
authored
Add getContentTitle to properly render the title for sites & legacy published content (#2343)
1 parent 8ad0657 commit 3a4caf0

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

src/app/(space)/(content)/layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { buildVersion } from '@/lib/build';
1212
import { getContentSecurityPolicyNonce } from '@/lib/csp';
1313
import { absoluteHref, baseUrl } from '@/lib/links';
1414
import { shouldIndexSpace } from '@/lib/seo';
15+
import { getContentTitle } from '@/lib/utils';
1516

1617
import { ClientContexts } from './ClientContexts';
1718
import { RocketLoaderDetector } from './RocketLoaderDetector';
@@ -104,7 +105,7 @@ export async function generateMetadata(): Promise<Metadata> {
104105
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
105106

106107
return {
107-
title: `${parent ? parent.title : customization.title ?? space.title}`,
108+
title: getContentTitle(space, customization, parent),
108109
generator: `GitBook (${buildVersion()})`,
109110
metadataBase: new URL(baseUrl()),
110111
icons: {

src/app/(space)/(core)/~gitbook/icon/route.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@/lib/api';
1515
import { getEmojiForCode } from '@/lib/emojis';
1616
import { tcls } from '@/lib/tailwind';
17+
import { getContentTitle } from '@/lib/utils';
1718

1819
export const runtime = 'edge';
1920

@@ -54,7 +55,7 @@ export async function GET(req: NextRequest) {
5455
: space.visibility === ContentVisibility.InCollection && space.parent
5556
? await getCollection(space.parent)
5657
: null;
57-
const contentTitle = parent?.title ?? customization.title ?? space.title;
58+
const contentTitle = getContentTitle(space, customization, parent);
5859

5960
return new ImageResponse(
6061
(

src/app/(space)/(core)/~gitbook/ogimage/[pageId]/route.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { ImageResponse } from 'next/og';
33
import { NextRequest } from 'next/server';
44
import React from 'react';
55

6+
import { getContentTitle } from '@/lib/utils';
7+
68
import { PageIdParams, fetchPageData } from '../../../../fetch';
79

810
export const runtime = 'edge';
@@ -31,7 +33,7 @@ export async function GET(req: NextRequest, { params }: { params: PageIdParams }
3133
}}
3234
>
3335
<h2 tw="text-7xl font-bold tracking-tight text-left">
34-
{parent?.title ?? customization.title ?? space.title}
36+
{getContentTitle(space, customization, parent)}
3537
</h2>
3638
<div tw="flex flex-1">
3739
<p tw="text-4xl">{page ? page.title : 'Not found'}</p>

src/components/Header/HeaderLogo.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { HeaderMobileMenu } from '@/components/Header/HeaderMobileMenu';
1111
import { Image } from '@/components/utils';
1212
import { absoluteHref } from '@/lib/links';
1313
import { tcls } from '@/lib/tailwind';
14+
import { getContentTitle } from '@/lib/utils';
1415

1516
import { Link } from '../primitives';
1617

@@ -145,7 +146,7 @@ function LogoFallback(props: HeaderLogoProps) {
145146
: 'text-header-link',
146147
)}
147148
>
148-
{parent ? parent.title : customization.title ?? space.title}
149+
{getContentTitle(space, customization, parent)}
149150
</h1>
150151
</>
151152
);

src/lib/utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
Collection,
3+
CustomizationSettings,
4+
Site,
5+
SiteCustomizationSettings,
6+
Space,
7+
} from '@gitbook/api';
8+
9+
/**
10+
* Get the title to display for a content.
11+
*/
12+
export function getContentTitle(
13+
space: Space,
14+
customization: CustomizationSettings | SiteCustomizationSettings,
15+
parent: Site | Collection | null,
16+
) {
17+
// When we are rendering a site, always give priority to the customization title first
18+
// and then fallback to the site title
19+
if (parent?.object === 'site') {
20+
return customization.title ?? parent.title ?? space.title;
21+
}
22+
23+
// Otherwise the legacy behavior is not changed to avoid regressions
24+
return parent ? parent.title : customization.title ?? space.title;
25+
}

0 commit comments

Comments
 (0)