Skip to content

Commit 73e0cbb

Browse files
authored
Fix an issue where PDF export URLs were not bringing their query params. (#3351)
1 parent 392f594 commit 73e0cbb

File tree

6 files changed

+180
-3
lines changed

6 files changed

+180
-3
lines changed

.changeset/rich-buses-hunt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook-v2": patch
3+
---
4+
5+
Fix an issue where PDF export URLs were not keeping their query params.

packages/gitbook-v2/src/middleware.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
259259
console.log(`rewriting ${request.nextUrl.toString()} to ${route}`);
260260

261261
const rewrittenURL = new URL(`/${route}`, request.nextUrl.toString());
262+
rewrittenURL.search = request.nextUrl.search; // Preserve the original search params
263+
262264
const response = NextResponse.rewrite(rewrittenURL, {
263265
request: {
264266
headers: requestHeaders,

packages/gitbook/e2e/pdf.spec.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { argosScreenshot } from '@argos-ci/playwright';
2+
import { expect, test } from '@playwright/test';
3+
import { getContentTestURL } from '../tests/utils';
4+
import { waitForIcons } from './util';
5+
6+
test.describe('PDF export', () => {
7+
test('export all pages as PDF (e2e)', async ({ page }) => {
8+
// Set the header to disable the Vercel toolbar
9+
// But only on the main document as it'd cause CORS issues on other resources
10+
await page.route('**/*', async (route, request) => {
11+
if (request.resourceType() === 'document') {
12+
await route.continue({
13+
headers: {
14+
...request.headers(),
15+
'x-vercel-skip-toolbar': '1',
16+
},
17+
});
18+
} else {
19+
await route.continue();
20+
}
21+
});
22+
23+
await page.goto(
24+
getContentTestURL(
25+
'https://gitbook-open-e2e-sites.gitbook.io/gitbook-doc/~gitbook/pdf?limit=10'
26+
)
27+
);
28+
29+
const printBtn = page.getByTestId('print-button');
30+
await expect(printBtn).toBeVisible();
31+
32+
await argosScreenshot(page, 'pdf - all pages', {
33+
viewports: ['macbook-13'],
34+
argosCSS: `
35+
/* Hide Intercom */
36+
.intercom-lightweight-app {
37+
display: none !important;
38+
}
39+
`,
40+
threshold: undefined,
41+
fullPage: true,
42+
beforeScreenshot: async ({ runStabilization }) => {
43+
await runStabilization();
44+
await waitForIcons(page);
45+
},
46+
});
47+
});
48+
49+
test('export all pages as PDF (GitBook docs)', async ({ page }) => {
50+
// Set the header to disable the Vercel toolbar
51+
// But only on the main document as it'd cause CORS issues on other resources
52+
await page.route('**/*', async (route, request) => {
53+
if (request.resourceType() === 'document') {
54+
await route.continue({
55+
headers: {
56+
...request.headers(),
57+
'x-vercel-skip-toolbar': '1',
58+
},
59+
});
60+
} else {
61+
await route.continue();
62+
}
63+
});
64+
65+
await page.goto(getContentTestURL('https://gitbook.com/docs/~gitbook/pdf?limit=10'));
66+
67+
const printBtn = page.getByTestId('print-button');
68+
await expect(printBtn).toBeVisible();
69+
70+
await argosScreenshot(page, 'pdf - all pages', {
71+
viewports: ['macbook-13'],
72+
argosCSS: `
73+
/* Hide Intercom */
74+
.intercom-lightweight-app {
75+
display: none !important;
76+
}
77+
`,
78+
threshold: undefined,
79+
fullPage: true,
80+
beforeScreenshot: async ({ runStabilization }) => {
81+
await runStabilization();
82+
await waitForIcons(page);
83+
},
84+
});
85+
});
86+
87+
test('export a single page as PDF (e2e)', async ({ page }) => {
88+
// Set the header to disable the Vercel toolbar
89+
// But only on the main document as it'd cause CORS issues on other resources
90+
await page.route('**/*', async (route, request) => {
91+
if (request.resourceType() === 'document') {
92+
await route.continue({
93+
headers: {
94+
...request.headers(),
95+
'x-vercel-skip-toolbar': '1',
96+
},
97+
});
98+
} else {
99+
await route.continue();
100+
}
101+
});
102+
103+
await page.goto(
104+
getContentTestURL(
105+
'https://gitbook-open-e2e-sites.gitbook.io/gitbook-doc/~gitbook/pdf?page=Bw7LjWwgTjV8nIV4s7rs&only=yes&limit=2'
106+
)
107+
);
108+
109+
const printBtn = page.getByTestId('print-button');
110+
await expect(printBtn).toBeVisible();
111+
112+
await argosScreenshot(page, 'pdf - all pages', {
113+
viewports: ['macbook-13'],
114+
argosCSS: `
115+
/* Hide Intercom */
116+
.intercom-lightweight-app {
117+
display: none !important;
118+
}
119+
`,
120+
threshold: undefined,
121+
fullPage: true,
122+
beforeScreenshot: async ({ runStabilization }) => {
123+
await runStabilization();
124+
await waitForIcons(page);
125+
},
126+
});
127+
});
128+
129+
test('export a single page as PDF (GitBook docs)', async ({ page }) => {
130+
// Set the header to disable the Vercel toolbar
131+
// But only on the main document as it'd cause CORS issues on other resources
132+
await page.route('**/*', async (route, request) => {
133+
if (request.resourceType() === 'document') {
134+
await route.continue({
135+
headers: {
136+
...request.headers(),
137+
'x-vercel-skip-toolbar': '1',
138+
},
139+
});
140+
} else {
141+
await route.continue();
142+
}
143+
});
144+
145+
await page.goto(
146+
getContentTestURL(
147+
'https://gitbook.com/docs/~gitbook/pdf?page=DfnNkU49mvLe2ythHAyx&only=yes&limit=2'
148+
)
149+
);
150+
151+
const printBtn = page.getByTestId('print-button');
152+
await expect(printBtn).toBeVisible();
153+
154+
await argosScreenshot(page, 'pdf - all pages', {
155+
viewports: ['macbook-13'],
156+
argosCSS: `
157+
/* Hide Intercom */
158+
.intercom-lightweight-app {
159+
display: none !important;
160+
}
161+
`,
162+
threshold: undefined,
163+
fullPage: true,
164+
beforeScreenshot: async ({ runStabilization }) => {
165+
await runStabilization();
166+
await waitForIcons(page);
167+
},
168+
});
169+
});
170+
});

packages/gitbook/e2e/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export function getCustomizationURL(partial: DeepPartial<SiteCustomizationSettin
346346
/**
347347
* Wait for all icons present on the page to be loaded.
348348
*/
349-
async function waitForIcons(page: Page) {
349+
export async function waitForIcons(page: Page) {
350350
await page.waitForFunction(() => {
351351
const urlStates: Record<
352352
string,

packages/gitbook/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build:cloudflare": "next-on-pages --custom-entrypoint=./src/cloudflare-entrypoint.ts",
99
"start": "next start",
1010
"typecheck": "tsc --noEmit",
11-
"e2e": "playwright test e2e/internal.spec.ts",
11+
"e2e": "playwright test e2e/internal.spec.ts e2e/pdf.spec.ts",
1212
"e2e-customers": "playwright test e2e/customers.spec.ts",
1313
"unit": "bun test {src,packages} --preload ./tests/preload-bun.ts",
1414
"generate": "gitbook-icons ./public/~gitbook/static/icons custom-icons && gitbook-math ./public/~gitbook/static/math",

packages/gitbook/src/components/PDF/PDFPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export async function PDFPage(props: {
5353
}) {
5454
const baseContext = props.context;
5555
const searchParams = new URLSearchParams(props.searchParams);
56-
const pdfParams = getPDFSearchParams(new URLSearchParams(searchParams));
56+
const pdfParams = getPDFSearchParams(searchParams);
5757

5858
const customization =
5959
'customization' in baseContext ? baseContext.customization : defaultCustomization();

0 commit comments

Comments
 (0)