Skip to content

Commit e2afc07

Browse files
authored
Fix resolution of page by resolving site redirects before space redirects (#3414)
1 parent e8fb84d commit e2afc07

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

.changeset/green-bulldogs-punch.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 resolution of page by resolving site redirects before space redirects

packages/gitbook/src/components/SitePage/fetch.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,12 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams |
5353

5454
// We don't test path that are too long as GitBook doesn't support them and will return a 404 anyway.
5555
if (rawPathname.length <= 512) {
56-
// If page can't be found, we try with the API, in case we have a redirect at space level.
57-
// We use the raw pathname to handle special/malformed redirects setup by users in the GitSync.
58-
// The page rendering will take care of redirecting to a normalized pathname.
59-
const resolved = await getDataOrNull(
60-
context.dataFetcher.getRevisionPageByPath({
61-
spaceId: space.id,
62-
revisionId: revisionId,
63-
path: rawPathname,
64-
})
65-
);
66-
if (resolved) {
67-
return resolvePageId(revision.pages, resolved.id);
68-
}
69-
70-
// If a page still can't be found, we try with the API, in case we have a redirect at site level.
56+
// Duplicated the regex pattern from SiteRedirectSourcePath API type.
57+
const SITE_REDIRECT_SOURCE_PATH_REGEX =
58+
/^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/;
7159
const redirectPathname = withLeadingSlash(rawPathname);
72-
if (
73-
/^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/.test(
74-
redirectPathname
75-
)
76-
) {
60+
// If a page can't be found, we try with the API, in case we have a redirect at site level.
61+
if (SITE_REDIRECT_SOURCE_PATH_REGEX.test(redirectPathname)) {
7762
const redirectSources = new Set<string>([
7863
// Test the pathname relative to the root
7964
// For example hello/world -> section/variant/hello/world
@@ -98,6 +83,20 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams |
9883
}
9984
}
10085
}
86+
87+
// If page still can't be found, we try with the API, in case we have a redirect at space level.
88+
// We use the raw pathname to handle special/malformed redirects setup by users in the GitSync.
89+
// The page rendering will take care of redirecting to a normalized pathname.
90+
const resolved = await getDataOrNull(
91+
context.dataFetcher.getRevisionPageByPath({
92+
spaceId: space.id,
93+
revisionId: revisionId,
94+
path: rawPathname,
95+
})
96+
);
97+
if (resolved) {
98+
return resolvePageId(revision.pages, resolved.id);
99+
}
101100
}
102101

103102
return undefined;

0 commit comments

Comments
 (0)