Skip to content

Commit e5366d3

Browse files
authored
v2: fix error handling in middleware and data fetcher (#2925)
1 parent 38e84ff commit e5366d3

File tree

22 files changed

+547
-360
lines changed

22 files changed

+547
-360
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ jobs:
243243
runs-on: ubuntu-latest
244244
name: Visual Testing v2
245245
needs: deploy-v2-vercel
246-
timeout-minutes: 8
246+
timeout-minutes: 10
247247
steps:
248248
- name: Checkout
249249
uses: actions/checkout@v4

bun.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"@gitbook/api": "0.96.1",
138138
"@gitbook/cache-tags": "workspace:*",
139139
"@sindresorhus/fnv1a": "^3.1.0",
140-
"next": "^15.2.0",
140+
"next": "^15.2.1",
141141
"react": "^19.0.0",
142142
"react-dom": "^19.0.0",
143143
"server-only": "^0.0.1",
@@ -4280,7 +4280,7 @@
42804280

42814281
"gaxios/node-fetch": ["[email protected]", "", { "dependencies": { "whatwg-url": "^5.0.0" }, "peerDependencies": { "encoding": "^0.1.0" }, "optionalPeers": ["encoding"] }, "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A=="],
42824282

4283-
"gitbook-v2/next": ["[email protected]", "", { "dependencies": { "@next/env": "15.2.0", "@swc/counter": "0.1.3", "@swc/helpers": "0.5.15", "busboy": "1.6.0", "caniuse-lite": "^1.0.30001579", "postcss": "8.4.31", "styled-jsx": "5.1.6" }, "optionalDependencies": { "@next/swc-darwin-arm64": "15.2.0", "@next/swc-darwin-x64": "15.2.0", "@next/swc-linux-arm64-gnu": "15.2.0", "@next/swc-linux-arm64-musl": "15.2.0", "@next/swc-linux-x64-gnu": "15.2.0", "@next/swc-linux-x64-musl": "15.2.0", "@next/swc-win32-arm64-msvc": "15.2.0", "@next/swc-win32-x64-msvc": "15.2.0", "sharp": "^0.33.5" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", "@playwright/test": "^1.41.2", "babel-plugin-react-compiler": "*", "react": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "react-dom": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "sass": "^1.3.0" }, "optionalPeers": ["@opentelemetry/api", "@playwright/test", "babel-plugin-react-compiler", "sass"], "bin": { "next": "dist/bin/next" } }, "sha512-VaiM7sZYX8KIAHBrRGSFytKknkrexNfGb8GlG6e93JqueCspuGte8i4ybn8z4ww1x3f2uzY4YpTaBEW4/hvsoQ=="],
4283+
"gitbook-v2/next": ["[email protected]", "", { "dependencies": { "@next/env": "15.2.1", "@swc/counter": "0.1.3", "@swc/helpers": "0.5.15", "busboy": "1.6.0", "caniuse-lite": "^1.0.30001579", "postcss": "8.4.31", "styled-jsx": "5.1.6" }, "optionalDependencies": { "@next/swc-darwin-arm64": "15.2.1", "@next/swc-darwin-x64": "15.2.1", "@next/swc-linux-arm64-gnu": "15.2.1", "@next/swc-linux-arm64-musl": "15.2.1", "@next/swc-linux-x64-gnu": "15.2.1", "@next/swc-linux-x64-musl": "15.2.1", "@next/swc-win32-arm64-msvc": "15.2.1", "@next/swc-win32-x64-msvc": "15.2.1", "sharp": "^0.33.5" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", "@playwright/test": "^1.41.2", "babel-plugin-react-compiler": "*", "react": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "react-dom": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "sass": "^1.3.0" }, "optionalPeers": ["@opentelemetry/api", "@playwright/test", "babel-plugin-react-compiler", "sass"], "bin": { "next": "dist/bin/next" } }, "sha512-zxbsdQv3OqWXybK5tMkPCBKyhIz63RstJ+NvlfkaLMc/m5MwXgz2e92k+hSKcyBpyADhMk2C31RIiaDjUZae7g=="],
42844284

42854285
"google-auth-library/jws": ["[email protected]", "", { "dependencies": { "jwa": "^2.0.0", "safe-buffer": "^5.0.1" } }, "sha512-KDncfTmOZoOMTFG4mBlG0qUIOlc03fmzH+ru6RgYVZhPkyiy/92Owlt/8UEN+a4TXR1FQetfIpJE8ApdvdVxTg=="],
42864286

@@ -5180,23 +5180,23 @@
51805180

51815181
"gaxios/https-proxy-agent/debug": ["[email protected]", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ=="],
51825182

5183-
"gitbook-v2/next/@next/env": ["@next/[email protected].0", "", {}, "sha512-eMgJu1RBXxxqqnuRJQh5RozhskoNUDHBFybvi+Z+yK9qzKeG7dadhv/Vp1YooSZmCnegf7JxWuapV77necLZNA=="],
5183+
"gitbook-v2/next/@next/env": ["@next/[email protected].1", "", {}, "sha512-JmY0qvnPuS2NCWOz2bbby3Pe0VzdAQ7XpEB6uLIHmtXNfAsAO0KLQLkuAoc42Bxbo3/jMC3dcn9cdf+piCcG2Q=="],
51845184

5185-
"gitbook-v2/next/@next/swc-darwin-arm64": ["@next/[email protected].0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-rlp22GZwNJjFCyL7h5wz9vtpBVuCt3ZYjFWpEPBGzG712/uL1bbSkS675rVAUCRZ4hjoTJ26Q7IKhr5DfJrHDA=="],
5185+
"gitbook-v2/next/@next/swc-darwin-arm64": ["@next/[email protected].1", "", { "os": "darwin", "cpu": "arm64" }, "sha512-aWXT+5KEREoy3K5AKtiKwioeblmOvFFjd+F3dVleLvvLiQ/mD//jOOuUcx5hzcO9ISSw4lrqtUPntTpK32uXXQ=="],
51865186

5187-
"gitbook-v2/next/@next/swc-darwin-x64": ["@next/[email protected].0", "", { "os": "darwin", "cpu": "x64" }, "sha512-DiU85EqSHogCz80+sgsx90/ecygfCSGl5P3b4XDRVZpgujBm5lp4ts7YaHru7eVTyZMjHInzKr+w0/7+qDrvMA=="],
5187+
"gitbook-v2/next/@next/swc-darwin-x64": ["@next/[email protected].1", "", { "os": "darwin", "cpu": "x64" }, "sha512-E/w8ervu4fcG5SkLhvn1NE/2POuDCDEy5gFbfhmnYXkyONZR68qbUlJlZwuN82o7BrBVAw+tkR8nTIjGiMW1jQ=="],
51885188

5189-
"gitbook-v2/next/@next/swc-linux-arm64-gnu": ["@next/[email protected].0", "", { "os": "linux", "cpu": "arm64" }, "sha512-VnpoMaGukiNWVxeqKHwi8MN47yKGyki5q+7ql/7p/3ifuU2341i/gDwGK1rivk0pVYbdv5D8z63uu9yMw0QhpQ=="],
5189+
"gitbook-v2/next/@next/swc-linux-arm64-gnu": ["@next/[email protected].1", "", { "os": "linux", "cpu": "arm64" }, "sha512-gXDX5lIboebbjhiMT6kFgu4svQyjoSed6dHyjx5uZsjlvTwOAnZpn13w9XDaIMFFHw7K8CpBK7HfDKw0VZvUXQ=="],
51905190

5191-
"gitbook-v2/next/@next/swc-linux-arm64-musl": ["@next/[email protected].0", "", { "os": "linux", "cpu": "arm64" }, "sha512-ka97/ssYE5nPH4Qs+8bd8RlYeNeUVBhcnsNUmFM6VWEob4jfN9FTr0NBhXVi1XEJpj3cMfgSRW+LdE3SUZbPrw=="],
5191+
"gitbook-v2/next/@next/swc-linux-arm64-musl": ["@next/[email protected].1", "", { "os": "linux", "cpu": "arm64" }, "sha512-3v0pF/adKZkBWfUffmB/ROa+QcNTrnmYG4/SS+r52HPwAK479XcWoES2I+7F7lcbqc7mTeVXrIvb4h6rR/iDKg=="],
51925192

5193-
"gitbook-v2/next/@next/swc-linux-x64-gnu": ["@next/[email protected].0", "", { "os": "linux", "cpu": "x64" }, "sha512-zY1JduE4B3q0k2ZCE+DAF/1efjTXUsKP+VXRtrt/rJCTgDlUyyryx7aOgYXNc1d8gobys/Lof9P9ze8IyRDn7Q=="],
5193+
"gitbook-v2/next/@next/swc-linux-x64-gnu": ["@next/[email protected].1", "", { "os": "linux", "cpu": "x64" }, "sha512-RbsVq2iB6KFJRZ2cHrU67jLVLKeuOIhnQB05ygu5fCNgg8oTewxweJE8XlLV+Ii6Y6u4EHwETdUiRNXIAfpBww=="],
51945194

5195-
"gitbook-v2/next/@next/swc-linux-x64-musl": ["@next/[email protected].0", "", { "os": "linux", "cpu": "x64" }, "sha512-QqvLZpurBD46RhaVaVBepkVQzh8xtlUN00RlG4Iq1sBheNugamUNPuZEH1r9X1YGQo1KqAe1iiShF0acva3jHQ=="],
5195+
"gitbook-v2/next/@next/swc-linux-x64-musl": ["@next/[email protected].1", "", { "os": "linux", "cpu": "x64" }, "sha512-QHsMLAyAIu6/fWjHmkN/F78EFPKmhQlyX5C8pRIS2RwVA7z+t9cTb0IaYWC3EHLOTjsU7MNQW+n2xGXr11QPpg=="],
51965196

5197-
"gitbook-v2/next/@next/swc-win32-arm64-msvc": ["@next/[email protected].0", "", { "os": "win32", "cpu": "arm64" }, "sha512-ODZ0r9WMyylTHAN6pLtvUtQlGXBL9voljv6ujSlcsjOxhtXPI1Ag6AhZK0SE8hEpR1374WZZ5w33ChpJd5fsjw=="],
5197+
"gitbook-v2/next/@next/swc-win32-arm64-msvc": ["@next/[email protected].1", "", { "os": "win32", "cpu": "arm64" }, "sha512-Gk42XZXo1cE89i3hPLa/9KZ8OuupTjkDmhLaMKFohjf9brOeZVEa3BQy1J9s9TWUqPhgAEbwv6B2+ciGfe54Vw=="],
51985198

5199-
"gitbook-v2/next/@next/swc-win32-x64-msvc": ["@next/[email protected].0", "", { "os": "win32", "cpu": "x64" }, "sha512-8+4Z3Z7xa13NdUuUAcpVNA6o76lNPniBd9Xbo02bwXQXnZgFvEopwY2at5+z7yHl47X9qbZpvwatZ2BRo3EdZw=="],
5199+
"gitbook-v2/next/@next/swc-win32-x64-msvc": ["@next/[email protected].1", "", { "os": "win32", "cpu": "x64" }, "sha512-YjqXCl8QGhVlMR8uBftWk0iTmvtntr41PhG1kvzGp0sUP/5ehTM+cwx25hKE54J0CRnHYjSGjSH3gkHEaHIN9g=="],
52005200

52015201
"gitbook-v2/next/postcss": ["[email protected]", "", { "dependencies": { "nanoid": "^3.3.6", "picocolors": "^1.0.0", "source-map-js": "^1.0.2" } }, "sha512-PS08Iboia9mts/2ygV3eLpY5ghnUcfLV/EXTOW1E2qYxJKGGBUtNjN76FYHnMs36RmARn41bC0AZmn+rR0OVpQ=="],
52025202

packages/gitbook-v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.1.2",
44
"private": true,
55
"dependencies": {
6-
"next": "^15.2.0",
6+
"next": "^15.2.1",
77
"react": "^19.0.0",
88
"react-dom": "^19.0.0",
99
"@gitbook/api": "0.96.1",

packages/gitbook-v2/src/app/sites/dynamic/[mode]/[siteURL]/~gitbook/icon/route.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type { NextRequest } from 'next/server';
33
import { serveIcon } from '@/routes/icon';
44
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
55

6-
export const dynamic = 'force-static';
7-
86
export async function GET(
97
request: NextRequest,
108
{ params }: { params: Promise<RouteLayoutParams> }

packages/gitbook-v2/src/lib/context.ts

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
SiteStructure,
1414
Space,
1515
} from '@gitbook/api';
16-
import { type GitBookDataFetcher, createDataFetcher } from '@v2/lib/data';
16+
import { type GitBookDataFetcher, createDataFetcher, throwIfDataError } from '@v2/lib/data';
1717
import { redirect } from 'next/navigation';
1818
import { assert } from 'ts-essentials';
1919
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_URL } from './env';
@@ -164,11 +164,13 @@ export async function fetchSiteContextByURL(
164164
}
165165
): Promise<GitBookSiteContext> {
166166
const { dataFetcher } = baseContext;
167-
const data = await dataFetcher.getPublishedContentByUrl({
168-
url: input.url,
169-
visitorAuthToken: input.visitorAuthToken,
170-
redirectOnError: input.redirectOnError,
171-
});
167+
const data = await throwIfDataError(
168+
dataFetcher.getPublishedContentByUrl({
169+
url: input.url,
170+
visitorAuthToken: input.visitorAuthToken,
171+
redirectOnError: input.redirectOnError,
172+
})
173+
);
172174

173175
return fetchSiteContextByURLLookup(baseContext, data);
174176
}
@@ -225,11 +227,13 @@ export async function fetchSiteContextByIds(
225227

226228
const [{ site: orgSite, structure: siteStructure, customizations, scripts }, spaceContext] =
227229
await Promise.all([
228-
dataFetcher.getPublishedContentSite({
229-
organizationId: ids.organization,
230-
siteId: ids.site,
231-
siteShareKey: ids.shareKey,
232-
}),
230+
throwIfDataError(
231+
dataFetcher.getPublishedContentSite({
232+
organizationId: ids.organization,
233+
siteId: ids.site,
234+
siteShareKey: ids.shareKey,
235+
})
236+
),
233237
fetchSpaceContextByIds(baseContext, ids),
234238
]);
235239

@@ -302,27 +306,33 @@ export async function fetchSpaceContextByIds(
302306
const { dataFetcher } = baseContext;
303307

304308
const [space, changeRequest] = await Promise.all([
305-
dataFetcher.getSpace({
306-
spaceId: ids.space,
307-
shareKey: ids.shareKey,
308-
}),
309+
throwIfDataError(
310+
dataFetcher.getSpace({
311+
spaceId: ids.space,
312+
shareKey: ids.shareKey,
313+
})
314+
),
309315
ids.changeRequest
310-
? dataFetcher.getChangeRequest({
311-
spaceId: ids.space,
312-
changeRequestId: ids.changeRequest,
313-
})
316+
? throwIfDataError(
317+
dataFetcher.getChangeRequest({
318+
spaceId: ids.space,
319+
changeRequestId: ids.changeRequest,
320+
})
321+
)
314322
: null,
315323
]);
316324

317325
const revisionId = changeRequest?.revision ?? ids.revision ?? space.revision;
318326

319-
const pages = await dataFetcher.getRevisionPages({
320-
spaceId: ids.space,
321-
revisionId,
322-
// We only care about the Git metadata when the Git sync is enabled,
323-
// otherwise we can optimize performance by not fetching it
324-
metadata: !!space.gitSync,
325-
});
327+
const pages = await throwIfDataError(
328+
dataFetcher.getRevisionPages({
329+
spaceId: ids.space,
330+
revisionId,
331+
// We only care about the Git metadata when the Git sync is enabled,
332+
// otherwise we can optimize performance by not fetching it
333+
metadata: !!space.gitSync,
334+
})
335+
);
326336

327337
return {
328338
...baseContext,

0 commit comments

Comments
 (0)