11import { RevisionPage } from '@gitbook/api' ;
2+ import { redirect } from 'next/navigation' ;
23
34import {
45 getRevisionPageByPath ,
56 getDocument ,
6- ContentTarget ,
77 getSpaceContentData ,
88 getSiteData ,
9+ getSiteRedirectBySource ,
910} from '@/lib/api' ;
1011import { resolvePagePath , resolvePageId } from '@/lib/pages' ;
1112import { getSiteContentPointer } from '@/lib/pointer' ;
@@ -41,6 +42,7 @@ export async function fetchContentData() {
4142 site,
4243 sections,
4344 spaces,
45+ shareKey : content . siteShareKey ,
4446 customization,
4547 scripts,
4648 ancestors : [ ] ,
@@ -54,7 +56,15 @@ export async function fetchContentData() {
5456export async function fetchPageData ( params : PagePathParams | PageIdParams ) {
5557 const contentData = await fetchContentData ( ) ;
5658
57- const page = await resolvePage ( contentData . contentTarget , contentData . pages , params ) ;
59+ const page = await resolvePage ( {
60+ organizationId : contentData . space . organization ,
61+ siteId : contentData . site . id ,
62+ spaceId : contentData . contentTarget . spaceId ,
63+ revisionId : contentData . contentTarget . revisionId ,
64+ pages : contentData . pages ,
65+ shareKey : contentData . shareKey ,
66+ params,
67+ } ) ;
5868 const document = page ?. page . documentId
5969 ? await getDocument ( contentData . space . id , page . page . documentId )
6070 : null ;
@@ -70,11 +80,17 @@ export async function fetchPageData(params: PagePathParams | PageIdParams) {
7080 * Resolve a page from the params.
7181 * If the path can't be found, we try to resolve it from the API to handle redirects.
7282 */
73- async function resolvePage (
74- contentTarget : ContentTarget ,
75- pages : RevisionPage [ ] ,
76- params : PagePathParams | PageIdParams ,
77- ) {
83+ async function resolvePage ( input : {
84+ organizationId : string ;
85+ siteId : string ;
86+ spaceId : string ;
87+ revisionId : string ;
88+ shareKey : string | undefined ;
89+ pages : RevisionPage [ ] ;
90+ params : PagePathParams | PageIdParams ;
91+ } ) {
92+ const { organizationId, siteId, spaceId, revisionId, pages, shareKey, params } = input ;
93+
7894 if ( 'pageId' in params ) {
7995 return resolvePageId ( pages , params . pageId ) ;
8096 }
@@ -88,20 +104,26 @@ async function resolvePage(
88104 return page ;
89105 }
90106
91- // If page can't be found, we try with the API, in case we have a redirect
92- // We use the raw pathname to handle special/malformed redirects setup by users in the GitSync.
93- // The page rendering will take care of redirecting to a normalized pathname.
94- //
95107 // We don't test path that are too long as GitBook doesn't support them and will return a 404 anyway.
96108 if ( rawPathname . length <= 512 ) {
97- const resolved = await getRevisionPageByPath (
98- contentTarget . spaceId ,
99- contentTarget . revisionId ,
100- rawPathname ,
101- ) ;
109+ // If page can't be found, we try with the API, in case we have a redirect at space level.
110+ // We use the raw pathname to handle special/malformed redirects setup by users in the GitSync.
111+ // The page rendering will take care of redirecting to a normalized pathname.
112+ const resolved = await getRevisionPageByPath ( spaceId , revisionId , rawPathname ) ;
102113 if ( resolved ) {
103114 return resolvePageId ( pages , resolved . id ) ;
104115 }
116+
117+ // If a page still can't be found, we try with the API, in case we have a redirect at site level.
118+ const resolvedSiteRedirect = await getSiteRedirectBySource ( {
119+ organizationId,
120+ siteId,
121+ source : rawPathname . startsWith ( '/' ) ? rawPathname : `/${ rawPathname } ` ,
122+ siteShareKey : input . shareKey ,
123+ } ) ;
124+ if ( resolvedSiteRedirect ) {
125+ return redirect ( resolvedSiteRedirect . target ) ;
126+ }
105127 }
106128
107129 return undefined ;
0 commit comments