Skip to content

Commit 590729c

Browse files
authored
Redirect to space base path on fallback true (#3745)
1 parent b554d7d commit 590729c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/gitbook/src/components/SitePage/SitePage.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ export async function generateSitePageMetadata(props: SitePageProps): Promise<Me
9999
});
100100

101101
if (!pageTarget) {
102+
if (context.isFallback) {
103+
redirect(context.linker.toPathInSpace('/'));
104+
}
102105
notFound();
103106
}
104107

@@ -151,6 +154,10 @@ export async function getSitePageData(props: SitePageProps) {
151154
// before trying to resolve the page again
152155
redirect(context.linker.toPathInSpace(pathname));
153156
} else {
157+
// If the page is not found and we are in fallback mode, return a redirect to the basepath
158+
if (context.isFallback) {
159+
redirect(context.linker.toPathInSpace('/'));
160+
}
154161
notFound();
155162
}
156163
} else if (getPagePath(context.revision.pages, pageTarget.page) !== rawPathname) {

packages/gitbook/src/lib/context.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ export type SiteURLData = Pick<
5050
* Identifier used for image resizing.
5151
*/
5252
imagesContextId: string;
53+
54+
/**
55+
* Whether this request is a fallback rendering.
56+
* We use this when switching variant as we don't know if the page exists in the other variant.
57+
* By knowing it's a fallback, we can redirect to the space base path instead of returning a 404.
58+
*/
59+
isFallback?: boolean;
5360
};
5461

5562
/**
@@ -127,6 +134,9 @@ export type GitBookSiteContext = GitBookSpaceContext & {
127134

128135
/** Context ID used by adaptive content. It represents an unique identifier for the authentication context */
129136
contextId?: string;
137+
138+
/** Whether this request is a fallback rendering. */
139+
isFallback: boolean;
130140
};
131141

132142
/**
@@ -205,6 +215,7 @@ export async function fetchSiteContextByURLLookup(
205215
changeRequest: data.changeRequest,
206216
revision: data.revision,
207217
contextId: data.contextId,
218+
isFallback: data.isFallback ?? false,
208219
});
209220
}
210221

@@ -223,6 +234,7 @@ export async function fetchSiteContextByIds(
223234
changeRequest: string | undefined;
224235
revision: string | undefined;
225236
contextId?: string;
237+
isFallback: boolean;
226238
}
227239
): Promise<GitBookSiteContext> {
228240
const { dataFetcher } = baseContext;
@@ -321,6 +333,7 @@ export async function fetchSiteContextByIds(
321333
sections,
322334
scripts,
323335
contextId: ids.contextId,
336+
isFallback: ids.isFallback,
324337
};
325338
}
326339

packages/gitbook/src/middleware.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
298298
apiToken: siteURLData.apiToken,
299299
imagesContextId: imagesContextId,
300300
contextId: siteURLData.contextId,
301+
isFallback: requestURL.searchParams.get('fallback') === 'true' ? true : undefined,
301302
};
302303

303304
const requestHeaders = new Headers(request.headers);
@@ -382,7 +383,11 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
382383
].join('/');
383384

384385
const rewrittenURL = new URL(`/${route}`, request.nextUrl.toString());
385-
rewrittenURL.search = request.nextUrl.search; // Preserve the original search params
386+
// Preserve the original search params but remove fallback=true if present
387+
rewrittenURL.search = request.nextUrl.search;
388+
if (rewrittenURL.searchParams.has('fallback')) {
389+
rewrittenURL.searchParams.delete('fallback');
390+
}
386391

387392
const response = NextResponse.rewrite(rewrittenURL, {
388393
request: {

0 commit comments

Comments
 (0)