Skip to content

Commit dc4268d

Browse files
authored
Compute correct urls for variants/sections when previewing unpublished site (#3259)
1 parent a0c06a7 commit dc4268d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.changeset/cool-seas-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Fix navigation between sections/variants when previewing a site in v2

packages/gitbook/src/components/Header/SpacesDropdown.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { SiteSpace } from '@gitbook/api';
22

33
import { tcls } from '@/lib/tailwind';
44

5+
import { joinPath } from '@/lib/paths';
56
import type { GitBookSiteContext } from '@v2/lib/context';
67
import { DropdownChevron, DropdownMenu } from './DropdownMenu';
78
import { SpacesDropdownMenuItem } from './SpacesDropdownMenuItem';
@@ -74,11 +75,22 @@ export function SpacesDropdown(props: {
7475
title: otherSiteSpace.title,
7576
url: otherSiteSpace.urls.published
7677
? linker.toLinkForContent(otherSiteSpace.urls.published)
77-
: otherSiteSpace.space.urls.app,
78+
: getFallbackSiteSpaceURL(otherSiteSpace, context),
7879
}}
7980
active={otherSiteSpace.id === siteSpace.id}
8081
/>
8182
))}
8283
</DropdownMenu>
8384
);
8485
}
86+
87+
/**
88+
* When the site is not published yet, `urls.published` is not available.
89+
* To ensure navigation works in preview, we compute a relative URL from the siteSpace path.
90+
*/
91+
function getFallbackSiteSpaceURL(siteSpace: SiteSpace, context: GitBookSiteContext) {
92+
const { linker, sections } = context;
93+
return linker.toPathInSite(
94+
sections?.current ? joinPath(sections.current.path, siteSpace.path) : siteSpace.path
95+
);
96+
}

packages/gitbook/src/components/SiteSections/encodeClientSiteSections.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ function encodeSection(context: GitBookSiteContext, section: SiteSection) {
5353
description: section.description,
5454
icon: section.icon,
5555
object: section.object,
56-
url: section.urls.published ? linker.toLinkForContent(section.urls.published) : '',
56+
url: section.urls.published
57+
? linker.toLinkForContent(section.urls.published)
58+
: linker.toPathInSite(section.path),
5759
};
5860
}

0 commit comments

Comments
 (0)