Skip to content

Commit 8701b5e

Browse files
authored
Use the site canonical URL when rewriting in the middleware (#3060)
1 parent 61db166 commit 8701b5e

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262
},
263263
"overrides": {
264264
"@codemirror/state": "6.4.1",
265-
"@gitbook/api": "0.102.0",
265+
"@gitbook/api": "0.104.0",
266266
"react": "18.3.1",
267267
"react-dom": "18.3.1",
268268
},
@@ -623,7 +623,7 @@
623623

624624
"@fortawesome/fontawesome-svg-core": ["@fortawesome/[email protected]", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],
625625

626-
"@gitbook/api": ["@gitbook/api@0.102.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-9wPr5kyCHhTTwkaCYEMT4q3JjEl1q9mytgvpeusmeREfG4E0RZHYPEL8bmumO7T9wH/0vHjpfW2LSTUUs60AwQ=="],
626+
"@gitbook/api": ["@gitbook/api@0.104.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-Y+Z2K5uFZCUOFiaFzvkZQaiTFCttrNc+yRjf0uAtQVBYKn245FzVTi55q/DqdFipJ83D5dY4sknclQs9qwi+Mg=="],
627627

628628
"@gitbook/cache-do": ["@gitbook/cache-do@workspace:packages/cache-do"],
629629

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@codemirror/state": "6.4.1",
1313
"react": "18.3.1",
1414
"react-dom": "18.3.1",
15-
"@gitbook/api": "0.102.0"
15+
"@gitbook/api": "0.104.0"
1616
},
1717
"private": true,
1818
"scripts": {

packages/gitbook-v2/src/middleware.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
152152
);
153153
}
154154

155+
// We use the host/origin from the canonical URL to ensure the links are
156+
// correctly generated when the site is proxied. e.g. https://proxy.gitbook.com/site/siteId/...
157+
const siteCanonicalURL = new URL(siteURLData.canonicalUrl);
158+
155159
//
156160
// Render and serve the content
157161
//
@@ -163,7 +167,10 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
163167
const requestHeaders = new Headers(request.headers);
164168
requestHeaders.set(MiddlewareHeaders.RouteType, routeType);
165169
requestHeaders.set(MiddlewareHeaders.URLMode, mode);
166-
requestHeaders.set(MiddlewareHeaders.SiteURL, `${siteURL.origin}${siteURLData.basePath}`);
170+
requestHeaders.set(
171+
MiddlewareHeaders.SiteURL,
172+
`${siteCanonicalURL.origin}${siteURLData.basePath}`
173+
);
167174
requestHeaders.set(MiddlewareHeaders.SiteURLData, JSON.stringify(siteURLData));
168175

169176
// Preview of customization/theme
@@ -188,15 +195,15 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
188195
requestHeaders.set('x-forwarded-host', request.nextUrl.host);
189196
requestHeaders.set('origin', request.nextUrl.origin);
190197

191-
const siteURLWithoutProtocol = `${siteURL.host}${siteURLData.basePath}`;
198+
const siteURLWithoutProtocol = `${siteCanonicalURL.host}${siteURLData.basePath}`;
192199
const { pathname, routeType: routeTypeFromPathname } = encodePathInSiteContent(
193200
siteURLData.pathname
194201
);
195202
routeType = routeTypeFromPathname ?? routeType;
196203

197204
// We pick only stable data from the siteURL data to prevent re-rendering of
198205
// the root layout when changing pages..
199-
const stableSiteURLData: Omit<typeof siteURLData, 'pathname'> = {
206+
const stableSiteURLData: Omit<typeof siteURLData, 'pathname' | 'canonicalUrl'> = {
200207
site: siteURLData.site,
201208
siteSection: siteURLData.siteSection,
202209
siteSpace: siteURLData.siteSpace,

packages/gitbook/src/fonts/custom.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { generateFontFacesCSS, getFontSourcesToPreload } from './custom';
66
const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
77
basic: {
88
id: 'open-sans',
9+
custom: true,
910
fontFamily: 'Open Sans',
1011
fontFaces: [
1112
{
@@ -31,6 +32,7 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
3132

3233
multiWeight: {
3334
id: 'roboto',
35+
custom: true,
3436
fontFamily: 'Roboto',
3537
fontFaces: [
3638
{
@@ -83,6 +85,7 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
8385

8486
multiSource: {
8587
id: 'lato',
88+
custom: true,
8689
fontFamily: 'Lato',
8790
fontFaces: [
8891
{
@@ -100,6 +103,7 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
100103

101104
missingFormat: {
102105
id: 'source-sans',
106+
custom: true,
103107
fontFamily: 'Source Sans Pro',
104108
fontFaces: [
105109
{
@@ -117,12 +121,14 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
117121

118122
empty: {
119123
id: 'empty-font',
124+
custom: true,
120125
fontFamily: 'Empty Font',
121126
fontFaces: [],
122127
},
123128

124129
specialChars: {
125130
id: 'special-font',
131+
custom: true,
126132
fontFamily: 'Special Font & Co.',
127133
fontFaces: [
128134
{
@@ -134,6 +140,7 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
134140

135141
complex: {
136142
id: 'complex-font',
143+
custom: true,
137144
fontFamily: 'Complex Font',
138145
fontFaces: [
139146
{
@@ -155,6 +162,7 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
155162

156163
variousURLs: {
157164
id: 'various-urls',
165+
custom: true,
158166
fontFamily: 'Various URLs Font',
159167
fontFaces: [
160168
{

packages/gitbook/src/middleware.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ async function lookupSiteOrSpaceInMultiIdMode(
564564
apiEndpoint,
565565
contextId,
566566
cookies,
567+
canonicalUrl: url.toString(),
567568
};
568569
}
569570

0 commit comments

Comments
 (0)