@@ -53,27 +53,12 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams |
53
53
54
54
// We don't test path that are too long as GitBook doesn't support them and will return a 404 anyway.
55
55
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 - Z a - z 0 - 9 \- . _ ~ ] | % [ 0 - 9 A - F a - f ] { 2 } ) + (?: \/ (?: [ A - Z a - z 0 - 9 \- . _ ~ ] | % [ 0 - 9 A - F a - f ] { 2 } ) + ) * $ / ;
71
59
const redirectPathname = withLeadingSlash ( rawPathname ) ;
72
- if (
73
- / ^ \/ (?: [ A - Z a - z 0 - 9 \- . _ ~ ] | % [ 0 - 9 A - F a - f ] { 2 } ) + (?: \/ (?: [ A - Z a - z 0 - 9 \- . _ ~ ] | % [ 0 - 9 A - F a - 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 ) ) {
77
62
const redirectSources = new Set < string > ( [
78
63
// Test the pathname relative to the root
79
64
// For example hello/world -> section/variant/hello/world
@@ -98,6 +83,20 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams |
98
83
}
99
84
}
100
85
}
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
+ }
101
100
}
102
101
103
102
return undefined ;
0 commit comments