Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to resolve site redirect with the full path first #2971

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
21 changes: 17 additions & 4 deletions packages/gitbook/src/components/SitePage/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,30 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams |
return resolvePageId(pages, resolved.id);
}

// If a page still can't be found, we try with the API, in case we have a redirect at site level.
const resolvedSiteRedirect = await getDataOrNull(
// We first try to resovle a site redirect with the full path
const resolvedSiteRedirectFull = await getDataOrNull(
context.dataFetcher.getSiteRedirectBySource({
organizationId,
siteId: site.id,
source: context.linker.toPathInContent(rawPathname),
siteShareKey: shareKey,
})
);
if (resolvedSiteRedirectFull) {
return redirect(resolvedSiteRedirectFull.target);
}

// If a page still can't be found, we try to resolve a site redirect with the partial path
const resolvedSiteRedirectPartial = await getDataOrNull(
context.dataFetcher.getSiteRedirectBySource({
organizationId,
siteId: site.id,
source: rawPathname.startsWith('/') ? rawPathname : `/${rawPathname}`,
siteShareKey: shareKey,
})
);
if (resolvedSiteRedirect) {
return redirect(resolvedSiteRedirect.target);
if (resolvedSiteRedirectPartial) {
return redirect(resolvedSiteRedirectPartial.target);
}
}

Expand Down