Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/gitbook/src/components/SitePage/SitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export async function generateSitePageMetadata(props: SitePageProps): Promise<Me
});

if (!pageTarget) {
if (context.isFallback) {
redirect(context.linker.toPathInSpace('/'));
}
notFound();
}

Expand Down Expand Up @@ -154,6 +157,10 @@ export async function getSitePageData(props: SitePageProps) {
// before trying to resolve the page again
redirect(context.linker.toPathInSpace(pathname));
} else {
// If the page is not found and we are in fallback mode, return a redirect to the basepath
if (context.isFallback) {
redirect(context.linker.toPathInSpace('/'));
}
notFound();
}
} else if (getPagePath(context.revision.pages, pageTarget.page) !== rawPathname) {
Expand Down
13 changes: 13 additions & 0 deletions packages/gitbook/src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export type SiteURLData = Pick<
* Identifier used for image resizing.
*/
imagesContextId: string;

/**
* Whether this request is a fallback rendering.
* We use this when switching variant as we don't know if the page exists in the other variant.
* By knowing it's a fallback, we can redirect to the space base path instead of returning a 404.
*/
isFallback?: boolean;
};

/**
Expand Down Expand Up @@ -127,6 +134,9 @@ export type GitBookSiteContext = GitBookSpaceContext & {

/** Context ID used by adaptive content. It represents an unique identifier for the authentication context */
contextId?: string;

/** Whether this request is a fallback rendering. */
isFallback: boolean;
};

/**
Expand Down Expand Up @@ -205,6 +215,7 @@ export async function fetchSiteContextByURLLookup(
changeRequest: data.changeRequest,
revision: data.revision,
contextId: data.contextId,
isFallback: data.isFallback ?? false,
});
}

Expand All @@ -223,6 +234,7 @@ export async function fetchSiteContextByIds(
changeRequest: string | undefined;
revision: string | undefined;
contextId?: string;
isFallback: boolean;
}
): Promise<GitBookSiteContext> {
const { dataFetcher } = baseContext;
Expand Down Expand Up @@ -321,6 +333,7 @@ export async function fetchSiteContextByIds(
sections,
scripts,
contextId: ids.contextId,
isFallback: ids.isFallback,
};
}

Expand Down
7 changes: 6 additions & 1 deletion packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
apiToken: siteURLData.apiToken,
imagesContextId: imagesContextId,
contextId: siteURLData.contextId,
isFallback: requestURL.searchParams.get('fallback') === 'true' ? true : undefined,
};

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

const rewrittenURL = new URL(`/${route}`, request.nextUrl.toString());
rewrittenURL.search = request.nextUrl.search; // Preserve the original search params
// Preserve the original search params but remove fallback=true if present
rewrittenURL.search = request.nextUrl.search;
if (rewrittenURL.searchParams.has('fallback')) {
rewrittenURL.searchParams.delete('fallback');
}

const response = NextResponse.rewrite(rewrittenURL, {
request: {
Expand Down