Compare commits

..

18 Commits

Author SHA1 Message Date
Peter White 736195e633 Revert "Search 100 ms after the last key and keep the search route unchanged"
This reverts commit c2a25a7485.
2026-11-16 15:07:51 +00:00
Peter White c2a25a7485 Search 100 ms after the last key and keep the search route unchanged
The search debounce goes from 150 to 100 ms. The search route, the data fetcher and the Cloudflare and Vercel configuration are back to the stack's versions.
2026-11-16 15:05:50 +00:00
Peter White 5c6eecbc42 changeset 2026-11-16 14:42:27 +00:00
Peter White c1e391d824 Stop cancelled searches at the search route and search 100 ms after the last key
The route passes the browser's cancel to the API call, logs it with the organization and site IDs and returns 499. Both Cloudflare workers forward the request signal, and the Vercel search function supports cancellation. The search debounce is 100 ms.
2026-11-16 14:42:27 +00:00
Peter White 15b2f26358 Add a browser test that retrying a failed search sends it once 2026-11-15 16:40:51 +00:00
Peter White 72e5fd0dfd Merge branch 'peter/search-result-placeholders' into peter/search-request-owner 2026-11-15 16:40:51 +00:00
Peter White f0ec25da9f Add a browser test that search loading rows cannot be activated 2026-11-15 16:40:45 +00:00
Peter White 5d65f90946 Merge branch 'peter/search-server-list' into peter/search-result-placeholders 2026-11-15 16:40:45 +00:00
Peter White d9da81a794 Add browser tests for Enter while search loads, scope changes and embedded search 2026-11-15 16:40:25 +00:00
Peter White f55dc464f6 changeset 2026-11-13 15:56:01 +00:00
Peter White ab39dc74c8 Send each search once on sites without a top header
Only the search container shown on the current viewport sends requests
and logs search_type_query. Its visibility follows the lg breakpoint the
header and sidebar CSS use, so search works between 768 and 1023 px. A
hidden container drops its pending Enter.
2026-11-13 15:56:01 +00:00
Peter White 35148805e7 changeset 2026-11-13 12:57:31 +00:00
Peter White 36154a2d86 Show result-shaped placeholders while search loads 2026-11-13 12:57:31 +00:00
Peter White a018e930de Lower the search idle delay to 150 ms 2026-11-13 12:07:13 +00:00
Peter White d596bdc43b Search IME composition text like typing and send on commit 2026-11-13 11:59:40 +00:00
Peter White b1dc336f00 changeset 2026-11-13 11:50:20 +00:00
Peter White a92f11b9dd Merge remote-tracking branch 'origin/main' into peter/search-server-list 2026-11-13 11:50:19 +00:00
Peter White 1f656f1713 Show one server-ordered search list and open its top result on Enter 2026-11-13 11:50:19 +00:00
85 changed files with 552 additions and 2589 deletions
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Keep links in generated Markdown usable outside the source page.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Fade the bottom of collapsed code blocks so readers can tell there is more code, matching the editor.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Bump Next.js to 16.4.6 and OpenNext (Cloudflare 1.20.17, AWS 4.1.16), keeping the dev-mode OOM patch.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Fix `'use cache'` entries never being stored in or read from R2 and the regional cache on Cloudflare.
@@ -1,5 +0,0 @@
---
"@gitbook/icons": minor
---
Add a `@gitbook/icons/validateIconName` entry that validates icon names without loading the icon labels and search terms.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Keep inline code and highlighted text readable when a paragraph crosses the bottom edge of a background page cover.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Reduce memory and CPU use of the OpenNext workers: minify the middleware, resolve the site context once per request, cache parsed OpenAPI descriptions across a spec, drop the redis client and the unused force-revalidate route from the server bundle.
@@ -1,5 +0,0 @@
---
"@gitbook/react-openapi": patch
---
Copy the current spec's server in OpenAPI endpoint URLs instead of a server kept from a previously visited page.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Show H3 headings in the page outline.
-5
View File
@@ -1,5 +0,0 @@
---
'gitbook': patch
---
Respect disabled change request previews on published sites.
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Stop cancelled site searches at the search route, and search 100 ms after the last key.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Send site searches straight to the search API when the site hands out a search pass, and use the site's search route when it does not or when the API refuses a search.
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Stop caching site search results in the framework cache, so every search reaches the API.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Follow the API's cache lifetime for site data, so a site preview shows layout changes soon after they are saved.
-5
View File
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Render small cards in a responsive four-column grid and carousel.
@@ -1,5 +0,0 @@
---
"gitbook": patch
---
Ignore tag category references when displaying assigned tags and update filters.
-30
View File
@@ -59,36 +59,6 @@ Run a specific test file:
cd packages/gitbook && bun test src/lib/cache.test.ts
```
## Changes customers see on their live site
Published sites are our customers' product. If a change alters how their content looks or behaves without them changing anything, it needs a comms decision before it merges.
It counts if it ships on by default and any of these are true:
- It changes structure or navigation: outline/"On this page", sidebar, TOC, headings, breadcrumbs, pagination, header or footer.
- Something new appears on every page (or every page of a type), or something existing disappears or moves.
- Layout or typography shifts enough that a customer would notice in a screenshot.
- URLs, anchors, redirects, SEO/meta tags or search/Ask behaviour change.
- A default changes for existing sites.
It does not count when:
- It's behind a flag, a site setting or opt-in that stays off for existing sites until comms go out.
- It's a bug fix that restores the intended behaviour.
- It only affects the editor/app, not published content.
When it counts:
1. Warn the engineer before the PR is opened: "This changes customers' live sites. Have the support and sales leads agreed the rollout and announcement?"
2. Suggest a flag or staged rollout if there isn't one, so it can ship dark and be turned on after the notice goes out.
## Bumping Next.js or OpenNext
Type checks and unit tests miss runtime regressions: OpenNext 4.1.15 changed the composable cache key format, and every `'use cache'` call silently missed for days. Before committing a bump of `next`, `@opennextjs/aws` or `@opennextjs/cloudflare`, run both runtimes locally from `packages/gitbook`:
1. **Next**: `bun run build:local && bun run start`, then load several distinct pages under `http://localhost:3000/url/gitbook.com/docs`.
2. **OpenNext**: `bun run build:cloudflare`, delete `openNext/customWorkers/.wrangler` (stale cache hits otherwise skip the server), then run `bun run dev:cf:server` and `bun run dev:cf:middleware` and load several distinct pages under `http://127.0.0.1:8771/url/gitbook.com/docs`.
3. Diff the changed OpenNext versions' adapters that call our overrides in `openNext/` (incremental, composable and tag cache, queue), e.g. with `npm pack` of the old and new versions.
The bump is safe when every page returns 200 on both runtimes and the server logs show no cache errors (grep `Failed to` and `regional cache`). Report the pages checked and the log result in the PR.
## Changesets
After committing code changes, create a changeset for the affected package:
+45 -28
View File
@@ -8,7 +8,7 @@
"@changesets/cli": "^2.31.1",
"oxfmt": "^0.62.0",
"oxlint": "^1.93.0",
"turbo": "^2.11.11",
"turbo": "^2.11.10",
"vercel": "50.37.3",
},
},
@@ -128,8 +128,8 @@
"@gitbook/react-openapi": "workspace:*",
"@mermaid-js/mermaid-zenuml": "^0.2.2",
"@modelcontextprotocol/sdk": "1.17.5",
"@opennextjs/aws": "4.1.16",
"@opennextjs/cloudflare": "1.20.17",
"@opennextjs/aws": "4.1.15",
"@opennextjs/cloudflare": "1.20.16",
"@panzoom/panzoom": "^4.6.1",
"@sindresorhus/fnv1a": "^3.1.0",
"@tailwindcss/container-queries": "^0.1.1",
@@ -159,7 +159,7 @@
"micromark-extension-gfm": "^3.0.0",
"motion": "^12.23.24",
"negotiator": "^1.0.0",
"next": "^16.4.6",
"next": "^16.4.5",
"next-themes": "^0.4.6",
"nuqs": "^2.2.3",
"object-hash": "^3.0.0",
@@ -167,7 +167,6 @@
"openapi-types": "^12.1.3",
"p-map": "^7.0.3",
"p-retry": "^8.0.0",
"parse5": "7.2.0",
"quick-lru": "^7.0.1",
"react": "19.2.4",
"react-dom": "19.2.4",
@@ -342,9 +341,9 @@
},
"patchedDependencies": {
"decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch",
"next@16.4.5": "patches/next@16.4.5.patch",
"@shikijs/langs@3.21.0": "patches/@shikijs%2Flangs@3.21.0.patch",
"mermaid@11.16.1": "patches/mermaid@11.16.1.patch",
"next@16.4.6": "patches/next@16.4.6.patch",
},
"overrides": {
"@codemirror/state": "6.4.1",
@@ -889,29 +888,29 @@
"@napi-rs/wasm-runtime": ["@napi-rs/wasm-runtime@1.0.6", "", { "dependencies": { "@emnapi/core": "^1.5.0", "@emnapi/runtime": "^1.5.0", "@tybys/wasm-util": "^0.10.1" } }, "sha512-DXj75ewm11LIWUk198QSKUTxjyRjsBwk09MuMk5DGK+GDUtyPhhEHOGP/Xwwj3DjQXXkivoBirmOnKrLfc0+9g=="],
"@next/env": ["@next/env@16.4.6", "", {}, "sha512-SunBjJ6t+oBtecS3dTjdVuMhUogP5wD/vrGL5EVuQYM1iS4O5ilL51HkiJ3sPjMzRHiCChvr4dbaSJbR7F3lxA=="],
"@next/env": ["@next/env@16.4.5", "", {}, "sha512-JwSPwpdkZL5VMl2jA/q+JI8MZdweAL3xmw5/tb9y/TLtmS0YXZnmYDzSihwk8xTj1OL+WM53gTqaj96yuLp6Xw=="],
"@next/swc-darwin-arm64": ["@next/swc-darwin-arm64@16.4.6", "", { "os": "darwin", "cpu": "arm64" }, "sha512-kJ4VVZkley17xKaJx72QN2IBqjrj7zenCQZaUF0l9ShS/1Mt2kW4+Z5KGG6vgr3L/J562xTOizVyM1iA/u5nlg=="],
"@next/swc-darwin-arm64": ["@next/swc-darwin-arm64@16.4.5", "", { "os": "darwin", "cpu": "arm64" }, "sha512-W5TniBzWKASbNNwHZ/S/f1MxQUXU/LdjklVMWA0Ujbfl7SZnUzFls45hO0i/sF7Bx1jJBWp7WWmoQajXtX/O8A=="],
"@next/swc-darwin-x64": ["@next/swc-darwin-x64@16.4.6", "", { "os": "darwin", "cpu": "x64" }, "sha512-eIPGZIqC+8JKaeZmvnCANJQF4xWkkHqSnAAajtFxgwDMAOnCgxNvP52SeWZ41PyVwoe+Le/NQuZSaQ8j7YshSA=="],
"@next/swc-darwin-x64": ["@next/swc-darwin-x64@16.4.5", "", { "os": "darwin", "cpu": "x64" }, "sha512-uWq4qkHapBDS1oFltWS4YxTb9pLHAtLNcpbuK6SmRsebLYZqpBML/j/C0op+50JYGTpZgVL/DWNt/6oWlamrrw=="],
"@next/swc-linux-arm64-gnu": ["@next/swc-linux-arm64-gnu@16.4.6", "", { "os": "linux", "cpu": "arm64" }, "sha512-9nI6O8vvczH2D9ZNdMGqb/3NuT15T9ksOZnAmx9/gjF2cOi74+xxyf1Hk96uIMfm14c9y6wHeBq6SH8g6udvcA=="],
"@next/swc-linux-arm64-gnu": ["@next/swc-linux-arm64-gnu@16.4.5", "", { "os": "linux", "cpu": "arm64" }, "sha512-FdRcyOx5u5P51mlAqLB9EXdd8jZO5fsdHtk4BAQSkqOywCsbTNN/rmw+jO7pnmjNdAtu2AG46NzbCL2FPDrrgw=="],
"@next/swc-linux-arm64-musl": ["@next/swc-linux-arm64-musl@16.4.6", "", { "os": "linux", "cpu": "arm64" }, "sha512-7/qPhTxDp2ScluOJOhLuhWmlA17o1yZ0SIgpISBCpBxXgtnK4+KGywBpFkfLPiU1rnSM1faFGzYkH6i7JdTrqA=="],
"@next/swc-linux-arm64-musl": ["@next/swc-linux-arm64-musl@16.4.5", "", { "os": "linux", "cpu": "arm64" }, "sha512-cyvmmgtRXR8aGVT86wi9Py53IlfyCx7P2fZxuAXAoTummDnhKtUyjrMeXfapqp4qgD+gT5RqyGD/VfA16jEK7Q=="],
"@next/swc-linux-x64-gnu": ["@next/swc-linux-x64-gnu@16.4.6", "", { "os": "linux", "cpu": "x64" }, "sha512-oOpbNa8XOW1qCGd9VvzC4fbkkRxEls7/WI6tz+YHC0xRA7KpV1U7DrAnCPjzVyOk/karzWEepCOcTIDmwK8FwQ=="],
"@next/swc-linux-x64-gnu": ["@next/swc-linux-x64-gnu@16.4.5", "", { "os": "linux", "cpu": "x64" }, "sha512-hPc+Fl+N22DAvqZHq559NmamXboOX5Lu8I5Wl0DezfrhTk1Zz21NQeYoZDmNI/NjTcrdsVxM72Fzn2lI+Q+vAw=="],
"@next/swc-linux-x64-musl": ["@next/swc-linux-x64-musl@16.4.6", "", { "os": "linux", "cpu": "x64" }, "sha512-aM8CZ+OR2EwXaAYw7KGkW2GDmNDsLBIRe1MMRZCJPTdw/xhTD0IMUVO4sf3KqrzuRouGzwwLZn34+gFeVXiUQA=="],
"@next/swc-linux-x64-musl": ["@next/swc-linux-x64-musl@16.4.5", "", { "os": "linux", "cpu": "x64" }, "sha512-gbACYE8IP/jlprNTpiWXHb8N9CofOzpnEwH2SrIz1dUbiJf1FbDssDupCAPeP1Taxe8mgG20sGZamJI7KFOdZw=="],
"@next/swc-win32-arm64-msvc": ["@next/swc-win32-arm64-msvc@16.4.6", "", { "os": "win32", "cpu": "arm64" }, "sha512-U0p7/tEtqvHX2crhd841CDwKtp5CFkKXVjOhmfccR3Pvp64sj8KBwRqsvltqLOwVBbj0NtkxwXSDpEHYGbxeXQ=="],
"@next/swc-win32-arm64-msvc": ["@next/swc-win32-arm64-msvc@16.4.5", "", { "os": "win32", "cpu": "arm64" }, "sha512-+8gj++ZMo8H20Xh2ijjNqyFk5LPLw0AC4Fs4Tq/G94rjjQXhOjN4jgSWsWKPFsyY26hfIeemJIwKtMxOsp5GBA=="],
"@next/swc-win32-x64-msvc": ["@next/swc-win32-x64-msvc@16.4.6", "", { "os": "win32", "cpu": "x64" }, "sha512-3ZG15/5WIdlTQsIEvc9sPTTSL5XFrlLklIKmfF4fwsDnoAhu6NcBDXsk3mbGxNyo6aVLEGex+fHCv2MahY7a9g=="],
"@next/swc-win32-x64-msvc": ["@next/swc-win32-x64-msvc@16.4.5", "", { "os": "win32", "cpu": "x64" }, "sha512-qNZj66pYXnaWLiF9R9GUbfBdrCv7kC6TaZfIwB2KH5WPc/8pEM3nVOj8hpQhWJU8S94rlH5MPDcX15+RrgUhiw=="],
"@node-minify/core": ["@node-minify/core@10.5.0", "", { "dependencies": { "@node-minify/utils": "10.5.0" } }, "sha512-duptyliRQY+fXszqbm5CP+NauASUwkOlx/c4bwlT7tj5AQWhG9F26D3bfU/okKto9RDTGJy3cLwPvJC/j9lCkA=="],
"@node-minify/core": ["@node-minify/core@9.0.2", "", { "dependencies": { "@node-minify/utils": "9.0.1", "glob": "10.3.3", "mkdirp": "3.0.1" } }, "sha512-FNhv29Wom6wKrrFKaeAfmZqz7TX5A1E6P+bpd0VIc+DYWMLUIhAViS8riaZg3A1oD0s06s+5BG2Fg7RqMKiKHw=="],
"@node-minify/terser": ["@node-minify/terser@10.5.0", "", { "dependencies": { "@node-minify/utils": "10.5.0", "terser": "5.46.0" } }, "sha512-NvR9vYMQ/exd2lNKQ9TtoJD98gqpzQu0+bIZRqA3mci38hmacPr7H5cP4coh1vz5ZZp+WkYSrOwCm2FiC2P5MQ=="],
"@node-minify/terser": ["@node-minify/terser@9.0.1", "", { "dependencies": { "@node-minify/utils": "9.0.1", "terser": "5.36.0" } }, "sha512-WF78Ex+/xNZZDYvzwB7+sLUYQbJzyyS36ZjMhVqhORejHOsoOcTjQ9TdOgKZoY7wlfxjnLxeMKkO8R/R1KL9aQ=="],
"@node-minify/utils": ["@node-minify/utils@10.5.0", "", { "dependencies": { "fast-glob": "^3.3.3", "gzip-size": "7.0.0" } }, "sha512-3DZEYgersSgia5V/cee35sw6B7TTZlcCKyAGiuhT274/r1obMEtHt1lRmyCd52uX5QlZqDOTrD4bqHCxKzIYLg=="],
"@node-minify/utils": ["@node-minify/utils@9.0.1", "", { "dependencies": { "gzip-size": "6.0.0" } }, "sha512-aC1+mhKTP3IMa2VcuGl3ui92LO/7CPQWldNGzu3BVGKiMNJ70AKJW/R6huuYCSuQyHDGM9oFwiVClsZnFxn67g=="],
"@nodelib/fs.scandir": ["@nodelib/fs.scandir@2.1.5", "", { "dependencies": { "@nodelib/fs.stat": "2.0.5", "run-parallel": "^1.1.9" } }, "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g=="],
@@ -935,9 +934,9 @@
"@octokit/types": ["@octokit/types@14.1.0", "", { "dependencies": { "@octokit/openapi-types": "^25.1.0" } }, "sha512-1y6DgTy8Jomcpu33N+p5w58l6xyt55Ar2I91RPiIA0xCJBXyUAhXCcmZaDWSANiha7R9a6qJJ2CRomGPZ6f46g=="],
"@opennextjs/aws": ["@opennextjs/aws@4.1.16", "", { "dependencies": { "@ast-grep/napi": "^0.45.3", "@aws-sdk/client-cloudfront": "3.984.0", "@aws-sdk/client-dynamodb": "3.984.0", "@aws-sdk/client-lambda": "3.984.0", "@aws-sdk/client-s3": "3.984.0", "@aws-sdk/client-sqs": "3.984.0", "@node-minify/core": "^10.5.0", "@node-minify/terser": "^10.5.0", "@tsconfig/node18": "^18.2.7", "aws4fetch": "^1.0.20", "chalk": "^5.6.2", "cookie": "^1.1.1", "esbuild": "0.28.2", "express": "^5.2.1", "path-to-regexp": "^6.3.0", "urlpattern-polyfill": "^10.1.0", "yaml": "^2.9.1" }, "peerDependencies": { "diff": "^9.0.0", "next": ">=15.5.29 <16 || >=16.4.5" }, "optionalPeers": ["diff"], "bin": { "open-next": "dist/index.js" } }, "sha512-eHb6/LCrpr+3DmbmE+/q5GkcrbSs5IRpO/O0kcay7FK5WYDhcML0K5NhO7I9H62z42wscVIi75Hv3ASsXDeEGw=="],
"@opennextjs/aws": ["@opennextjs/aws@4.1.15", "", { "dependencies": { "@ast-grep/napi": "^0.45.3", "@aws-sdk/client-cloudfront": "3.984.0", "@aws-sdk/client-dynamodb": "3.984.0", "@aws-sdk/client-lambda": "3.984.0", "@aws-sdk/client-s3": "3.984.0", "@aws-sdk/client-sqs": "3.984.0", "@node-minify/core": "^9.0.2", "@node-minify/terser": "^9.0.1", "@tsconfig/node18": "^18.2.7", "aws4fetch": "^1.0.20", "chalk": "^5.6.2", "cookie": "^1.1.1", "esbuild": "0.28.2", "express": "^5.2.1", "path-to-regexp": "^6.3.0", "urlpattern-polyfill": "^10.1.0", "yaml": "^2.9.1" }, "peerDependencies": { "diff": "^9.0.0", "next": ">=15.5.29 <16 || >=16.4.5" }, "optionalPeers": ["diff"], "bin": { "open-next": "dist/index.js" } }, "sha512-ulv7rFEAeieD0d/zDMnw4IFPU+zY/atVUhn7ezgFqIurLB5oBYeHCUGlcGk3Pg7uMPeI03SNRjHW1+xL9EwMPw=="],
"@opennextjs/cloudflare": ["@opennextjs/cloudflare@1.20.17", "", { "dependencies": { "@ast-grep/napi": "^0.45.3", "@dotenvx/dotenvx": "^2.33.0", "@opennextjs/aws": "4.1.16", "ci-info": "^4.2.0", "cloudflare": "^4.4.1", "comment-json": "^4.5.1", "enquirer": "^2.4.1", "glob": "^13.0.6", "resolve.exports": "^2.0.4", "ts-tqdm": "^0.8.6", "yargs": "^18.2.0" }, "peerDependencies": { "next": ">=15.5.29 <16 || >=16.4.5", "rclone.js": "^0.6.6", "wrangler": "^4.148.0" }, "optionalPeers": ["rclone.js"], "bin": { "opennextjs-cloudflare": "dist/cli/index.js" } }, "sha512-jGEC9ue8LLHvOFXp62LjDUSSz7JpD+PQB+Y3r0lU78PtQ84wI16+EMF9TXEDkTek3g3fGWTLTI07pbZt8/gj8w=="],
"@opennextjs/cloudflare": ["@opennextjs/cloudflare@1.20.16", "", { "dependencies": { "@ast-grep/napi": "^0.45.3", "@dotenvx/dotenvx": "^2.33.0", "@opennextjs/aws": "4.1.15", "ci-info": "^4.2.0", "cloudflare": "^4.4.1", "comment-json": "^4.5.1", "enquirer": "^2.4.1", "glob": "^13.0.6", "ts-tqdm": "^0.8.6", "yargs": "^18.2.0" }, "peerDependencies": { "next": ">=15.5.29 <16 || >=16.4.5", "rclone.js": "^0.6.6", "wrangler": "^4.148.0" }, "optionalPeers": ["rclone.js"], "bin": { "opennextjs-cloudflare": "dist/cli/index.js" } }, "sha512-3c1Cwex5e2jXy4o4RZAusy8o9Uz4eowuPCjdVLYEZ/MnkAzN2jh9dOgsRjgu3wjigXuNe8rmPECmOTo+xKQHsw=="],
"@opentelemetry/api": ["@opentelemetry/api@1.9.0", "", {}, "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg=="],
@@ -2125,7 +2124,7 @@
"focus-trap": ["focus-trap@7.6.1", "", { "dependencies": { "tabbable": "^6.2.0" } }, "sha512-nB8y4nQl8PshahLpGKZOq1sb0xrMVFSn6at7u/qOsBZTlZRzaapISGENcB6mOkoezbClZyiMwEF/dGY8AZ00rA=="],
"foreground-child": ["foreground-child@3.3.0", "", { "dependencies": { "cross-spawn": "^7.0.0", "signal-exit": "^4.0.1" } }, "sha512-Ld2g8rrAyMYFXBhEqMz8ZAHBi4J4uS1i/CxGMDnjyFWddMXLVcDp051DZfu+t7+ab7Wv6SMqpWmyFIj5UbfFvg=="],
"foreground-child": ["foreground-child@3.3.1", "", { "dependencies": { "cross-spawn": "^7.0.6", "signal-exit": "^4.0.1" } }, "sha512-gIXjKqtFuWEgzFRJA9WCQeSJLZDjgJUOMCMzxtvFq/37KojM1BFGufqsCy0r4qSQmYLsZYMeyRqzIWOMup03sw=="],
"form-data": ["form-data@4.0.1", "", { "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", "mime-types": "^2.1.12" } }, "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw=="],
@@ -2191,7 +2190,7 @@
"graceful-fs": ["graceful-fs@4.2.11", "", {}, "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ=="],
"gzip-size": ["gzip-size@7.0.0", "", { "dependencies": { "duplexer": "^0.1.2" } }, "sha512-O1Ld7Dr+nqPnmGpdhzLmMTQ4vAsD+rHwMm1NLUmoUFFymBOMKxCCrtDxqdBRYXdeEPEi3SyoR4TizJLQrnKBNA=="],
"gzip-size": ["gzip-size@6.0.0", "", { "dependencies": { "duplexer": "^0.1.2" } }, "sha512-ax7ZYomf6jqPTQ4+XCpUGyXKHk5WweS+e05MBO4/y3WJ5RkmPXNKvX+bx1behVILVwr6JSQvZAku021CHPXG3Q=="],
"hachure-fill": ["hachure-fill@0.5.2", "", {}, "sha512-3GKBOn+m2LX9iq+JC1064cSFprJY4jL1jCXTcpnfER5HYE2l/4EfWSGzkPa/ZDBmYI0ZOEj5VHV/eKnPGkHuOg=="],
@@ -2339,7 +2338,7 @@
"isexe": ["isexe@2.0.0", "", {}, "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw=="],
"jackspeak": ["jackspeak@3.4.3", "", { "dependencies": { "@isaacs/cliui": "^8.0.2" }, "optionalDependencies": { "@pkgjs/parseargs": "^0.11.0" } }, "sha512-OGlZQpz2yfahA/Rd1Y8Cd9SIEsqvXkLVoSw/cgwhnhFMDbsQFeZYoJJ7bIZBS9BcamUW96asq/npPWugM+RQBw=="],
"jackspeak": ["jackspeak@2.3.6", "", { "dependencies": { "@isaacs/cliui": "^8.0.2" }, "optionalDependencies": { "@pkgjs/parseargs": "^0.11.0" } }, "sha512-N3yCS/NegsOBokc8GAdM8UcmfsKiSS8cipheD/nivzr700H+nsMOxJjQnvwOcRYVuFkdH0wGUvW2WbXGmrZGbQ=="],
"jiti": ["jiti@2.5.1", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-twQoecYPiVA5K/h6SxtORw/Bs3ar+mLUtoPSc7iMXzQzK8d7eJ/R09wmTwAjiamETn1cXYPGfNnu7DMoHgu12w=="],
@@ -2637,7 +2636,7 @@
"netmask": ["netmask@2.0.2", "", {}, "sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg=="],
"next": ["next@16.4.6", "", { "dependencies": { "@next/env": "16.4.6", "@swc/helpers": "0.5.23", "baseline-browser-mapping": "^2.9.19", "caniuse-lite": "^1.0.30001579", "postcss": "8.5.23", "styled-jsx": "5.1.6" }, "optionalDependencies": { "@next/swc-darwin-arm64": "16.4.6", "@next/swc-darwin-x64": "16.4.6", "@next/swc-linux-arm64-gnu": "16.4.6", "@next/swc-linux-arm64-musl": "16.4.6", "@next/swc-linux-x64-gnu": "16.4.6", "@next/swc-linux-x64-musl": "16.4.6", "@next/swc-win32-arm64-msvc": "16.4.6", "@next/swc-win32-x64-msvc": "16.4.6", "sharp": "^0.35.5" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", "@playwright/test": "^1.51.1", "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-QZJiYhaZLJMo2KpkQGQIwvmd79WVsAWeYIU554a8d7zArS3si/IRy3lBNV/rtYhE70oLwdLYhPAXLPx8HwcwtA=="],
"next": ["next@16.4.5", "", { "dependencies": { "@next/env": "16.4.5", "@swc/helpers": "0.5.23", "baseline-browser-mapping": "^2.9.19", "caniuse-lite": "^1.0.30001579", "postcss": "8.5.23", "styled-jsx": "5.1.6" }, "optionalDependencies": { "@next/swc-darwin-arm64": "16.4.5", "@next/swc-darwin-x64": "16.4.5", "@next/swc-linux-arm64-gnu": "16.4.5", "@next/swc-linux-arm64-musl": "16.4.5", "@next/swc-linux-x64-gnu": "16.4.5", "@next/swc-linux-x64-musl": "16.4.5", "@next/swc-win32-arm64-msvc": "16.4.5", "@next/swc-win32-x64-msvc": "16.4.5", "sharp": "^0.35.5" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", "@playwright/test": "^1.51.1", "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-rmdfsWosjTUO+6R0S3KokoIk6Kj0sWDRTe86tZHdqaWH/oXq/udX3/4xb+YQi7aWbyEFoglDU54kaifLCdXbBw=="],
"next-themes": ["next-themes@0.4.6", "", { "peerDependencies": { "react": "^16.8 || ^17 || ^18 || ^19 || ^19.0.0-rc", "react-dom": "^16.8 || ^17 || ^18 || ^19 || ^19.0.0-rc" } }, "sha512-pZvgD5L0IEvX5/9GWyHMf3m8BKiVQwsCMHfoFosXtXBMnaS0ZnIJ9ST4b4NqLVKDEm8QBxoNNGNaBv2JNF6XNA=="],
@@ -2903,7 +2902,7 @@
"resolve-pkg-maps": ["resolve-pkg-maps@1.0.0", "", {}, "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw=="],
"resolve.exports": ["resolve.exports@2.0.4", "", {}, "sha512-zepuGL0ulf+nt+XaFrlKeaiTJeyPFPLLY62+ncq1/VTHBdt3HsfXOH60priZ6tnXQozdGcDeIF5JVTanvIcYWQ=="],
"resolve.exports": ["resolve.exports@2.0.3", "", {}, "sha512-OcXjMsGdhL4XnbShKpAcSqPMzQoYkYyhbEaeSko47MjRP9NfEQMhZkXL1DoFlt9LWQn4YttrdnV6X2OiyzBi+A=="],
"retry": ["retry@0.13.1", "", {}, "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg=="],
@@ -3073,7 +3072,7 @@
"term-size": ["term-size@2.2.1", "", {}, "sha512-wK0Ri4fOGjv/XPy8SBHZChl8CM7uMc5VML7SqiQ0zG7+J5Vr+RMQDoHa2CNT6KHUnTGIXH34UDMkPzAUyapBZg=="],
"terser": ["terser@5.46.0", "", { "dependencies": { "@jridgewell/source-map": "^0.3.3", "acorn": "^8.15.0", "commander": "^2.20.0", "source-map-support": "~0.5.20" }, "bin": { "terser": "bin/terser" } }, "sha512-jTwoImyr/QbOWFFso3YoU3ik0jBBDJ6JTOQiy/J2YxVJdZCc+5u7skhNwiOR3FQIygFqVUPHl7qbbxtjW2K3Qg=="],
"terser": ["terser@5.36.0", "", { "dependencies": { "@jridgewell/source-map": "^0.3.3", "acorn": "^8.8.2", "commander": "^2.20.0", "source-map-support": "~0.5.20" }, "bin": { "terser": "bin/terser" } }, "sha512-IYV9eNMuFAV4THUspIRXkLakHnV6XO7FEdtKjf/mDyrnqUg9LnlOn6/RwRvM9SZjR4GUq8Nk8zj67FzVARr74w=="],
"thenify": ["thenify@3.3.1", "", { "dependencies": { "any-promise": "^1.0.0" } }, "sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw=="],
@@ -3449,6 +3448,8 @@
"@modelcontextprotocol/sdk/zod-to-json-schema": ["zod-to-json-schema@3.24.5", "", { "peerDependencies": { "zod": "^3.24.1" } }, "sha512-/AuWwMP+YqiPbsJx5D6TfgRTc4kTLjsh5SOcd4bLsfUg2RcEXrFMJl1DGgdHy2aCfsIA/cr/1JM0xcB2GZji8g=="],
"@node-minify/core/glob": ["glob@10.3.3", "", { "dependencies": { "foreground-child": "^3.1.0", "jackspeak": "^2.0.3", "minimatch": "^9.0.1", "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0", "path-scurry": "^1.10.1" }, "bin": { "glob": "dist/cjs/src/bin.js" } }, "sha512-92vPiMb/iqpmEgsOoIDvTjc50wf9CCCvMzsi6W0JLPeUKE8TWP1a73PgqSrqy7iAZxaSD1YdzU7QZR5LF51MJw=="],
"@opennextjs/aws/express": ["express@5.2.1", "", { "dependencies": { "accepts": "^2.0.0", "body-parser": "^2.2.1", "content-disposition": "^1.0.0", "content-type": "^1.0.5", "cookie": "^0.7.1", "cookie-signature": "^1.2.1", "debug": "^4.4.0", "depd": "^2.0.0", "encodeurl": "^2.0.0", "escape-html": "^1.0.3", "etag": "^1.8.1", "finalhandler": "^2.1.0", "fresh": "^2.0.0", "http-errors": "^2.0.0", "merge-descriptors": "^2.0.0", "mime-types": "^3.0.0", "on-finished": "^2.4.1", "once": "^1.4.0", "parseurl": "^1.3.3", "proxy-addr": "^2.0.7", "qs": "^6.14.0", "range-parser": "^1.2.1", "router": "^2.2.0", "send": "^1.1.0", "serve-static": "^2.2.0", "statuses": "^2.0.1", "type-is": "^2.0.1", "vary": "^1.1.2" } }, "sha512-hIS4idWWai69NezIdRt2xFVofaF4j+6INOpJlVOLDO8zXGpUVEVzIYk12UUi2JzjEzWL3IOAxcTubgz9Po0yXw=="],
"@oxc-transform/binding-wasm32-wasi/@napi-rs/wasm-runtime": ["@napi-rs/wasm-runtime@1.1.1", "", { "dependencies": { "@emnapi/core": "^1.7.1", "@emnapi/runtime": "^1.7.1", "@tybys/wasm-util": "^0.10.1" } }, "sha512-p64ah1M1ld8xjWv3qbvFwHiFVWrq1yFvV4f7w+mzaqiR4IlSgkqhcRdHwsGgomwzBH51sRY4NEowLxnaBjcW/A=="],
@@ -3615,8 +3616,6 @@
"@vercel/backends/path-to-regexp": ["path-to-regexp@8.3.0", "", {}, "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA=="],
"@vercel/backends/resolve.exports": ["resolve.exports@2.0.3", "", {}, "sha512-OcXjMsGdhL4XnbShKpAcSqPMzQoYkYyhbEaeSko47MjRP9NfEQMhZkXL1DoFlt9LWQn4YttrdnV6X2OiyzBi+A=="],
"@vercel/backends/rolldown": ["rolldown@1.0.0-rc.1", "", { "dependencies": { "@oxc-project/types": "=0.110.0", "@rolldown/pluginutils": "1.0.0-rc.1" }, "optionalDependencies": { "@rolldown/binding-android-arm64": "1.0.0-rc.1", "@rolldown/binding-darwin-arm64": "1.0.0-rc.1", "@rolldown/binding-darwin-x64": "1.0.0-rc.1", "@rolldown/binding-freebsd-x64": "1.0.0-rc.1", "@rolldown/binding-linux-arm-gnueabihf": "1.0.0-rc.1", "@rolldown/binding-linux-arm64-gnu": "1.0.0-rc.1", "@rolldown/binding-linux-arm64-musl": "1.0.0-rc.1", "@rolldown/binding-linux-x64-gnu": "1.0.0-rc.1", "@rolldown/binding-linux-x64-musl": "1.0.0-rc.1", "@rolldown/binding-openharmony-arm64": "1.0.0-rc.1", "@rolldown/binding-wasm32-wasi": "1.0.0-rc.1", "@rolldown/binding-win32-arm64-msvc": "1.0.0-rc.1", "@rolldown/binding-win32-x64-msvc": "1.0.0-rc.1" }, "bin": { "rolldown": "bin/cli.mjs" } }, "sha512-M3AeZjYE6UclblEf531Hch0WfVC/NOL43Cc+WdF3J50kk5/fvouHhDumSGTh0oRjbZ8C4faaVr5r6Nx1xMqDGg=="],
"@vercel/backends/zod": ["zod@3.22.4", "", {}, "sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg=="],
@@ -3791,6 +3790,8 @@
"finalhandler/encodeurl": ["encodeurl@1.0.2", "", {}, "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w=="],
"foreground-child/cross-spawn": ["cross-spawn@7.0.6", "", { "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" } }, "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA=="],
"global-prefix/which": ["which@1.3.1", "", { "dependencies": { "isexe": "^2.0.0" }, "bin": { "which": "./bin/which" } }, "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ=="],
"globby/fast-glob": ["fast-glob@3.3.2", "", { "dependencies": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", "glob-parent": "^5.1.2", "merge2": "^1.3.0", "micromatch": "^4.0.4" } }, "sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow=="],
@@ -4059,6 +4060,10 @@
"@jridgewell/source-map/@jridgewell/trace-mapping/@jridgewell/sourcemap-codec": ["@jridgewell/sourcemap-codec@1.5.0", "", {}, "sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ=="],
"@node-minify/core/glob/minimatch": ["minimatch@9.0.5", "", { "dependencies": { "brace-expansion": "^2.0.1" } }, "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow=="],
"@node-minify/core/glob/path-scurry": ["path-scurry@1.11.1", "", { "dependencies": { "lru-cache": "^10.2.0", "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0" } }, "sha512-Xa4Nw17FS9ApQFJ9umLiJS4orGjm7ZzwUrwamcGQuHSzDyth9boKDaycYdDcZDuqYATXw4HFXgaqWTctW/v1HA=="],
"@opennextjs/aws/express/body-parser": ["body-parser@2.2.1", "", { "dependencies": { "bytes": "^3.1.2", "content-type": "^1.0.5", "debug": "^4.4.3", "http-errors": "^2.0.0", "iconv-lite": "^0.7.0", "on-finished": "^2.4.1", "qs": "^6.14.0", "raw-body": "^3.0.1", "type-is": "^2.0.1" } }, "sha512-nfDwkulwiZYQIGwxdy0RUmowMhKcFVcYXUU7m4QlKYim1rUtg83xm2yjZ40QjDuc291AJjjeSc9b++AWHSgSHw=="],
"@opennextjs/aws/express/cookie": ["cookie@0.7.1", "", {}, "sha512-6DnInpx7SJ2AK3+CTUE/ZM0vWTUboZCegxhC2xiIydHR9jNuTAASBrfEpHhiGOZw/nX51bHt6YQl8jsGo4y/0w=="],
@@ -4421,6 +4426,10 @@
"remark-gfm/mdast-util-gfm/mdast-util-to-markdown": ["mdast-util-to-markdown@2.1.0", "", { "dependencies": { "@types/mdast": "^4.0.0", "@types/unist": "^3.0.0", "longest-streak": "^3.0.0", "mdast-util-phrasing": "^4.0.0", "mdast-util-to-string": "^4.0.0", "micromark-util-decode-string": "^2.0.0", "unist-util-visit": "^5.0.0", "zwitch": "^2.0.0" } }, "sha512-SR2VnIEdVNCJbP6y7kVTJgPLifdr8WEU440fQec7qHoHOUz/oJ2jmNRqdDQ3rbiStOXb2mCDGTuwsK5OPUgYlQ=="],
"rimraf/glob/foreground-child": ["foreground-child@3.3.0", "", { "dependencies": { "cross-spawn": "^7.0.0", "signal-exit": "^4.0.1" } }, "sha512-Ld2g8rrAyMYFXBhEqMz8ZAHBi4J4uS1i/CxGMDnjyFWddMXLVcDp051DZfu+t7+ab7Wv6SMqpWmyFIj5UbfFvg=="],
"rimraf/glob/jackspeak": ["jackspeak@3.4.3", "", { "dependencies": { "@isaacs/cliui": "^8.0.2" }, "optionalDependencies": { "@pkgjs/parseargs": "^0.11.0" } }, "sha512-OGlZQpz2yfahA/Rd1Y8Cd9SIEsqvXkLVoSw/cgwhnhFMDbsQFeZYoJJ7bIZBS9BcamUW96asq/npPWugM+RQBw=="],
"rimraf/glob/minimatch": ["minimatch@9.0.5", "", { "dependencies": { "brace-expansion": "^2.0.1" } }, "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow=="],
"rimraf/glob/minipass": ["minipass@7.1.2", "", {}, "sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw=="],
@@ -4473,6 +4482,12 @@
"@headlessui/react/@floating-ui/react/@floating-ui/react-dom/@floating-ui/dom": ["@floating-ui/dom@1.7.5", "", { "dependencies": { "@floating-ui/core": "^1.7.4", "@floating-ui/utils": "^0.2.10" } }, "sha512-N0bD2kIPInNHUHehXhMke1rBGs1dwqvC9O9KYMyyjK7iXt7GAhnro7UlcuYcGdS/yYOlq0MAVgrow8IbWJwyqg=="],
"@node-minify/core/glob/minimatch/brace-expansion": ["brace-expansion@2.0.1", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA=="],
"@node-minify/core/glob/path-scurry/lru-cache": ["lru-cache@10.4.3", "", {}, "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="],
"@node-minify/core/glob/path-scurry/minipass": ["minipass@7.1.2", "", {}, "sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw=="],
"@opennextjs/aws/express/body-parser/raw-body": ["raw-body@3.0.2", "", { "dependencies": { "bytes": "~3.1.2", "http-errors": "~2.0.1", "iconv-lite": "~0.7.0", "unpipe": "~1.0.0" } }, "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA=="],
"@opennextjs/aws/express/mime-types/mime-db": ["mime-db@1.53.0", "", {}, "sha512-oHlN/w+3MQ3rba9rqFr6V/ypF10LSkdwUysQL7GkXoTgIWeV+tcXGA852TBxH+gsh8UWoyhR1hKcoMJTuWflpg=="],
@@ -4555,6 +4570,8 @@
"@headlessui/react/@floating-ui/react/@floating-ui/react-dom/@floating-ui/dom/@floating-ui/core": ["@floating-ui/core@1.7.4", "", { "dependencies": { "@floating-ui/utils": "^0.2.10" } }, "sha512-C3HlIdsBxszvm5McXlB8PeOEWfBhcGBTZGkGlWc2U0KFY5IwG5OQEuQ8rq52DZmcHDlPLd+YFBK+cZcytwIFWg=="],
"@node-minify/core/glob/minimatch/brace-expansion/balanced-match": ["balanced-match@1.0.2", "", {}, "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="],
"@opennextjs/aws/express/body-parser/raw-body/http-errors": ["http-errors@2.0.1", "", { "dependencies": { "depd": "~2.0.0", "inherits": "~2.0.4", "setprototypeof": "~1.2.0", "statuses": "~2.0.2", "toidentifier": "~1.0.1" } }, "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ=="],
"@opennextjs/aws/express/qs/side-channel/object-inspect": ["object-inspect@1.13.4", "", {}, "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew=="],
+1 -1
View File
@@ -68,7 +68,7 @@
"decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch",
"@vercel/next@4.4.2": "patches/@vercel%2Fnext@4.4.2.patch",
"mermaid@11.16.1": "patches/mermaid@11.16.1.patch",
"next@16.4.6": "patches/next@16.4.6.patch",
"next@16.4.5": "patches/next@16.4.5.patch",
"@shikijs/langs@3.21.0": "patches/@shikijs%2Flangs@3.21.0.patch"
}
}
+2 -554
View File
@@ -1,4 +1,4 @@
import { type Page, type Request, type Route, expect } from '@playwright/test';
import { type Page, type Route, expect } from '@playwright/test';
import jwt from 'jsonwebtoken';
import {
@@ -16,7 +16,7 @@ import {
} from '@gitbook/api';
import type { GitBookStandalone } from '@gitbook/embed';
import { getContentTestURL, getGitBookPreviewURL, getSiteAPIToken } from '../tests/utils';
import { getGitBookPreviewURL, getSiteAPIToken } from '../tests/utils';
import {
type Test,
type TestsCase,
@@ -299,8 +299,6 @@ const searchTestCases: Test[] = [
screenshot: false,
run: async (page) => {
await waitForCookiesDialog(page);
// Keep the search on the failing route whether or not the site gives search passes.
await page.route('**/~gitbook/search-pass', (route) => route.fulfill({ status: 204 }));
let failing = true;
await page.route('**/~gitbook/search', (route) =>
failing ? route.fulfill({ status: 500 }) : route.continue()
@@ -415,8 +413,6 @@ const searchTestCases: Test[] = [
screenshot: false,
run: async (page) => {
await waitForCookiesDialog(page);
// Keep both searches on the held route whether or not the site gives search passes.
await page.route('**/~gitbook/search-pass', (route) => route.fulfill({ status: 204 }));
const heldSearch = await holdSearchRequests(page);
const startPath = new URL(page.url()).pathname;
const searchInput = page.getByTestId('search-input');
@@ -543,544 +539,6 @@ const searchTestCases: Test[] = [
},
];
const SEARCH_API_URL = /\/v1\/orgs\/[^/]+\/sites\/[^/]+\/search$/;
const SEARCH_PASS_RECORD = {
type: 'record',
id: 'search-pass-record',
title: 'Answered by the search API',
description: 'A search made with a search pass',
url: 'https://example.com/search-pass',
score: 1,
};
/**
* Answer the page's search pass requests as the API does when passes are switched on.
*/
async function giveSearchPasses(page: Page, lifetimesMs: number[] = []) {
let passes = 0;
await page.route('**/~gitbook/search-pass', async (route) => {
passes += 1;
await route.fulfill({
json: {
token: `test-search-pass-${passes}`,
expiresIn: lifetimesMs.shift() ?? 300_000,
},
});
});
}
/**
* Answer each search API call with the next status, and with a record when it succeeds.
*/
async function answerSearchAPI(page: Page, statuses: number[] = []) {
const calls: Request[] = [];
await page.route(SEARCH_API_URL, async (route) => {
calls.push(route.request());
const status = statuses.shift() ?? 200;
await route.fulfill({
status,
headers: { 'access-control-allow-origin': '*' },
json: status === 200 ? { items: [SEARCH_PASS_RECORD] } : { error: { code: status } },
});
});
return calls;
}
/**
* Collect the searches the page sends to the site's search route.
*/
function trackRouteSearches(page: Page) {
const searches: Request[] = [];
page.on('request', (request) => {
if (request.method() === 'POST' && request.url().endsWith('/~gitbook/search')) {
searches.push(request);
}
});
return searches;
}
/**
* Open search and wait for the page to receive its search pass.
*/
async function openSearchWithPass(page: Page) {
const passReply = page.waitForResponse('**/~gitbook/search-pass');
await page.getByTestId('search-input').focus();
await passReply;
}
async function typeSearch(page: Page, text: string) {
const searchInput = page.getByTestId('search-input');
await searchInput.focus();
await searchInput.pressSequentially(text);
}
const searchPassTestCases: Test[] = [
{
name: 'No pass: searches go through the site route',
url: '',
screenshot: false,
run: async (page) => {
await page.route('**/~gitbook/search-pass', (route) => route.fulfill({ status: 204 }));
const passReply = page.waitForResponse('**/~gitbook/search-pass');
const apiCalls: Request[] = [];
page.on('request', (request) => {
if (SEARCH_API_URL.test(request.url())) {
apiCalls.push(request);
}
});
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await typeSearch(page, 'gitbook');
expect((await passReply).status()).toBe(204);
await expect
.poll(() => routeSearches.length, { timeout: 30_000 })
.toBeGreaterThanOrEqual(1);
expect((await routeSearches[0]?.response())?.status()).toBe(200);
expect(apiCalls).toHaveLength(0);
},
},
{
name: 'With a pass: searches go straight to the API',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const apiCalls = await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
expect((await apiCalls[0]?.allHeaders())?.authorization).toBe(
'Bearer test-search-pass-1'
);
expect(routeSearches).toHaveLength(0);
},
},
{
name: 'Expired pass: the search uses the route, then the next pass searches the API',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page, [1_000]);
const apiCalls = await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
expect(routeSearches[0]?.headers()['x-gitbook-search-fallback']).toBe('expired');
expect(apiCalls).toHaveLength(0);
await typeSearch(page, ' editor');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
expect((await apiCalls[0]?.allHeaders())?.authorization).toBe(
'Bearer test-search-pass-2'
);
},
},
{
name: 'Refused pass: the page stays on the route',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const apiCalls = await answerSearchAPI(page, [403]);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
await typeSearch(page, ' editor');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(2);
expect(
routeSearches.map((request) => request.headers()['x-gitbook-search-fallback'])
).toEqual(['403', '403']);
expect(apiCalls).toHaveLength(1);
expect((await routeSearches.at(-1)?.response())?.status()).toBe(200);
},
},
{
name: 'Dropped API request: the search falls back to the route after its time limit',
url: '',
screenshot: false,
run: async (page) => {
await page.clock.install();
await giveSearchPasses(page);
const apiCalls: Request[] = [];
await page.route(SEARCH_API_URL, (route) => {
apiCalls.push(route.request());
});
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect.poll(() => apiCalls.length, { timeout: 30_000 }).toBe(1);
await page.clock.fastForward(5_000);
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
expect(routeSearches[0]?.headers()['x-gitbook-search-fallback']).toBe('timeout');
expect((await routeSearches[0]?.response())?.status()).toBe(200);
await typeSearch(page, ' editor');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(2);
expect(apiCalls).toHaveLength(1);
},
},
{
name: 'Withdrawn pass: a no-pass reply stops the page using the earlier pass',
url: '',
screenshot: false,
run: async (page) => {
let passReplies = 0;
await page.route('**/~gitbook/search-pass', async (route) => {
passReplies += 1;
await (passReplies === 1
? route.fulfill({ json: { token: 'test-search-pass-1', expiresIn: 300_000 } })
: route.fulfill({ status: 204 }));
});
const apiCalls = await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
await page.keyboard.press('Escape');
await openSearchWithPass(page);
await typeSearch(page, ' editor');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
expect(apiCalls).toHaveLength(1);
},
},
{
name: 'Rejected replacement pass: the page stays on the route after one refresh',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const passRequests: Request[] = [];
page.on('request', (request) => {
if (request.url().endsWith('/~gitbook/search-pass')) {
passRequests.push(request);
}
});
const apiCalls = await answerSearchAPI(page, [401, 401, 401, 401]);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
for (const [index, text] of ['gitbook', ' editor', ' blocks', ' lists'].entries()) {
await typeSearch(page, text);
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(index + 1);
await page.waitForTimeout(500);
}
expect(apiCalls).toHaveLength(2);
expect(passRequests).toHaveLength(2);
},
},
{
name: 'Rate limited: the next query waits for the cooldown before using the route',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const apiCalls = await answerSearchAPI(page, [429]);
const routeSearchTimes: number[] = [];
page.on('request', (request) => {
if (request.method() === 'POST' && request.url().endsWith('/~gitbook/search')) {
routeSearchTimes.push(Date.now());
}
});
await waitForCookiesDialog(page);
await openSearchWithPass(page);
const rateLimited = page.waitForResponse(SEARCH_API_URL);
await typeSearch(page, 'gitbook');
await rateLimited;
const rateLimitedAt = Date.now();
await typeSearch(page, ' editor');
await expect.poll(() => routeSearchTimes.length, { timeout: 30_000 }).toBe(1);
expect(routeSearchTimes[0]! - rateLimitedAt).toBeGreaterThanOrEqual(1_500);
expect(apiCalls).toHaveLength(1);
},
},
{
name: 'Renewing pass: a search typed before the reply goes to the route',
url: '',
screenshot: false,
run: async (page) => {
let releaseRenewal = () => {};
const renewalHeld = new Promise<void>((resolve) => {
releaseRenewal = resolve;
});
let passReplies = 0;
await page.route('**/~gitbook/search-pass', async (route) => {
passReplies += 1;
if (passReplies === 1) {
await route.fulfill({
json: { token: 'test-search-pass-1', expiresIn: 300_000 },
});
return;
}
await renewalHeld;
await route.fulfill({ status: 204 }).catch(() => undefined);
});
const apiCalls = await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect.poll(() => apiCalls.length, { timeout: 30_000 }).toBe(1);
await page.keyboard.press('Escape');
const renewal = page.waitForRequest('**/~gitbook/search-pass');
await typeSearch(page, ' editor');
await renewal;
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
expect(apiCalls).toHaveLength(1);
releaseRenewal();
},
},
{
name: 'Rate limited by a shared limit: searches fail at once until its reset',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const apiCalls: Request[] = [];
await page.route(SEARCH_API_URL, async (route) => {
apiCalls.push(route.request());
await route.fulfill({
status: 429,
headers: {
'access-control-allow-origin': '*',
'access-control-expose-headers': 'x-ratelimit-reset, x-ratelimit-scope',
'x-ratelimit-reset': String(Math.ceil(Date.now() / 1000) + 60),
'x-ratelimit-scope': 'tenant-operation',
},
json: { error: { code: 429 } },
});
});
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await expect.poll(() => apiCalls.length, { timeout: 30_000 }).toBe(1);
await typeSearch(page, ' editor');
await expect(page.getByRole('button', { name: 'Try again' })).toBeVisible({
timeout: 30_000,
});
await page.waitForTimeout(3_000);
expect(routeSearches).toHaveLength(0);
expect(apiCalls).toHaveLength(1);
},
},
{
name: 'Cancelled search: a late API answer never replaces the newer query',
url: '',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
const staleTitle = 'Late answer for an older query';
await page.route(SEARCH_API_URL, async (route) => {
const { query } = route.request().postDataJSON() as { query: string };
const stale = query === 'gitbook';
if (stale) {
await page.waitForTimeout(3_000);
}
await route
.fulfill({
headers: { 'access-control-allow-origin': '*' },
json: {
items: [
stale
? { ...SEARCH_PASS_RECORD, title: staleTitle }
: SEARCH_PASS_RECORD,
],
},
})
.catch(() => undefined);
});
const routeSearches = trackRouteSearches(page);
await waitForCookiesDialog(page);
const firstSearch = page.waitForRequest(
(request) =>
SEARCH_API_URL.test(request.url()) &&
(request.postDataJSON() as { query: string }).query === 'gitbook'
);
await openSearchWithPass(page);
await typeSearch(page, 'gitbook');
await firstSearch;
await typeSearch(page, ' editor');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
await page.waitForTimeout(4_000);
await expect(page.getByText(staleTitle)).toHaveCount(0);
expect(routeSearches).toHaveLength(0);
},
},
{
name: 'Embedded search goes straight to the API',
url: '~gitbook/embed/search',
screenshot: false,
run: async (page) => {
await giveSearchPasses(page);
await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
// The embedded page keeps <html> hidden, so wait for hydration without visibility.
await page.locator('html.hydrated').waitFor({ state: 'attached' });
// The embedded search opens on the first key; a one-letter query is never sent.
const passReply = page.waitForResponse('**/~gitbook/search-pass');
await typeSearch(page, 'g');
await passReply;
await typeSearch(page, 'itbook');
await expect(
page
.getByTestId('search-record-result')
.filter({ hasText: SEARCH_PASS_RECORD.title })
).toBeVisible({ timeout: 30_000 });
// The one-letter query can reach the route when it is sent before the pass arrives.
const routeQueries = routeSearches.map((request) => request.postDataJSON().query);
expect(routeQueries).not.toContain('gitbook');
},
},
];
const VISITOR_AUTH_SITE = 'https://gitbook.gitbook.io/gbo-va-space/';
function signVisitorToken(name: string) {
return jwt.sign({ name }, '70b844d0-c519-4532-8586-5970ce48c537', { expiresIn: '24h' });
}
/**
* Give the page one search pass, then answer later pass requests with `later` or the site's route.
*/
async function giveOneSearchPass(page: Page, later?: { token: string }) {
let passes = 0;
await page.route('**/~gitbook/search-pass', async (route) => {
passes += 1;
if (passes > 1 && !later) {
await route.continue();
return;
}
await route.fulfill({
json: {
token: passes === 1 ? 'test-search-pass-1' : later?.token,
expiresIn: 300_000,
},
});
});
}
/**
* Open a second tab of the site at `path`, keeping it from leaving for an upstream sign-in page.
*/
async function visitInOtherTab(page: Page, path: string) {
const other = await page.context().newPage();
await other.route(
(url) => url.hostname !== new URL(page.url()).hostname,
(route) => route.abort()
);
await other.goto(getContentTestURL(new URL(path, VISITOR_AUTH_SITE).href), {
waitUntil: 'commit',
});
await other.close();
}
/**
* Close search and open it again, waiting for the pass request that opening sends.
*/
async function reopenSearch(page: Page) {
await page.keyboard.press('Escape');
const passReply = page.waitForResponse('**/~gitbook/search-pass');
await page.getByTestId('search-input').focus();
return passReply;
}
const searchPassSessionTestCases: Test[] = [
{
name: 'Signed out in another tab: reopening search drops the pass',
url: () => `first?jwt_token=${signVisitorToken('gitbook-open-tests')}`,
screenshot: false,
run: async (page) => {
await giveOneSearchPass(page);
const apiCalls = await answerSearchAPI(page);
const routeSearches = trackRouteSearches(page);
await waitForHydration(page);
await openSearchWithPass(page);
await typeSearch(page, 'first');
await expect.poll(() => apiCalls.length, { timeout: 30_000 }).toBe(1);
await visitInOtherTab(page, '~gitbook/auth/logout');
await expect
.poll(async () =>
(await page.context().cookies()).some((cookie) =>
cookie.name.startsWith(VISITOR_TOKEN_COOKIE)
)
)
.toBe(false);
expect((await reopenSearch(page)).status()).toBe(401);
await typeSearch(page, ' page');
await expect.poll(() => routeSearches.length, { timeout: 30_000 }).toBe(1);
expect(apiCalls).toHaveLength(1);
},
},
{
name: 'Signed in as another visitor in another tab: reopening search uses the new pass',
url: () => `first?jwt_token=${signVisitorToken('gitbook-open-tests')}`,
screenshot: false,
run: async (page) => {
await giveOneSearchPass(page, { token: 'test-search-pass-2' });
const apiCalls = await answerSearchAPI(page);
await waitForHydration(page);
await openSearchWithPass(page);
await typeSearch(page, 'first');
await expect.poll(() => apiCalls.length, { timeout: 30_000 }).toBe(1);
const otherVisitor = signVisitorToken('another-visitor');
await visitInOtherTab(page, `first?jwt_token=${otherVisitor}`);
const passReply = await reopenSearch(page);
expect((await passReply.request().allHeaders()).cookie).toContain(otherVisitor);
await typeSearch(page, ' page');
await expect.poll(() => apiCalls.length, { timeout: 30_000 }).toBe(2);
expect((await apiCalls[1]?.allHeaders())?.authorization).toBe(
'Bearer test-search-pass-2'
);
},
},
];
const testCases: TestsCase[] = [
{
name: 'GitBook Site (Single Variant)',
@@ -1924,16 +1382,6 @@ const testCases: TestsCase[] = [
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
tests: searchTestCases,
},
{
name: 'Search pass',
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
tests: searchPassTestCases,
},
{
name: 'Search pass with visitor authentication',
contentBaseURL: VISITOR_AUTH_SITE,
tests: searchPassSessionTestCases,
},
{
name: 'Content tests',
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
-12
View File
@@ -1,7 +1,6 @@
// @ts-check
import { networkInterfaces } from 'node:os';
import { fileURLToPath } from 'node:url';
// Next blocks its dev client and HMR when a physical device opens the server over a LAN address.
// Needed to hydrate the dev client on physical phones/tablets over the internal network
@@ -120,17 +119,6 @@ const nextConfig = {
],
},
webpack(config, { isServer }) {
if (isServer) {
// mcp-handler only needs redis for SSE, which we disable: keep the client out of the server bundle.
config.resolve.alias = {
...config.resolve.alias,
redis$: fileURLToPath(new URL('./src/lib/mcp/redis-stub.ts', import.meta.url)),
};
}
return config;
},
async headers() {
return [
{
@@ -32,7 +32,7 @@ export default {
await import('../../.open-next/server-functions/default/handler.mjs');
// - `Request`s are handled by the Next server
return handler(request, env, ctx);
return handler(request, env, ctx, request.signal);
});
},
};
@@ -8,6 +8,7 @@
"nodejs_compat",
"allow_importable_env",
"global_fetch_strictly_public",
"enable_request_signal",
],
"observability": {
"enabled": false,
@@ -51,6 +51,7 @@ export default class extends WorkerEntrypoint {
);
const response = await this.env.DEFAULT_WORKER?.fetch(reqOrResp, {
redirect: 'manual',
signal: request.signal,
cf: {
cacheEverything: false,
},
@@ -3,11 +3,11 @@
"name": "gitbook-open-v2",
"compatibility_date": "2026-04-02",
"keep_names": false,
"minify": true,
"compatibility_flags": [
"nodejs_compat",
"allow_importable_env",
"global_fetch_strictly_public",
"enable_request_signal",
],
"observability": {
"enabled": false,
@@ -1,23 +0,0 @@
import { describe, expect, it } from 'bun:test';
import { createHash } from 'node:crypto';
import { GitbookIncrementalCache } from './incrementalCache';
describe('GitbookIncrementalCache.getR2Key', () => {
const cache = new GitbookIncrementalCache();
it('accepts the hashed composable key passed by OpenNext', () => {
const storageKey = createHash('sha256').update('["build-id","action",[]]').digest('hex');
const key = cache.getR2Key(storageKey, 'composable');
const hash = createHash('sha256').update(storageKey).digest('hex');
expect(key).toBe(`incremental-cache/dataCache/${hash}.composable`);
});
it('maps distinct composable keys to distinct R2 keys', () => {
expect(cache.getR2Key('a'.repeat(64), 'composable')).not.toBe(
cache.getR2Key('b'.repeat(64), 'composable')
);
});
});
@@ -118,8 +118,15 @@ export class GitbookIncrementalCache implements IncrementalCache {
}
}
// OpenNext pre-hashes composable keys with the build ID in them: no reuse across deploys.
getR2Key(key: string, cacheType: CacheEntryType = 'cache'): string {
// Utility function to generate keys for R2/Cache API
getR2Key(initialKey: string, cacheType: CacheEntryType = 'cache'): string {
let key = initialKey;
if (cacheType === 'composable') {
// For composable cache, we need to discard the build ID from the key
const [_buildId, ...restOfTheKey] = JSON.parse(initialKey);
key = JSON.stringify([...restOfTheKey]);
}
const hash = createHash('sha256').update(key).digest('hex');
return `${DEFAULT_PREFIX}/${cacheType === 'cache' ? process.env?.NEXT_BUILD_ID : 'dataCache'}/${hash}.${cacheType}`.replace(
/\/+/g,
+4 -5
View File
@@ -20,8 +20,8 @@
"@gitbook/react-openapi": "workspace:*",
"@mermaid-js/mermaid-zenuml": "^0.2.2",
"@modelcontextprotocol/sdk": "1.17.5",
"@opennextjs/aws": "4.1.16",
"@opennextjs/cloudflare": "1.20.17",
"@opennextjs/aws": "4.1.15",
"@opennextjs/cloudflare": "1.20.16",
"@panzoom/panzoom": "^4.6.1",
"@sindresorhus/fnv1a": "^3.1.0",
"@tailwindcss/container-queries": "^0.1.1",
@@ -51,7 +51,7 @@
"micromark-extension-gfm": "^3.0.0",
"motion": "^12.23.24",
"negotiator": "^1.0.0",
"next": "^16.4.6",
"next": "^16.4.5",
"next-themes": "^0.4.6",
"nuqs": "^2.2.3",
"object-hash": "^3.0.0",
@@ -59,7 +59,6 @@
"openapi-types": "^12.1.3",
"p-map": "^7.0.3",
"p-retry": "^8.0.0",
"parse5": "7.2.0",
"quick-lru": "^7.0.1",
"react": "19.2.4",
"react-dom": "19.2.4",
@@ -136,7 +135,7 @@
"e2e": "playwright test e2e/internal.spec.ts e2e/cookie-banner.spec.ts e2e/pdf.spec.ts e2e/select.spec.ts e2e/tabs-overflow.spec.ts --project=chromium",
"e2e-customers": "playwright test e2e/customers.spec.ts --project=chromium",
"e2e-style-perf": "playwright test e2e/style-perf.spec.ts --project=chromium --reporter=list",
"unit": "bun run generate:assets && bun test {src,packages,openNext} --preload ./tests/preload-bun.ts",
"unit": "bun run generate:assets && bun test {src,packages} --preload ./tests/preload-bun.ts",
"e2e-browserless": "bun test ./tests/",
"typecheck": "tsc --noEmit"
},
@@ -50,7 +50,6 @@ export async function handleMcpRequest(
streamableHttpEndpoint: context.linker.toPathInSite(endpoint),
maxDuration: 60,
verboseLogs: true,
// SSE needs redis, which next.config.mjs stubs out of the server bundle.
disableSse: true,
}
);
@@ -1,18 +1,17 @@
import { type NextRequest, NextResponse } from 'next/server';
import { SEARCH_FALLBACK_HEADER } from '@/components/Search/search-pass-store';
import type { SearchSiteContentRequest } from '@/components/Search/search-types';
import { orderSearchResultGroups } from './orderSearchResults';
import type {
OrderedComputedResult,
SearchSiteContentRequest,
} from '@/components/Search/search-types';
import { throwIfDataError } from '@/lib/data';
import { getLogger } from '@/lib/logger';
import { getSiteURLDataFromMiddleware } from '@/lib/middleware';
import { transformSitePageResult } from '@/lib/search';
import { computeSearchResults } from '@/lib/search-results';
import { getServerActionBaseContext } from '@/lib/server-actions';
import { findSiteSpaceBy, getLinkerForSiteSpace } from '@/lib/sites';
const SEARCH_FALLBACK_REASON =
/^(\d{3}|expired|pass_request|network|timeout|invalid|unknown_space)$/;
export async function POST(request: NextRequest) {
const { asEmbeddable, query, scope } = (await request.json()) as SearchSiteContentRequest;
const [context, siteURLData] = await Promise.all([
@@ -20,28 +19,18 @@ export async function POST(request: NextRequest) {
getSiteURLDataFromMiddleware(),
]);
const fallback = request.headers.get(SEARCH_FALLBACK_HEADER);
if (fallback && SEARCH_FALLBACK_REASON.test(fallback)) {
getLogger()
.subLogger('search')
.info(
`Search pass fallback to the route (reason=${fallback}, organization=${siteURLData.organization}, site=${siteURLData.site})`
);
}
if (query.length <= 1) {
return NextResponse.json([]);
}
const [searchResults, { structure }, revision] = await Promise.all([
throwIfDataError(
context.dataFetcher.searchSiteContent({
organizationId: siteURLData.organization,
siteId: siteURLData.site,
query,
scope,
})
),
const [searchResponse, { structure }, revision] = await Promise.all([
context.dataFetcher.searchSiteContent({
organizationId: siteURLData.organization,
siteId: siteURLData.site,
query,
scope,
signal: request.signal,
}),
throwIfDataError(
context.dataFetcher.getPublishedContentSite({
organizationId: siteURLData.organization,
@@ -59,6 +48,16 @@ export async function POST(request: NextRequest) {
: Promise.resolve(undefined),
]);
if (request.signal.aborted) {
getLogger()
.subLogger('search', {
labels: { organizationId: siteURLData.organization, siteId: siteURLData.site },
})
.info('search cancelled');
return new Response(null, { status: 499 });
}
const searchResults = throwIfDataError(searchResponse);
const currentSiteSpace = revision
? findSiteSpaceBy(structure, (siteSpace) => siteSpace.id === siteURLData.siteSpace)
: null;
@@ -67,24 +66,49 @@ export async function POST(request: NextRequest) {
? getLinkerForSiteSpace(context.linker, currentSiteSpace.siteSpace, revision.pages)
: context.linker;
const results = computeSearchResults(searchResults, (spaceItem, pageItem) => {
const isCurrentRevisionSpace = Boolean(revision && spaceItem.id === siteURLData.space);
const found =
isCurrentRevisionSpace && currentSiteSpace
? currentSiteSpace
: findSiteSpaceBy(structure, (siteSpace) => siteSpace.space.id === spaceItem.id);
const results = orderSearchResultGroups<OrderedComputedResult>(
searchResults.map((resultItem) => {
if (resultItem.type === 'record') {
const result: OrderedComputedResult = {
type: 'record',
id: resultItem.id,
title: resultItem.title,
description: resultItem.description,
href: resultItem.url,
score: resultItem.score,
};
return transformSitePageResult({
asEmbeddable: Boolean(asEmbeddable),
linker: isCurrentRevisionSpace ? revisionLinker : context.linker,
pageItem,
spaceItem,
siteSpace: found?.siteSpace,
siteSection: found?.siteSection ?? undefined,
siteSectionGroup: found?.siteSectionGroup ?? undefined,
revisionPages: isCurrentRevisionSpace ? revision?.pages : undefined,
});
});
return { type: 'context' as const, results: [result] };
}
const isCurrentRevisionSpace = Boolean(revision && resultItem.id === siteURLData.space);
const found =
isCurrentRevisionSpace && currentSiteSpace
? currentSiteSpace
: findSiteSpaceBy(
structure,
(siteSpace) => siteSpace.space.id === resultItem.id
);
return {
type: 'pages' as const,
results: resultItem.pages.flatMap((pageItem) => {
const result = transformSitePageResult({
asEmbeddable: Boolean(asEmbeddable),
linker: isCurrentRevisionSpace ? revisionLinker : context.linker,
pageItem,
spaceItem: resultItem,
siteSpace: found?.siteSpace,
siteSection: found?.siteSection ?? undefined,
siteSectionGroup: found?.siteSectionGroup ?? undefined,
revisionPages: isCurrentRevisionSpace ? revision?.pages : undefined,
});
return result ? [{ rank: pageItem.rank, result }] : [];
}),
};
})
);
return NextResponse.json(results);
}
+2 -21
View File
@@ -1,6 +1,5 @@
import { jwtDecode } from 'jwt-decode';
import { forbidden, notFound } from 'next/navigation';
import { cache } from 'react';
import rison from 'rison';
import type { SiteAPIToken } from '@gitbook/api';
@@ -28,17 +27,7 @@ export type RouteParams = RouteLayoutParams & {
/**
* Get the static context when rendering statically a site.
*/
export function getStaticSiteContext(params: RouteLayoutParams) {
return getStaticSiteContextCached(params.mode, params.siteURL, params.siteData);
}
// Layouts, pages and their metadata functions each resolve the context: share it per request.
// Keyed on the raw strings as `params` objects aren't guaranteed to be the same reference.
const getStaticSiteContextCached = cache((mode: string, siteURL: string, siteData: string) =>
resolveStaticSiteContext({ mode, siteURL, siteData })
);
async function resolveStaticSiteContext(params: RouteLayoutParams) {
export async function getStaticSiteContext(params: RouteLayoutParams) {
const siteURL = getSiteURLFromParams(params);
const siteURLData = getSiteURLDataFromParams(params);
@@ -68,15 +57,7 @@ async function resolveStaticSiteContext(params: RouteLayoutParams) {
* Get the site context when rendering dynamically.
* The context will depend on the request.
*/
export function getDynamicSiteContext(params: RouteLayoutParams) {
return getDynamicSiteContextCached(params.mode, params.siteURL, params.siteData);
}
const getDynamicSiteContextCached = cache((mode: string, siteURL: string, siteData: string) =>
resolveDynamicSiteContext({ mode, siteURL, siteData })
);
async function resolveDynamicSiteContext(params: RouteLayoutParams) {
export async function getDynamicSiteContext(params: RouteLayoutParams) {
const siteURL = getSiteURLFromParams(params);
const siteURLData = getSiteURLDataFromParams(params);
@@ -180,11 +180,7 @@ function CodeBlockExpandable(props: {
<div
className={tcls(
!isExpanded
? [
'[&_pre]:h-[calc(2rem+var(--line-count)*var(--line-height))] [&_pre]:overflow-y-hidden',
// Mask the code rather than the pre to keep its border, with stops on the visible lines.
'[&_code]:mask-b-from-[calc((var(--line-count)+1)*var(--line-height)*0.55)] [&_code]:mask-b-to-[calc((var(--line-count)+1)*var(--line-height))]',
]
? '[&_pre]:h-[calc(2rem+var(--line-count)*var(--line-height))] [&_pre]:overflow-y-hidden'
: ''
)}
style={
@@ -127,7 +127,8 @@ export const CodeBlockRenderer = forwardRef(function CodeBlockRenderer(
<code
className={tcls(
'table max-h-full w-fit min-w-full [counter-reset:line] print:max-h-none print:whitespace-pre-wrap',
withWrap && 'whitespace-pre-wrap'
withWrap && 'whitespace-pre-wrap',
'[[aria-expanded=false]_&]:mask-b-from-50%'
)}
>
{theme.lines.map((line, index) => (
@@ -1,6 +1,6 @@
import type { DocumentBlockHint } from '@gitbook/api';
import { Icon, type IconName } from '@gitbook/icons';
import { validateIconName } from '@gitbook/icons/validateIconName';
import { validateIconName } from '@gitbook/icons/icons';
import { Block, type BlockProps } from './Block';
import { Blocks } from './Blocks';
@@ -4,7 +4,7 @@ import {
type DocumentBlockPrompt,
type SiteCustomizationSettings,
} from '@gitbook/api';
import { validateIconName } from '@gitbook/icons/validateIconName';
import { validateIconName } from '@gitbook/icons/icons';
import type { BlockProps } from '../Block';
import { CodeBlock } from '../CodeBlock';
@@ -12,13 +12,7 @@ mock.module('./RecordCard', () => ({
}));
mock.module('./TableSearch', () => ({
TableSearchRecord: ({
children,
visibleClassName,
}: {
children: React.ReactNode;
visibleClassName: string;
}) => <div className={visibleClassName}>{children}</div>,
TableSearchRecord: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}));
const { ViewCards } = await import('./ViewCards');
@@ -62,38 +56,9 @@ describe('ViewCards', () => {
expect(markup).toContain('snap-mandatory');
});
it.each([true, undefined])('renders the wrapping grid with wrap=%s', (wrap) => {
const props = makeProps(true);
props.view.wrap = wrap;
const markup = renderWithContext(<ViewCards {...props} />);
it('renders the wrapping grid by default', () => {
const markup = renderWithContext(<ViewCards {...makeProps(true)} />);
expect(markup).toContain('inline-grid');
});
it('renders small cards in four columns and uses the grid for print', () => {
const props = makeProps(true);
props.view.cardSize = 'small';
expect(renderWithContext(<ViewCards {...props} />)).toContain('@3xl:grid-cols-4');
props.view.wrap = false;
const carousel = renderWithContext(<ViewCards {...props} />);
expect(carousel).toContain('@3xl:w-[calc(22.5%-0.75rem)]');
expect(carousel).toContain('@5xl:w-[calc(25%-0.75rem)]');
props.context.mode = 'print';
const printed = renderWithContext(<ViewCards {...props} />);
expect(printed).toContain('@3xl:grid-cols-4');
expect(printed).not.toContain('snap-mandatory');
});
it.each([
['medium', '@2xl:grid-cols-3 @sm:grid-cols-2', '@5xl:w-[calc(33.33%-0.66rem)]'],
['large', '@xl:grid-cols-2', '@5xl:w-[calc(50%-0.5rem)]'],
['unknown', '@2xl:grid-cols-3 @sm:grid-cols-2', '@5xl:w-[calc(33.33%-0.66rem)]'],
])('preserves %s card layouts', (cardSize, grid, carousel) => {
const props = makeProps(true);
props.view.cardSize = cardSize;
expect(renderWithContext(<ViewCards {...props} />)).toContain(grid);
props.view.wrap = false;
expect(renderWithContext(<ViewCards {...props} />)).toContain(carousel);
});
});
@@ -6,18 +6,6 @@ import { TableSearchRecord } from './TableSearch';
import { ScrollContainer } from '@/components/primitives/ScrollContainer';
import { tcls } from '@/lib/tailwind';
const gridColumns = {
small: '@sm:grid-cols-2 @2xl:grid-cols-3 @3xl:grid-cols-4',
medium: '@2xl:grid-cols-3 @sm:grid-cols-2',
large: '@xl:grid-cols-2',
};
const carouselWidths = {
small: 'w-[90%] @sm:w-[calc(45%-0.5rem)] @xl:w-[calc(30%-0.66rem)] @3xl:w-[calc(22.5%-0.75rem)] @5xl:w-[calc(25%-0.75rem)]',
medium: 'w-[90%] @sm:w-[calc(45%-0.5rem)] @xl:w-[calc(30%-0.66rem)] @5xl:w-[calc(33.33%-0.66rem)]',
large: 'w-[90%] @sm:w-[calc(45%-0.5rem)] @5xl:w-[calc(50%-0.5rem)]',
};
export function ViewCards(props: TableViewProps<DocumentTableViewCards>) {
// `wrap` defaults to `true` (a wrapping grid); only an explicit `false` opts into the
// horizontally-scrolling carousel row. Fall back to the grid in print mode: a PDF can't
@@ -42,7 +30,7 @@ function CardsGrid(props: TableViewProps<DocumentTableViewCards>) {
'inline-grid',
'gap-4',
'grid-cols-1',
gridColumns[view.cardSize] ?? gridColumns.medium,
view.cardSize === 'large' ? '@xl:grid-cols-2' : '@2xl:grid-cols-3 @sm:grid-cols-2', // Large cards break earlier to avoid becoming *too* big.
block.data.fullWidth ? 'large:flex-column' : null
)}
>
@@ -82,6 +70,13 @@ const PEEK = {
function CardsCarousel(props: TableViewProps<DocumentTableViewCards>) {
const { view, records } = props;
// Cards need a fixed width so the row overflows and scrolls; mirror the grid's
// medium/large sizing.
const cardWidth =
view.cardSize === 'large'
? 'w-[90%] @sm:w-[calc(45%-0.5rem)] @5xl:w-[calc(50%-0.5rem)]'
: 'w-[90%] @sm:w-[calc(45%-0.5rem)] @xl:w-[calc(30%-0.66rem)] @5xl:w-[calc(33.33%-0.66rem)]';
return (
<ScrollContainer
orientation="horizontal"
@@ -120,7 +115,7 @@ function CardsCarousel(props: TableViewProps<DocumentTableViewCards>) {
'grid-cols-1',
'shrink-0',
'snap-start',
carouselWidths[view.cardSize] ?? carouselWidths.medium
cardWidth
)}
>
<RecordCard {...props} record={record} />
@@ -1,6 +1,6 @@
import type { DocumentBlockTabs } from '@gitbook/api';
import type { IconName } from '@gitbook/icons';
import { validateIconName } from '@gitbook/icons/validateIconName';
import { validateIconName } from '@gitbook/icons/icons';
import type { BlockProps } from '../Block';
import { Blocks } from '../Blocks';
@@ -125,9 +125,6 @@ function Color(props: MarkedLeafProps<DocumentMarkColor>) {
const { mark, children } = props;
return (
<span
data-highlight={
mark.data.background && mark.data.background !== 'default' ? '' : undefined
}
className={tcls([
textColorToStyle[mark.data.text ?? 'default'],
backgroundColorToStyle[mark.data.background ?? 'default'],
@@ -12,7 +12,6 @@ import { EmbeddableIframeAPI } from './EmbeddableIframeAPI';
import { EmbeddableThemeSync } from './EmbeddableThemeSync';
import { IfEmbeddableTrademark } from './EmbeddableTrademark';
import { CustomizationRootLayout } from '@/components/RootLayout';
import { getDirectSearchProps } from '@/components/Search/search-props';
import {
SiteLayoutClientContexts,
generateSiteLayoutMetadata,
@@ -61,7 +60,6 @@ export async function EmbeddableRootLayout({
contextId={context.contextId}
proxyOrigin={context.site.proxy?.origin}
searchPrewarmURL={context.linker.toPathInSite('~gitbook/search-prewarm')}
directSearch={getDirectSearchProps(context)}
>
{/* Persist an explicit ?theme= override so it survives tab navigation. RND-11571 */}
<EmbeddableThemeSync forcedTheme={rememberedTheme} />
@@ -73,11 +73,7 @@ export function ScrollSectionsList({ sections }: { sections: DocumentSection[] }
'mt-2',
'first:mt-0',
'mb-0.5',
section.depth > 1 && [
section.depth > 2 ? 'ml-6' : 'ml-3',
'my-0',
'sidebar-list-line:ml-0',
]
section.depth > 1 && ['ml-3', 'my-0', 'sidebar-list-line:ml-0']
)}
ref={activeId === section.id ? activeItemRef : null}
{...(section.tags !== undefined
@@ -123,9 +119,7 @@ export function ScrollSectionsList({ sections }: { sections: DocumentSection[] }
section.depth > 1 && [
'subitem',
section.depth > 2
? 'sidebar-list-line:pl-9'
: 'sidebar-list-line:pl-6',
'sidebar-list-line:pl-6',
'opacity-8',
'contrast-more:opacity-11',
@@ -508,18 +508,6 @@ html.dark .highlight-line.diff-deleted .highlight-line-content::before {
@apply text-contrast-cover;
}
}
/*
Inline code and highlights sit on their own background, not the cover, so they keep the regular
text color. Inherited, the transparent fill of a split element hides their text behind that
background. Kept out of the utilities layer so a highlight's own text color still applies.
*/
@media not (prefers-contrast: more) {
[data-over-cover] :is(code, [data-highlight]) {
color: var(--cover-text-below);
-webkit-text-fill-color: currentcolor;
}
}
}
/* Zoomable images */
@@ -1,139 +0,0 @@
import { afterEach, describe, expect, it, spyOn } from 'bun:test';
import { createSearchPassStore } from './search-pass-store';
import type { DirectSearchProps } from '@/lib/search-results';
const endpoint = 'https://api.gitbook.com/v1/orgs/org_1/sites/site_1/search';
const directSearch: DirectSearchProps = {
apiURL: 'https://api.gitbook.com',
organizationId: 'org_1',
siteId: 'site_1',
linker: {
urlMode: 'url-host',
siteHost: 'docs.example.com',
siteBasePath: '/',
spaceBasePath: '/',
gitbookURL: undefined,
},
spaces: {},
passURL: '/~gitbook/search-pass',
};
/** Answer each pass request with a new pass, and each API search with the next status. */
function mockFetch(apiStatuses: number[], passLifetimesMs: number[] = []) {
const authorizations: (string | null)[] = [];
let passes = 0;
const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation((async (
input: RequestInfo | URL,
init?: RequestInit
) => {
if (init?.signal?.aborted) {
throw new DOMException('Aborted', 'AbortError');
}
if (String(input) === '/~gitbook/search-pass') {
passes += 1;
return Response.json({
token: `pass-${passes}`,
expiresIn: passLifetimesMs.shift() ?? 300_000,
});
}
expect(String(input)).toBe(endpoint);
authorizations.push(new Headers(init?.headers).get('authorization'));
const status = apiStatuses.shift() ?? 200;
return status === 200 ? Response.json({ items: [] }) : new Response(null, { status });
}) as typeof fetch);
return { authorizations, fetchSpy };
}
function search(store: ReturnType<typeof createSearchPassStore>, signal?: AbortSignal) {
return store.search({
query: 'tokens',
scope: { mode: 'all' },
signal: signal ?? new AbortController().signal,
asEmbeddable: false,
});
}
describe('createSearchPassStore', () => {
let restore: (() => void) | undefined;
afterEach(() => restore?.());
it('stays on the route for the rest of the page after a 403', async () => {
const { authorizations, fetchSpy } = mockFetch([403]);
restore = () => fetchSpy.mockRestore();
const store = createSearchPassStore(directSearch);
await store.requestPass();
expect(await search(store)).toEqual({ type: 'route', reason: '403' });
await store.requestPass();
expect(await search(store)).toEqual({ type: 'route', reason: '403' });
expect(authorizations).toHaveLength(1);
});
it.each([
['an expired pass', [], [1_000], 'expired', ['Bearer pass-2']],
['a 401', [401], [], '401', ['Bearer pass-1', 'Bearer pass-2']],
])(
'sends the search to the route on %s and searches directly again with a new pass',
async (_name, apiStatuses, passLifetimesMs, reason, sent) => {
const { authorizations, fetchSpy } = mockFetch(apiStatuses, passLifetimesMs);
restore = () => fetchSpy.mockRestore();
const store = createSearchPassStore(directSearch);
await store.requestPass();
expect(await search(store)).toEqual({ type: 'route', reason });
await new Promise((resolve) => setTimeout(resolve, 0));
expect(await search(store)).toEqual({ type: 'results', results: [] });
expect(authorizations).toEqual(sent);
}
);
it('keeps the one-refresh bound when a 401 arrives while a refresh is pending', async () => {
const replies = {
search: Promise.withResolvers<Response>(),
pass: Promise.withResolvers<Response>(),
};
let passRequests = 0;
const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation((async (
input: RequestInfo | URL
) => {
if (String(input) === '/~gitbook/search-pass') {
passRequests += 1;
return passRequests === 2
? replies.pass.promise
: Response.json({ token: `pass-${passRequests}`, expiresIn: 300_000 });
}
return passRequests === 1
? replies.search.promise
: new Response(null, { status: 401 });
}) as typeof fetch);
restore = () => fetchSpy.mockRestore();
const store = createSearchPassStore(directSearch);
await store.requestPass();
const inFlight = search(store);
const refresh = store.requestPass();
replies.search.resolve(new Response(null, { status: 401 }));
expect(await inFlight).toEqual({ type: 'route', reason: '401' });
replies.pass.resolve(Response.json({ token: 'pass-2', expiresIn: 300_000 }));
await refresh;
expect(await search(store)).toEqual({ type: 'route', reason: '401' });
expect(passRequests).toBe(2);
});
it('neither retries nor falls back when the search is aborted', async () => {
const { authorizations, fetchSpy } = mockFetch([]);
restore = () => fetchSpy.mockRestore();
const store = createSearchPassStore(directSearch);
await store.requestPass();
const controller = new AbortController();
controller.abort();
await expect(search(store, controller.signal)).rejects.toThrow();
expect(await search(store)).toEqual({ type: 'results', results: [] });
expect(authorizations).toHaveLength(1);
});
});
@@ -1,258 +0,0 @@
import ReactDOM from 'react-dom';
import type { SearchRecordResult, SearchSpaceResult } from '@gitbook/api';
import type { OrderedComputedResult, SearchSiteContentScope } from './search-types';
import { getEmbeddableLinker } from '@/lib/embeddable-linker';
import { createSiteLinker } from '@/lib/links';
import {
type DirectSearchProps,
computePublishedSearchResults,
toSearchSiteContentBody,
} from '@/lib/search-results';
/** Request header on a search route call naming why the search did not go to the API. */
export const SEARCH_FALLBACK_HEADER = 'x-gitbook-search-fallback';
const PASS_REQUEST_TIMEOUT_MS = 3_000;
const EXPIRY_MARGIN_MS = 5_000;
const RATE_LIMIT_WAIT_MS = 2_000;
// The longest shared rate limit window of the search API.
const RATE_LIMIT_MAX_WAIT_MS = 60_000;
const DIRECT_SEARCH_TIMEOUT_MS = 5_000;
// Which limit answered a 429: `caller` is the pass's own, `capacity` a saturated index.
const RATE_LIMIT_SCOPE_HEADER = 'x-ratelimit-scope';
type SearchPassState =
| { status: 'none'; reason?: string }
| { status: 'ready'; token: string; expiresAt: number; replacesRejectedPass: boolean }
| { status: 'route'; reason: string; cooldownUntil: number };
type DirectSearchOutcome =
| { type: 'results'; results: OrderedComputedResult[] }
| { type: 'route'; reason?: string };
export type SearchPassStore = ReturnType<typeof createSearchPassStore>;
/**
* Hold a search pass from the site's search pass route in memory, and search the API with it.
*/
export function createSearchPassStore(directSearch: DirectSearchProps | undefined) {
let state: SearchPassState = { status: 'none' };
let pendingRequest: Promise<void> | null = null;
const sendPassRequest = async () => {
if (!directSearch || state.status === 'route') {
return;
}
// The held pass may belong to a session that has since ended, so it waits for the reply.
if (state.status === 'ready') {
state = state.replacesRejectedPass
? { status: 'none', reason: '401' }
: { status: 'none' };
}
const request = new AbortController();
const timeout = setTimeout(() => request.abort(), PASS_REQUEST_TIMEOUT_MS);
try {
const response = await fetch(directSearch.passURL, {
method: 'POST',
signal: request.signal,
});
const { token, expiresIn } =
response.status === 200
? ((await response.json()) as { token?: unknown; expiresIn?: unknown })
: {};
// A search may have moved the page to the route while this request was in flight.
const current = state as SearchPassState;
if (
current.status !== 'route' &&
typeof token === 'string' &&
typeof expiresIn === 'number'
) {
state = {
status: 'ready',
token,
expiresAt: Date.now() + expiresIn,
replacesRejectedPass:
(current.status === 'none' && current.reason === '401') ||
(current.status === 'ready' && current.replacesRejectedPass),
};
ReactDOM.preconnect(directSearch.apiURL, { crossOrigin: 'anonymous' });
} else if (current.status === 'ready') {
state = { status: 'none' };
}
if (!response.ok) {
throw new Error(`Search pass request failed: ${response.status}`);
}
} catch (error) {
const current = state as SearchPassState;
// Keep the 401 so a later replacement pass is still refused only once.
if (
current.status === 'ready' ||
(current.status === 'none' && current.reason !== '401')
) {
state = { status: 'none', reason: 'pass_request' };
}
throw error;
} finally {
clearTimeout(timeout);
}
};
const requestPass = () => {
pendingRequest ??= sendPassRequest().finally(() => {
pendingRequest = null;
});
return pendingRequest;
};
const dropPass = (token: string, reason: string): DirectSearchOutcome => {
if (state.status === 'ready' && state.token === token) {
state = { status: 'none', reason };
void requestPass().catch(() => undefined);
} else if (state.status === 'none' && reason === '401') {
// The pass was suspended for a pending request: its reply must still see the rejection.
state = { status: 'none', reason };
}
return { type: 'route', reason };
};
const fallBack = (reason: string): DirectSearchOutcome => {
state = { status: 'route', reason, cooldownUntil: 0 };
return { type: 'route', reason };
};
const search = async (input: {
query: string;
scope: SearchSiteContentScope;
signal: AbortSignal;
asEmbeddable: boolean;
}): Promise<DirectSearchOutcome> => {
const { query, scope, signal, asEmbeddable } = input;
if (!directSearch) {
return { type: 'route' };
}
if (state.status === 'ready' && state.expiresAt - Date.now() <= EXPIRY_MARGIN_MS) {
return dropPass(state.token, 'expired');
}
if (state.status === 'route') {
return waitForCooldown(state, signal);
}
if (state.status !== 'ready') {
return { type: 'route', reason: state.reason };
}
if (query.length <= 1) {
return { type: 'results', results: [] };
}
const { token, replacesRejectedPass } = state;
const { apiURL, organizationId, siteId } = directSearch;
// A request dropped on the way to the API never settles, so it gets its own time limit.
// The browser targets predate `AbortSignal.any`.
const request = new AbortController();
if (signal.aborted) {
request.abort();
}
signal.addEventListener('abort', () => request.abort(), { once: true });
const timeout = setTimeout(() => request.abort(), DIRECT_SEARCH_TIMEOUT_MS);
let response: Response;
try {
response = await fetch(`${apiURL}/v1/orgs/${organizationId}/sites/${siteId}/search`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(toSearchSiteContentBody(query, scope)),
credentials: 'omit',
signal: request.signal,
});
} catch (error) {
if (signal.aborted) {
throw error;
}
return fallBack(request.signal.aborted ? 'timeout' : 'network');
}
if (response.status === 401) {
return replacesRejectedPass ? fallBack('401') : dropPass(token, '401');
}
if (response.status === 429) {
state = {
status: 'route',
reason: '429',
cooldownUntil: Date.now() + getRateLimitWaitMs(response),
};
return waitForCooldown(state, signal);
}
if (!response.ok) {
return fallBack(String(response.status));
}
let results: OrderedComputedResult[] | null;
try {
const { items } = (await response.json()) as {
items: (SearchSpaceResult | SearchRecordResult)[];
};
const linker = createSiteLinker(directSearch.linker);
results = computePublishedSearchResults({
asEmbeddable,
linker: asEmbeddable ? getEmbeddableLinker(linker) : linker,
searchResults: items,
spaces: directSearch.spaces,
});
} catch (error) {
if (signal.aborted) {
throw error;
}
return fallBack(request.signal.aborted ? 'timeout' : 'invalid');
} finally {
clearTimeout(timeout);
}
return results ? { type: 'results', results } : fallBack('unknown_space');
};
return { requestPass, search };
}
/**
* Hold a search until a short rate limit cooldown ends, then send it to the route.
* During a longer cooldown the search fails at once, as the route would.
*/
async function waitForCooldown(
route: Extract<SearchPassState, { status: 'route' }>,
signal: AbortSignal
): Promise<DirectSearchOutcome> {
const waitMs = route.cooldownUntil - Date.now();
if (waitMs > RATE_LIMIT_WAIT_MS) {
throw new Error('Search is rate limited');
}
if (waitMs > 0) {
await new Promise((resolve) => setTimeout(resolve, waitMs));
if (signal.aborted) {
throw new DOMException('Search aborted', 'AbortError');
}
}
return { type: 'route', reason: route.reason };
}
/**
* How long to hold searches after a 429: until the API's rate limit resets, when it says so.
*/
function getRateLimitWaitMs(response: Response): number {
switch (response.headers.get(RATE_LIMIT_SCOPE_HEADER)) {
// The route does not use the pass, and an unnamed limit may come from an older API.
case null:
case 'caller':
case 'capacity':
return RATE_LIMIT_WAIT_MS;
default: {
const resetSeconds = Number(response.headers.get('x-ratelimit-reset'));
if (!resetSeconds) {
return RATE_LIMIT_WAIT_MS;
}
return Math.min(Math.max(resetSeconds * 1000 - Date.now(), 0), RATE_LIMIT_MAX_WAIT_MS);
}
}
}
@@ -2,8 +2,6 @@ import type { SiteExternalLink, SiteSection, SiteSectionGroup, SiteSpace } from
import { hasMultipleSiteSections } from '../SiteSections';
import type { GitBookSiteContext } from '@/lib/context';
import { getSearchSpaceLookup } from '@/lib/search';
import type { DirectSearchProps } from '@/lib/search-results';
import { getLocalizedTitle } from '@/lib/sites';
export type SearchSection = Pick<SiteSection, 'id' | 'icon' | 'object'> & {
@@ -123,18 +121,3 @@ export function findSearchSection(
return undefined;
}
/**
* What the browser needs to search the API directly, built from the site spaces visitors can see.
*/
export function getDirectSearchProps(context: GitBookSiteContext): DirectSearchProps | undefined {
if (!context.directSearch) {
return undefined;
}
return {
...context.directSearch,
spaces: getSearchSpaceLookup(context.structure),
passURL: context.linker.toPathInSite('~gitbook/search-pass'),
};
}
@@ -10,10 +10,7 @@ import {
import React from 'react';
import type { LinkProps } from '../primitives';
import { type SearchPassStore, createSearchPassStore } from './search-pass-store';
import { createSearchPrewarmer } from './search-prewarm';
import { useSearchPrewarm } from './useSearchPrewarm';
import type { DirectSearchProps } from '@/lib/search-results';
export type SearchScope =
/** Search all content on the site */
@@ -52,7 +49,6 @@ export type UpdateSearchState = (
const SetSearchStateContext = React.createContext<UpdateSearchState | undefined>(undefined);
const SearchStateContext = React.createContext<SearchState | null | undefined>(undefined);
const SearchPassContext = React.createContext<SearchPassStore | null>(null);
/**
* Normalize the raw state to handle legacy formats.
@@ -77,9 +73,9 @@ export function shouldKeepSearchState(
}
export function SearchContextProvider(
props: React.PropsWithChildren<{ prewarmURL: string; directSearch?: DirectSearchProps }>
props: React.PropsWithChildren<{ prewarmURL: string }>
): React.ReactElement {
const { children, prewarmURL, directSearch } = props;
const { children, prewarmURL } = props;
// URL-backed state
const [rawState, setRawState] = useQueryStates(keyMap, {
@@ -105,12 +101,6 @@ export function SearchContextProvider(
useSearchPrewarm(Boolean(state?.open), prewarmURL);
const searchPass = React.useMemo(() => createSearchPassStore(directSearch), [directSearch]);
const passRequester = React.useMemo(() => createSearchPrewarmer(false), []);
React.useEffect(() => {
passRequester.update(Boolean(state?.open), searchPass.requestPass);
}, [state?.open, searchPass, passRequester]);
const stateRef = React.useRef(state);
React.useLayoutEffect(() => {
stateRef.current = state;
@@ -146,18 +136,12 @@ export function SearchContextProvider(
return (
<SearchStateContext.Provider value={state}>
<SetSearchStateContext.Provider value={setState}>
<SearchPassContext.Provider value={searchPass}>
{children}
</SearchPassContext.Provider>
{children}
</SetSearchStateContext.Provider>
</SearchStateContext.Provider>
);
}
export function useSearchPass() {
return React.useContext(SearchPassContext);
}
export function useSetSearchState() {
const setState = React.useContext(SetSearchStateContext);
if (setState === undefined) {
@@ -11,10 +11,9 @@ import {
import { computeFilterSiteSpaceIds, filterResultsBySiteSpaces } from './filter';
import { useRecentSearchQueries } from './recent-queries';
import { computeRemoteSearchScope } from './remote-scope';
import { SEARCH_FALLBACK_HEADER, type SearchPassStore } from './search-pass-store';
import type { OrderedComputedResult, SearchSiteContentScope } from './search-types';
import { streamRecommendedQuestions } from './server-actions';
import { type SearchScope, useSearchPass } from './useSearch';
import type { SearchScope } from './useSearch';
import { useAI } from '@/components/AI';
export type ResultType = OrderedComputedResult | RecommendedQuestionResult;
@@ -64,7 +63,6 @@ export function useSearchResults(props: {
} = props;
const trackEvent = useTrackEvent();
const searchPass = useSearchPass();
const filterSiteSpaceIds = React.useMemo(
() =>
@@ -176,14 +174,7 @@ export function useSearchResults(props: {
const fetchSearch = (
scope: Parameters<typeof fetchSearchResults>[1]
): Promise<OrderedComputedResult[]> =>
fetchSearchResults(
searchURL,
scope,
query,
abortController.signal,
asEmbeddable,
searchPass
);
fetchSearchResults(searchURL, scope, query, abortController.signal, asEmbeddable);
try {
const resultsPromise = fetchSearch(remoteScope);
@@ -218,7 +209,7 @@ export function useSearchResults(props: {
setRemoteState({ status: 'error' });
}
};
const timeout = setTimeout(send, consumeImmediate?.(query) ? 0 : 150);
const timeout = setTimeout(send, consumeImmediate?.(query) ? 0 : 100);
flushRef.current = () => {
clearTimeout(timeout);
void send();
@@ -249,7 +240,6 @@ export function useSearchResults(props: {
searchURL,
asEmbeddable,
consumeImmediate,
searchPass,
]);
const flush = React.useCallback(() => {
@@ -306,32 +296,18 @@ export function useSearchResults(props: {
}
/**
* Fetch search results from the search API with a search pass, or else from the search API route.
* Fetch search results from the search API route.
*/
async function fetchSearchResults(
searchURL: string,
scope: SearchSiteContentScope,
query: string,
signal: AbortSignal,
asEmbeddable: boolean | undefined,
searchPass: SearchPassStore | null
signal?: AbortSignal,
asEmbeddable?: boolean
): Promise<OrderedComputedResult[]> {
const direct = await searchPass?.search({
query,
scope,
signal,
asEmbeddable: Boolean(asEmbeddable),
});
if (direct?.type === 'results') {
return direct.results;
}
const response = await fetch(searchURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(direct?.reason ? { [SEARCH_FALLBACK_HEADER]: direct.reason } : {}),
},
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
asEmbeddable,
query,
@@ -11,7 +11,6 @@ import { SiteLayoutClientContexts } from './SiteLayoutClientContexts';
import { AdminToolbar } from '@/components/AdminToolbar';
import { CookiesToast } from '@/components/Cookies';
import { LoadIntegrations } from '@/components/Integrations';
import { getDirectSearchProps } from '@/components/Search/search-props';
import { SpaceLayout } from '@/components/SpaceLayout';
import { WebMCP } from '@/components/WebMCP/WebMCP';
import type { VisitorAuthClaims } from '@/lib/adaptive';
@@ -98,7 +97,6 @@ export async function SiteLayout(props: {
context.linker.toAbsoluteURL(context.linker.toPathInSite(''))
)}
searchPrewarmURL={context.linker.toPathInSite('~gitbook/search-prewarm')}
directSearch={getDirectSearchProps(context)}
>
<AIContextProvider
aiMode={customization.ai?.mode}
@@ -13,7 +13,6 @@ import { LinkContext, type LinkContextType } from '../primitives';
import { SearchContextProvider } from '../Search';
import { SelectProvider } from '../Select';
import { isExternalLink } from '../utils/link';
import type { DirectSearchProps } from '@/lib/search-results';
/**
* Client component context providers for the site layout.
@@ -28,7 +27,6 @@ export function SiteLayoutClientContexts(props: {
/** Absolute URL of the site, so same-site absolute links render as internal on the server. */
siteURL?: string;
searchPrewarmURL: string;
directSearch: DirectSearchProps | undefined;
children: React.ReactNode;
}) {
const {
@@ -41,7 +39,6 @@ export function SiteLayoutClientContexts(props: {
proxyOrigin,
siteURL,
searchPrewarmURL,
directSearch,
} = props;
useClearRouterCache(contextId);
@@ -75,10 +72,7 @@ export function SiteLayoutClientContexts(props: {
<NuqsAdapter>
<SelectProvider>
<LinkContext.Provider value={linkContext}>
<SearchContextProvider
prewarmURL={searchPrewarmURL}
directSearch={directSearch}
>
<SearchContextProvider prewarmURL={searchPrewarmURL}>
<ReducedMotionProvider>{children}</ReducedMotionProvider>
</SearchContextProvider>
</LinkContext.Provider>
@@ -224,7 +224,7 @@ function NotFoundSuggestions(props: { suggestions: RelatedPage[] | null }) {
<h2 className="font-heading font-medium text-tint">
{t(language, 'notfound_suggestions_title')}
</h2>
<ul className="flex flex-col gap-2" aria-busy={loading}>
<ul className="flex flex-col gap-2">
{loading ? (
<SkeletonList items={RELATED_PAGES_COUNT} className="my-0.75 max-w-md" />
) : (
+24 -51
View File
@@ -20,15 +20,9 @@ import type {
TranslationLanguage,
} from '@gitbook/api';
import { GITBOOK_API_PUBLIC_URL, GITBOOK_URL } from './env';
import { GITBOOK_URL } from './env';
import { type ImageResizer, createImageResizer } from './images';
import {
type GitBookLinker,
type SiteLinkerInput,
createSiteLinker,
linkerForPublishedURL,
} from './links';
import { isPublishedPreviewAllowed } from './preview';
import { type GitBookLinker, createLinker, linkerForPublishedURL } from './links';
import type { AskVisitorAgent } from '@/lib/ask-visitor';
import {
type GitBookDataFetcher,
@@ -37,7 +31,6 @@ import {
throwIfDataError,
} from '@/lib/data';
import { getLogger } from '@/lib/logger';
import type { DirectSearchProps } from '@/lib/search-results';
import {
findSiteSpaceBy,
getFallbackSiteSpacePath,
@@ -140,11 +133,6 @@ export type GitBookBaseContext = {
* Translation language of the space.
*/
locale?: TranslationLanguage;
/**
* Target for the browser to query the site search API directly with a search pass.
*/
directSearch?: Omit<DirectSearchProps, 'spaces' | 'passURL'>;
};
/**
@@ -252,14 +240,28 @@ export function getBaseContext(input: {
apiToken: siteURLData.apiToken ?? null,
});
const linkerInput: SiteLinkerInput = {
urlMode,
siteHost: siteURL.host,
siteBasePath: siteURLData.siteBasePath,
spaceBasePath: siteURLData.basePath,
gitbookURL: GITBOOK_URL || undefined,
};
const linker = createSiteLinker(linkerInput);
const gitbookURL = GITBOOK_URL ? new URL(GITBOOK_URL) : undefined;
const linker =
urlMode === 'url-host'
? createLinker({
host: siteURL.host,
siteBasePath: siteURLData.siteBasePath,
spaceBasePath: siteURLData.basePath,
})
: createLinker({
protocol: gitbookURL?.protocol,
host: gitbookURL?.host,
siteBasePath: `/url/${siteURL.host}${siteURLData.siteBasePath}`,
spaceBasePath: `/url/${siteURL.host}${siteURLData.basePath}`,
});
if (urlMode === 'url') {
// Create link in the same format for links to other sites/sections.
linker.toLinkForContent = (rawURL: string) => {
const urlObject = new URL(rawURL);
return `/url/${urlObject.host}${urlObject.pathname}${urlObject.search}${urlObject.hash}`;
};
}
const imageResizer = createImageResizer({
imagesContextId: siteURLData.imagesContextId,
@@ -268,21 +270,10 @@ export function getBaseContext(input: {
linker: linker,
});
const directSearch: GitBookBaseContext['directSearch'] =
!siteURLData.revision && !siteURLData.changeRequest && !siteURLData.preview
? {
apiURL: GITBOOK_API_PUBLIC_URL,
organizationId: siteURLData.organization,
siteId: siteURLData.site,
linker: linkerInput,
}
: undefined;
return {
dataFetcher,
linker,
imageResizer,
directSearch,
};
}
@@ -349,24 +340,6 @@ export async function fetchSiteContextByIds(
fetchSpaceContextByIds(baseContext, ids),
]);
const { publicChangeRequestPreviews } = orgSite as Site & {
publicChangeRequestPreviews?: boolean;
};
if (
!isPublishedPreviewAllowed({
publicChangeRequestPreviews,
preview: ids.preview,
changeRequest: ids.changeRequest,
revision: ids.revision,
})
) {
getLogger().info('Refused disabled published preview', {
site: ids.site,
selector: ids.changeRequest ? 'change-request' : 'revision',
});
notFound();
}
const sections = ids.siteSection
? parseSiteSectionsAndGroups(siteStructure, ids.siteSection)
: null;
+50 -34
View File
@@ -1,51 +1,67 @@
import { afterAll, afterEach, describe, expect, it, mock } from 'bun:test';
import { afterEach, describe, expect, it, mock } from 'bun:test';
const cacheLife = mock((_profile: unknown) => {});
const cacheTag = mock((..._tags: string[]) => {});
const realNextCache = await import('next/cache');
mock.module('next/cache', () => ({ ...realNextCache, cacheLife, cacheTag }));
const realGlobals = await import('@/lib/env/globals');
mock.module('@/lib/env/globals', () => ({
...realGlobals,
GITBOOK_API_URL: 'https://api.example.com/cache/',
}));
const { createDataFetcher } = await import('./api');
import { createDataFetcher } from './api';
const originalFetch = globalThis.fetch;
afterAll(() => mock.restore());
describe('searchSiteContent', () => {
afterEach(() => {
globalThis.fetch = originalFetch;
});
it('reaches the API through the gateway on every search, without caching', async () => {
const fetchMock = mock((_input: unknown, _init?: RequestInit) =>
Promise.resolve(Response.json({ items: [] }))
);
globalThis.fetch = fetchMock as unknown as typeof fetch;
const dataFetcher = createDataFetcher({ apiToken: 'site-token' });
const params = {
function mockFetch(impl: (input: unknown, init?: RequestInit) => Promise<Response>) {
globalThis.fetch = mock(impl) as unknown as typeof fetch;
}
function search(signal?: AbortSignal) {
return createDataFetcher().searchSiteContent({
organizationId: 'org-1',
siteId: 'site-1',
query: 'test',
scope: { mode: 'all' as const },
};
scope: { mode: 'all' },
signal,
});
}
expect(await dataFetcher.searchSiteContent(params)).toEqual({ data: [] });
expect(await dataFetcher.searchSiteContent(params)).toEqual({ data: [] });
it('returns the API results', async () => {
mockFetch(async () => Response.json({ items: [] }));
expect(fetchMock).toHaveBeenCalledTimes(2);
const [input, init] = fetchMock.mock.calls[0]!;
expect(String(input)).toBe(
'https://api.example.com/cache/v1/orgs/org-1/sites/site-1/search'
const result = await search();
expect(result).toEqual({ data: [] });
});
it('returns an API error as a value', async () => {
mockFetch(async () =>
Response.json({ error: { code: 429, message: 'Namespace saturated' } }, { status: 429 })
);
expect(new Headers(init?.headers).get('authorization')).toBe('Bearer site-token');
expect(cacheLife).not.toHaveBeenCalled();
expect(cacheTag).not.toHaveBeenCalled();
const result = await search();
expect(result.error?.code).toBe(429);
});
it('aborts the API call when the search is cancelled', async () => {
let apiCallAborted = false;
const { promise: apiCalled, resolve: onApiCall } = Promise.withResolvers<void>();
mockFetch(
(_input, init) =>
new Promise((_resolve, reject) => {
init?.signal?.addEventListener('abort', () => {
apiCallAborted = true;
reject(init.signal?.reason);
});
setTimeout(() => reject(new Error('API call was not aborted')), 1000);
onApiCall();
})
);
const controller = new AbortController();
const result = search(controller.signal);
await apiCalled;
controller.abort();
expect((await result).error?.code).toBe(499);
expect(apiCallAborted).toBe(true);
});
});
+10 -3
View File
@@ -704,7 +704,7 @@ const getPublishedContentSite = cache(
}
);
cacheTag(...getCacheTagsFromResponse(res));
cacheLifeFromResponse(res, 'days');
cacheLife('days');
return res.data;
}
);
@@ -793,9 +793,9 @@ async function searchSiteContent(
return trace(
`searchSiteContent(${params.organizationId}, ${params.siteId}, ${params.query})`,
async () => {
const { organizationId, siteId, query, scope } = params;
const { organizationId, siteId, query, scope, signal } = params;
const api = apiClient(input);
const res = await api.orgs.searchSiteContent(
const searchRequest = api.orgs.searchSiteContent(
organizationId,
siteId,
{
@@ -815,8 +815,15 @@ async function searchSiteContent(
{},
{
...noCacheFetchOptions,
signal,
}
);
const res = await searchRequest.catch((error) => {
if (signal?.aborted) {
throw new DataFetcherError('Search cancelled', 499);
}
throw error;
});
return res.data.items;
}
);
+11 -1
View File
@@ -2,7 +2,17 @@ import { describe, expect, it } from 'bun:test';
import { GitBookAPIError } from '@gitbook/api';
import { extractCacheControl } from './errors';
import { extractCacheControl, isTransientDataFetcherError } from './errors';
describe('isTransientDataFetcherError', () => {
it.each([408, 429, 500, 502, 503, 504])('should treat %i as transient', (code) => {
expect(isTransientDataFetcherError({ code, message: 'Error' })).toBe(true);
});
it.each([400, 401, 403, 404, 410])('should treat %i as stable', (code) => {
expect(isTransientDataFetcherError({ code, message: 'Error' })).toBe(false);
});
});
describe('extractCacheControl', () => {
it('should return undefined when error has no response', () => {
+7
View File
@@ -93,6 +93,13 @@ export async function wrapDataFetcherError<T>(
}
}
/**
* Whether an error may go away on retry: timeouts, rate limits, server errors and network failures.
*/
export function isTransientDataFetcherError(error: DataFetcherErrorData): boolean {
return error.code === 408 || error.code === 429 || error.code >= 500;
}
/**
* Ignore some data fetcher errors.
*/
+1
View File
@@ -191,6 +191,7 @@ export interface GitBookDataFetcher {
| { mode: 'specific'; siteSpaceIds: string[] };
/** Cache bust to ensure the search results are fresh when the space is updated. */
cacheBust?: string;
signal?: AbortSignal;
}): Promise<DataFetcherResponse<(api.SearchSpaceResult | api.SearchRecordResult)[]>>;
/**
@@ -145,11 +145,7 @@ describe('getDocumentSections', () => {
type: 'update',
data: {
date: '2026-04-08',
tags: [
{ kind: 'tag', tag: 'improvements' },
{ kind: 'tag', tag: 'category' },
{ kind: 'tag', tag: 'missing' },
],
tags: [{ kind: 'tag', tag: 'improvements' }],
},
isVoid: false,
nodes: [
@@ -201,7 +197,6 @@ describe('getDocumentSections', () => {
...context,
revision: {
tags: [
{ slug: 'category', label: 'Products', color: 'default', kind: 'category' },
{
slug: 'improvements',
label: 'Improvements',
@@ -237,66 +232,6 @@ describe('getDocumentSections', () => {
]);
});
it('nests heading-3 under the heading before it', async () => {
const heading = (type: 'heading-1' | 'heading-2' | 'heading-3', id: string) => ({
object: 'block' as const,
type,
data: {},
meta: { id },
nodes: [
{
object: 'text' as const,
leaves: [{ object: 'leaf' as const, text: id, marks: [] }],
},
],
});
const document: JSONDocument = {
object: 'document',
data: { schemaVersion: 2 },
nodes: [
heading('heading-3', 'h3-first'),
heading('heading-2', 'h2-no-h1'),
heading('heading-3', 'h3-under-h2'),
heading('heading-1', 'h1'),
heading('heading-3', 'h3-under-h1'),
heading('heading-2', 'h2'),
heading('heading-3', 'h3-under-h1-h2'),
{
object: 'block',
type: 'stepper',
data: {},
isVoid: false,
nodes: [
{
object: 'block',
type: 'stepper-step',
data: {},
isVoid: false,
nodes: [heading('heading-3', 'h3-in-stepper')],
},
],
},
heading('heading-1', 'h1-again'),
heading('heading-3', 'h3-after-reset'),
],
};
const sections = await getDocumentSections(context, document);
expect(sections.map(({ id, depth }) => ({ id, depth }))).toEqual([
{ id: 'h3-first', depth: 1 },
{ id: 'h2-no-h1', depth: 1 },
{ id: 'h3-under-h2', depth: 2 },
{ id: 'h1', depth: 1 },
{ id: 'h3-under-h1', depth: 2 },
{ id: 'h2', depth: 2 },
{ id: 'h3-under-h1-h2', depth: 3 },
{ id: 'h3-in-stepper', depth: 3 },
{ id: 'h1-again', depth: 1 },
{ id: 'h3-after-reset', depth: 2 },
]);
});
it('extracts a single-schema openapi-schemas block as one section', async () => {
const document: JSONDocument = {
object: 'document',
+8 -27
View File
@@ -21,14 +21,6 @@ export interface DocumentSection {
tags?: string[];
}
/**
* Depth of the last heading-1 and heading-2 seen, or 0 when there is none.
*/
interface HeadingParents {
h1: number;
h2: number;
}
/**
* Extract a list of sections from a document.
*/
@@ -45,11 +37,11 @@ export async function getDocumentSections(
async function getSectionsFromNodes(
nodes: DocumentBlock[],
context: GitBookAnyContext,
parents: HeadingParents = { h1: 0, h2: 0 },
initialDepth = 0,
tags?: string[]
): Promise<DocumentSection[]> {
const sections: DocumentSection[] = [];
let { h1, h2 } = parents;
let depth = initialDepth;
for (const block of nodes) {
switch (block.type) {
@@ -58,10 +50,9 @@ async function getSectionsFromNodes(
if (!id) {
continue;
}
h1 = 1;
h2 = 0;
depth = 1;
const title = getNodeReactText(block);
sections.push(withTags({ id, title, depth: h1 }, tags));
sections.push(withTags({ id, title, depth: 1 }, tags));
continue;
}
case 'heading-2': {
@@ -69,25 +60,15 @@ async function getSectionsFromNodes(
if (!id) {
continue;
}
h2 = h1 + 1;
const title = getNodeReactText(block);
sections.push(withTags({ id, title, depth: h2 }, tags));
continue;
}
case 'heading-3': {
const id = block.meta?.id;
if (!id) {
continue;
}
const title = getNodeReactText(block);
sections.push(withTags({ id, title, depth: (h2 || h1) + 1 }, tags));
sections.push(withTags({ id, title, depth: depth > 0 ? 2 : 1 }, tags));
continue;
}
case 'columns':
case 'stepper': {
const stepNodes = await Promise.all(
block.nodes.map(async (step) =>
getSectionsFromNodes(step.nodes, context, { h1, h2 }, tags)
getSectionsFromNodes(step.nodes, context, depth, tags)
)
);
for (const stepSections of stepNodes) {
@@ -102,7 +83,7 @@ async function getSectionsFromNodes(
getSectionsFromNodes(
update.nodes as DocumentBlock[],
context,
{ h1, h2 },
depth,
resolveBlockTags(update.data.tags, revisionTags).map((tag) => tag.slug)
)
)
@@ -219,7 +200,7 @@ async function getSectionsFromNodes(
const reusableContentSections = await getSectionsFromNodes(
document.nodes,
reusableContent.context,
{ h1, h2 },
depth,
tags
);
sections.push(...reusableContentSections);
+1 -1
View File
@@ -10,9 +10,9 @@ import {
type SiteCustomizationSettings,
} from '@gitbook/api';
import { getIconStyle } from '@gitbook/icons/getIconStyle';
import { validateIconName } from '@gitbook/icons/icons';
import { type InlineIconSource, getInlineIconSourceKey } from '@gitbook/icons/IconSources';
import { type IconName, IconStyle } from '@gitbook/icons/types';
import { validateIconName } from '@gitbook/icons/validateIconName';
import { GITBOOK_ICONS_ASSET_VERSION } from '@gitbook/icons/version';
import { getAssetURL } from '@/lib/assets';
-44
View File
@@ -197,50 +197,6 @@ export function createLinker(
return linker;
}
/**
* Serializable inputs of the linker of a site request.
*/
export type SiteLinkerInput = {
urlMode: 'url' | 'url-host';
siteHost: string;
siteBasePath: string;
spaceBasePath: string;
/** Main host GitBook runs on, used in `url` mode. */
gitbookURL: string | undefined;
};
/**
* Create the linker of a site request, for the URL mode it is served in.
*/
export function createSiteLinker(input: SiteLinkerInput): GitBookLinker {
const { urlMode, siteHost, siteBasePath, spaceBasePath } = input;
const gitbookURL = input.gitbookURL ? new URL(input.gitbookURL) : undefined;
const linker =
urlMode === 'url-host'
? createLinker({
host: siteHost,
siteBasePath,
spaceBasePath,
})
: createLinker({
protocol: gitbookURL?.protocol,
host: gitbookURL?.host,
siteBasePath: `/url/${siteHost}${siteBasePath}`,
spaceBasePath: `/url/${siteHost}${spaceBasePath}`,
});
if (urlMode === 'url') {
// Create link in the same format for links to other sites/sections.
linker.toLinkForContent = (rawURL: string) => {
const urlObject = new URL(rawURL);
return `/url/${urlObject.host}${urlObject.pathname}${urlObject.search}${urlObject.hash}`;
};
}
return linker;
}
/**
* Create a new linker that intercepts links that belongs to the published site and rewrite them
* relative to the URL being served.
+2 -141
View File
@@ -39,7 +39,7 @@ async function rewrite(markdown: string) {
describe('HTML links in page markdown', () => {
it.each([
['/pages/designer', linker.toAbsoluteURL(linker.toPathForPagePath({ path: page.path }))],
['/pages/designer', linker.toPathForPagePath({ path: page.path })],
['/spaces/workflows', context.space.urls.published],
['/pages/missing', 'broken://pages/missing'],
])(
@@ -57,7 +57,7 @@ describe('HTML links in page markdown', () => {
const markdown =
'<table><tr><td><a href="/pages/designer">/pages/designer</a></td></tr></table>\n';
expect(await rewrite(markdown)).toBe(
`<table><tr><td><a href="${linker.toAbsoluteURL(linker.toPathForPagePath({ path: page.path }))}">${page.title}</a></td></tr></table>\n`
`<table><tr><td><a href="${linker.toPathForPagePath({ path: page.path })}">${page.title}</a></td></tr></table>\n`
);
});
});
@@ -79,142 +79,3 @@ describe('HTML images in page markdown', () => {
);
});
});
describe('Markdown URL context', () => {
it.each([
['docs.example.com', '/docs/api/v2/', '/docs/', 'https://docs.example.com/docs/api/v2/'],
[
'preview.example.com',
'/s/site/section/v2/',
'/s/site/',
'https://preview.example.com/s/site/section/v2/',
],
])(
'preserves the served origin and section path on %s',
async (host, spaceBasePath, siteBasePath, base) => {
const scoped = {
...context,
linker: linkerWithMarkdownPages(
createLinker({ host, spaceBasePath, siteBasePath })
),
};
const markdown =
'[Page](/pages/designer)\n\n[Anchor](/pages/designer#details)\n\n[Here](#details)\n\n[Missing](/pages/missing)\n\n[External](https://external.example/a?q=1#b)\n\n[Mail](mailto:test@example.com)\n\n[File](/files/dark-image)\n\n[Relative](../guide?q=1#b)\n\n[Stable][s]\n\n[s]: /pages/designer\n\n[Definition][d]\n\n[d]: ../guide?q=1#b\n\n[Fragment][f]\n\n[f]: #details\n\n![Image](./image.png)\n\n<a href="../guide?q=1&#x26;b=2#b">Guide</a>\n';
const result = toPageMarkdown(
await fromPageMarkdown(scoped, { markdown, pagePath: 'nested/current' })
);
expect(result).toContain(`${base}${page.path}.md`);
expect(result).toContain(`${base}${page.path}.md#details`);
expect(result).toContain('[Here](#details)');
expect(result).toContain('broken://pages/missing');
expect(result).toContain('https://external.example/a?q=1#b');
expect(result).toContain('mailto:test@example.com');
expect(result).toContain(file.downloadURL.replace('&', '\\&'));
expect(result).toContain(`[Relative](${base}guide?q=1#b)`);
expect(result).toContain(`[s]: ${base}${page.path}.md`);
expect(result).toContain(`[d]: ${base}guide?q=1#b`);
expect(result).toContain('[f]: #details');
expect(result).toContain(`![Image](${base}nested/image.png)`);
expect(result).toContain(`<a href="${base}guide?q=1&amp;b=2#b">Guide</a>`);
}
);
});
it('keeps malformed destinations from breaking the page export', async () => {
const markdown =
'<a href="http://">Broken</a>\n\n<img src="http://" alt="Broken">\n\n![Broken](http://)\n\n[Broken](http://)\n';
expect(await rewrite(markdown)).toBe(markdown);
});
it('preserves document fragments in generated answers without a page path', async () => {
const markdown =
'[Details](#details)\n\n[Details][d]\n\n[d]: #details\n\n<a href="#details">Details</a>\n';
expect(toPageMarkdown(await fromPageMarkdown(context, { markdown, pagePath: '' }))).toBe(
markdown
);
});
it.each(['/docs/', '/docs/api/v2/', '/guide/'])(
'resolves relative assets in answers from the space root %s',
async (basePath) => {
const scoped = {
...context,
linker: linkerWithMarkdownPages(
createLinker({
host: 'docs.example.com',
siteBasePath: basePath,
spaceBasePath: basePath,
})
),
};
const markdown =
'![Image](./image.png)\n\n[Guide][d]\n\n[d]: ./guide\n\n<a href="./guide">Guide</a>\n';
const output = toPageMarkdown(await fromPageMarkdown(scoped, { markdown, pagePath: '' }));
expect(output).toContain(`![Image](https://docs.example.com${basePath}image.png)`);
expect(output).toContain(`[d]: https://docs.example.com${basePath}guide`);
expect(output).toContain(`<a href="https://docs.example.com${basePath}guide">Guide</a>`);
}
);
it('preserves code examples and unrelated attributes in raw HTML', async () => {
const markdown =
'<table><tr><td><code>const src="example"; const href="/pages/designer";</code></td></tr></table>\n\n<pre>const src="example"</pre>\n\n<div data-src="./example" data-srcset="./one 1x, ./two 2x">Example</div>\n\n<!-- <img src="./example"> -->\n';
expect(await rewrite(markdown)).toBe(markdown);
});
it.each([
"<a href='/pages/designer'>Designer</a>",
'<A HREF="/pages/designer">Designer</A>',
'<a href = "/pages/designer">Designer</a>',
])('resolves HTML attributes in %s', async (html) => {
const output = await rewrite(html);
expect(output).toContain(
'href="https://docs.example.com/workflows/about-the-workflow-designer.md"'
);
expect(output).toContain('>Designer</');
});
it.each([
['//cdn.example.org/file', 'https://cdn.example.org/file'],
['/docs/guide', 'https://docs.example.com/docs/guide'],
['?view=full', 'https://docs.example.com/workflows/nested/current.md?view=full'],
['./guide?q=1#part', 'https://docs.example.com/workflows/nested/guide?q=1#part'],
['../guide', 'https://docs.example.com/workflows/guide'],
])('resolves %s consistently across Markdown and HTML destinations', async (url, expected) => {
const markdown = `[Inline](${url})\n\n[Definition][d]\n\n[d]: ${url}\n\n![Image](${url})\n\n<a href="${url}">HTML</a><img src="${url}">\n`;
const output = toPageMarkdown(
await fromPageMarkdown(context, { markdown, pagePath: 'nested/current' })
);
expect(output).toContain(`[Inline](${expected})`);
expect(output).toContain(`[d]: ${expected}`);
expect(output).toContain(`![Image](${expected})`);
expect(output).toContain(`<a href="${expected}">HTML</a><img src="${expected}">`);
});
it('preserves overlapping HTML formatting when parser nodes share a source range', async () => {
const id = 'averylongbutvalidpageidentifier1234567890';
const scoped = {
...context,
revision: {
...context.revision,
pages: context.revision.pages.map((entry) => ({ ...entry, id, path: 'a', slug: 'a' })),
},
linker: linkerWithMarkdownPages(
createLinker({ host: 'd.io', siteBasePath: '/', spaceBasePath: '/' })
),
};
const markdown = `<div><b><a href="/pages/${id}">one</b>two</a></div>\n`;
expect(toPageMarkdown(await fromPageMarkdown(scoped, { markdown, pagePath: 'current' }))).toBe(
'<div><b><a href="https://d.io/a.md">one</b>two</a></div>\n'
);
});
it('resolves image attributes and srcset candidates with HTML syntax variants', async () => {
const markdown =
"<PICTURE><SOURCE SRCSET='/files/dark-image 1x, //cdn.example.com/image.png 2x'><IMG SRC = '/files/dark-image' alt='Example'></PICTURE>\n";
const output = await rewrite(markdown);
const url = file.downloadURL.replaceAll('&', '&amp;');
expect(output).toBe(
`<PICTURE><SOURCE srcset="${url} 1x, https://cdn.example.com/image.png 2x"><IMG src="${url}" alt='Example'></PICTURE>\n`
);
});
+89 -123
View File
@@ -5,7 +5,7 @@ import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm';
import { toMarkdown } from 'mdast-util-to-markdown';
import { frontmatter } from 'micromark-extension-frontmatter';
import { gfm } from 'micromark-extension-gfm';
import { type DefaultTreeAdapterMap, parseFragment } from 'parse5';
import path from 'node:path';
import { remove } from 'unist-util-remove';
import { visit } from 'unist-util-visit';
@@ -18,7 +18,6 @@ import {
import { escapeHTML } from './html';
import type { GitBookLinker } from './links';
import { withTrailingSlash } from './paths';
import { resolveContentRef, resolveStringContentRef } from './references';
import { checkIsAnchor, checkIsExternalURL } from './urls';
import {
@@ -31,6 +30,10 @@ import type { ResolvedPagePath } from '@/lib/pages';
import { getIndexablePages } from '@/lib/sitemap';
import { getMarkdownForPagesTree } from '@/routes/llms';
const HTML_ANCHOR_RE = /<a\b([^>]*?)href="([^"]*)"([^>]*)>([\s\S]*?)<\/a>/g;
const HTML_ANCHOR_OPEN_RE = /<a\b([^>]*?)href="([^"]*)"([^>]*)>/g;
const HTML_SRC_RE = /\bsrc="([^"]*)"/g;
const HTML_SRCSET_RE = /\bsrcset="([^"]*)"/g;
const SRCSET_CANDIDATE_RE = /(\S*[^,\s])(?:\s+[^,]*)?(?:,|$)/g;
/**
@@ -252,44 +255,73 @@ async function renderGroupPageMarkdown(args: {
});
}
/** Resolve exported Markdown destinations in their source context. */
/**
* Re-writes URLs in a markdown content:
* - stable content refs (`/pages/:id`, `/spaces/:id/pages/:id`, `/files/:id`...) in links,
* images, definitions and in the `href`/`src` of raw HTML blocks are resolved to site URLs.
* - the URL of every relative <a> link so it is expressed from the site-root.
*/
async function rewriteMarkdownLinks(
context: GitBookAnyContext,
tree: Root,
currentPagePath: string
): Promise<Root> {
const currentDir = path.posix.dirname(currentPagePath);
const pending: Promise<void>[] = [];
visit(tree, 'link', (node: Link) => {
const isMention = isMentionLike(node);
const original = node.url;
pending.push(
(async () => {
const resolved = await resolveRefURL(context, original, currentPagePath);
if (!resolved) {
return;
}
node.url = resolved.url;
if (isMention && resolveStringContentRef(original)) {
node.children = [{ type: 'text', value: resolved.text ?? 'Broken mention' }];
node.title = undefined;
}
})()
);
// Skip anchors, mailto:, http(s):, protocol-like
if (checkIsExternalURL(original) || checkIsAnchor(original)) {
return;
}
const contentRef = resolveStringContentRef(original);
if (contentRef) {
pending.push(
(async () => {
const resolved = await resolveContentRef(contentRef, context);
node.url = resolved?.href ?? toBrokenURL(original);
if (isMention) {
// Replace the text for mentions as otherwise it contains the raw ref
node.children = [
{ type: 'text', value: resolved?.text ?? 'Broken mention' },
];
node.title = undefined;
}
})()
);
} else {
// DEPRECATED: to be removed once rollout for getRevisionPageMarkdown is done
//
// Resolve against the current page’s directory and strip any leading “/” or "../"
// Sometimes the path can be "../" if we are on the default section
// but it means we are just at the root of the site.
const pathInPage = path.posix
.normalize(path.posix.join(currentDir, original))
.replace(/^[/.]+/, '');
node.url = context.linker.toAbsoluteURL(context.linker.toPathInSpace(pathInPage));
}
});
visit(tree, 'image', (node: Image) => {
pending.push(rewriteNodeURL(context, node, currentPagePath));
pending.push(rewriteNodeURL(context, node));
});
visit(tree, 'definition', (node: Definition) => {
pending.push(rewriteNodeURL(context, node, currentPagePath));
pending.push(rewriteNodeURL(context, node));
});
// Blocks markdown cannot express (tables, cards, figures...) are emitted as raw HTML,
// with the same stable refs in their anchors and images.
visit(tree, 'html', (node: Html) => {
pending.push(rewriteHTMLRefs(context, node, currentPagePath));
pending.push(rewriteHTMLRefs(context, node));
});
if (pending.length > 0) {
@@ -300,130 +332,64 @@ async function rewriteMarkdownLinks(
}
/**
* Resolve Markdown destinations independently of the source document origin.
* Resolve a URL if it is a stable content ref. Returns null for anything else
* (external URLs, anchors, plain paths) so the caller leaves it untouched.
*/
async function resolveRefURL(
context: GitBookAnyContext,
url: string,
currentPagePath: string
url: string
): Promise<{ url: string; text: string | null } | null> {
if (checkIsExternalURL(url) || checkIsAnchor(url)) {
return null;
}
const contentRef = resolveStringContentRef(url);
if (!contentRef) {
const pageURL = currentPagePath
? context.linker.toAbsoluteURL(
context.linker.toPathForPagePath({ path: currentPagePath })
)
: withTrailingSlash(context.linker.toAbsoluteURL(context.linker.toPathInSpace('')));
if (!URL.canParse(url, pageURL)) {
return null;
}
return { url: new URL(url, pageURL).toString(), text: url };
return null;
}
const resolved = await resolveContentRef(contentRef, context);
return {
url: resolved ? context.linker.toAbsoluteURL(resolved.href) : toBrokenURL(url),
text: resolved?.text ?? null,
};
return { url: resolved?.href ?? toBrokenURL(url), text: resolved?.text ?? null };
}
async function rewriteNodeURL(
context: GitBookAnyContext,
node: Image | Definition,
currentPagePath: string
) {
const resolved = await resolveRefURL(context, node.url, currentPagePath);
async function rewriteNodeURL(context: GitBookAnyContext, node: Image | Definition) {
const resolved = await resolveRefURL(context, node.url);
if (resolved) {
node.url = resolved.url;
}
}
async function rewriteHTMLRefs(
context: GitBookAnyContext,
node: Html,
currentPagePath: string
): Promise<void> {
const fragment = parseFragment(node.value, { sourceCodeLocationInfo: true });
const elements: DefaultTreeAdapterMap['element'][] = [];
function collect(element: DefaultTreeAdapterMap['node']) {
if ('tagName' in element) {
elements.push(element);
async function rewriteHTMLRefs(context: GitBookAnyContext, node: Html): Promise<void> {
node.value = await replaceAsync(node.value, HTML_ANCHOR_RE, async (match) => {
const [full, before = '', href = '', after = '', text = ''] = match;
const resolved = await resolveRefURL(context, href);
if (!resolved) {
return full;
}
if ('childNodes' in element) {
element.childNodes.forEach(collect);
}
if ('content' in element) {
collect(element.content);
}
}
collect(fragment);
// The API emits the raw ref as the text; swap it for the resolved title.
const content = text === href ? escapeHTML(resolved.text ?? 'Broken link') : text;
return `<a${before}href="${escapeHTML(resolved.url)}"${after}>${content}</a>`;
});
const replacements = new Map<number, { start: number; end: number; value: string }>();
await Promise.all(
elements.map(async (element) => {
for (const attr of element.attrs) {
if (attr.name !== 'href' && attr.name !== 'src' && attr.name !== 'srcset') {
continue;
}
const name = attr.prefix ? `${attr.prefix}:${attr.name}` : attr.name;
const location = element.sourceCodeLocation?.attrs?.[name];
if (!location) {
continue;
}
let value: string;
if (attr.name === 'srcset') {
value = await replaceAsync(
attr.value,
SRCSET_CANDIDATE_RE,
async (candidate) => {
const [full, url = ''] = candidate;
const resolved = await resolveRefURL(context, url, currentPagePath);
return resolved ? resolved.url + full.slice(url.length) : full;
}
);
if (value === attr.value) {
continue;
}
} else {
const resolved = await resolveRefURL(context, attr.value, currentPagePath);
if (!resolved) {
continue;
}
value = resolved.url;
const text = element.childNodes[0];
if (
element.tagName === 'a' &&
attr.name === 'href' &&
resolveStringContentRef(attr.value) &&
element.childNodes.length === 1 &&
text &&
'value' in text &&
text.value === attr.value &&
text.sourceCodeLocation
) {
replacements.set(text.sourceCodeLocation.startOffset, {
start: text.sourceCodeLocation.startOffset,
end: text.sourceCodeLocation.endOffset,
value: escapeHTML(resolved.text ?? 'Broken link'),
});
}
}
replacements.set(location.startOffset, {
start: location.startOffset,
end: location.endOffset,
value: `${name}="${escapeHTML(value)}"`,
});
}
})
);
for (const replacement of [...replacements.values()].sort((a, b) => b.start - a.start)) {
node.value =
node.value.slice(0, replacement.start) +
replacement.value +
node.value.slice(replacement.end);
}
node.value = await replaceAsync(node.value, HTML_ANCHOR_OPEN_RE, async (match) => {
const [full, before = '', href = '', after = ''] = match;
const resolved = await resolveRefURL(context, href);
return resolved ? `<a${before}href="${escapeHTML(resolved.url)}"${after}>` : full;
});
node.value = await replaceAsync(node.value, HTML_SRC_RE, async (match) => {
const [full, src = ''] = match;
const resolved = await resolveRefURL(context, src);
return resolved ? `src="${escapeHTML(resolved.url)}"` : full;
});
node.value = await replaceAsync(node.value, HTML_SRCSET_RE, async (match) => {
const [, srcset = ''] = match;
const rewritten = await replaceAsync(srcset, SRCSET_CANDIDATE_RE, async (candidate) => {
const [full, url = ''] = candidate;
const resolved = await resolveRefURL(context, url);
return resolved ? escapeHTML(resolved.url) + full.slice(url.length) : full;
});
return `srcset="${rewritten}"`;
});
}
async function replaceAsync(
@@ -1,7 +0,0 @@
/**
* Replaces `redis` in the server bundle (see next.config.mjs). mcp-handler imports it statically
* but only uses it for SSE transport, which we disable.
*/
export function createClient(): never {
throw new Error('redis is not available: SSE transport is disabled for the MCP handler');
}
@@ -17,22 +17,4 @@ describe('#enrichFilesystem', () => {
'<p>Social platform<br>Hello</p>'
);
});
it('parses descriptions that collide with Object.prototype keys', async () => {
const { filesystem } = await parseOpenAPI({
value: JSON.stringify({
openapi: '3.0.0',
info: { title: 'Test', version: '1.0.0' },
paths: {
'/a': { get: { description: 'constructor', responses: {} } },
'/b': { get: { description: 'constructor', responses: {} } },
},
}),
rootURL: null,
});
const enriched = await enrichFilesystem(filesystem);
const paths = enriched[0]?.specification.paths;
expect(paths['/a'].get['x-gitbook-description-html']).toBe('<p>constructor</p>');
expect(paths['/b'].get['x-gitbook-description-html']).toBe('<p>constructor</p>');
});
});
+9 -12
View File
@@ -30,17 +30,14 @@ export async function enrichFilesystem(filesystem: Filesystem) {
/**
* Create a markdown parser that caches the results of parsing.
*/
const createMarkdownParser = () => {
// A Map rather than an object so descriptions like "constructor" don't hit prototype keys.
const cache = new Map<string, Promise<string>>();
return (input: string) => {
const existing = cache.get(input);
if (existing) {
return existing;
}
const createMarkdownParser = () => (input: string) => {
const cache: Record<string, Promise<string>> = {};
const existing = cache[input];
if (existing) {
return existing;
}
const result = parseMarkdown(input);
cache.set(input, result);
return result;
};
const result = parseMarkdown(input);
cache[input] = result;
return result;
};
-22
View File
@@ -6,7 +6,6 @@ import {
getPreviewCookieResponse,
getPreviewRequestIdentifier,
isPreviewRequest,
isPublishedPreviewAllowed,
parsePreviewCustomizationCookie,
serializePreviewCustomizationCookie,
} from './preview';
@@ -92,24 +91,3 @@ describe('parsePreviewCustomizationCookie', () => {
).toBeUndefined();
});
});
describe('isPublishedPreviewAllowed', () => {
const disabled = { publicChangeRequestPreviews: false };
it('refuses change requests and revisions on the published site when disabled', () => {
expect(
isPublishedPreviewAllowed({ ...disabled, preview: false, changeRequest: '458' })
).toBe(false);
expect(isPublishedPreviewAllowed({ ...disabled, preview: false, revision: 'rev' })).toBe(
false
);
});
it('allows previews inside GitBook, live pages and sites without the setting', () => {
expect(
isPublishedPreviewAllowed({ ...disabled, preview: true, changeRequest: '458' })
).toBe(true);
expect(isPublishedPreviewAllowed({ ...disabled, preview: false })).toBe(true);
expect(isPublishedPreviewAllowed({ preview: false, changeRequest: '458' })).toBe(true);
});
});
-17
View File
@@ -17,23 +17,6 @@ export function isPreviewRequest(requestURL: URL): boolean {
);
}
/**
* Whether a change request or revision URL can render, given the site's published preview setting.
* Previews inside GitBook are always allowed.
*/
export function isPublishedPreviewAllowed(input: {
publicChangeRequestPreviews?: boolean;
preview: boolean;
changeRequest?: string;
revision?: string;
}): boolean {
return (
input.preview ||
(!input.changeRequest && !input.revision) ||
input.publicChangeRequestPreviews !== false
);
}
/**
* Get the preview request site identifier from the request URL.
*/
@@ -143,30 +143,6 @@ describe('resolveStringContentRef', () => {
});
describe('resolveContentRef', () => {
it('does not resolve category or missing assignments as tag links', async () => {
const normal = { slug: 'constructor', label: 'Constructor', color: 'default' };
const category = {
slug: 'products',
label: 'Products',
color: 'default',
kind: 'category',
};
const context = {
space: { id: 'local' },
revision: { tags: [normal, category] },
linker: createLinker({
host: 'docs.example.com',
spaceBasePath: '/',
siteBasePath: '/',
}),
} as unknown as GitBookAnyContext;
expect(await resolveContentRef({ kind: 'tag', tag: 'products' }, context)).toBeNull();
expect(await resolveContentRef({ kind: 'tag', tag: 'missing' }, context)).toBeNull();
expect(
await resolveContentRef({ kind: 'tag', tag: 'constructor', space: 'local' }, context)
).toMatchObject({ text: 'Constructor' });
});
it("resolves a page in a different space using that space's page data, not the current context page", async () => {
const sharedPageId = 'page-shared';
+1 -1
View File
@@ -10,7 +10,7 @@ export async function withVerifySignature<T>(
request: Request,
fn: (body: T) => Promise<NextResponse>
) {
// Fail closed when no secret is configured.
// Fail closed when no secret is configured, consistent with force-revalidate.
if (!GITBOOK_SECRET) {
return NextResponse.json({ error: 'Revalidation is disabled' }, { status: 403 });
}
@@ -1,188 +0,0 @@
import { describe, expect, it } from 'bun:test';
import type {
SearchPageResult,
SearchRecordResult,
SearchSpaceResult,
SiteSpace,
SiteStructure,
} from '@gitbook/api';
import { getBaseContext } from './context';
import { getEmbeddableLinker } from './embeddable-linker';
import { createSiteLinker } from './links';
import { getSearchSpaceLookup, transformSitePageResult } from './search';
import { computePublishedSearchResults, computeSearchResults } from './search-results';
import { findSiteSpaceBy } from './sites';
function siteSpace(id: string, title: string, published: string, extra: Partial<SiteSpace> = {}) {
return {
object: 'site-space',
id: `ss_${id}`,
title,
path: '',
default: false,
draft: false,
space: { id: `space_${id}`, title, language: 'en' },
urls: { published },
...extra,
} as SiteSpace;
}
const structure = {
type: 'sections',
structure: [
{
object: 'site-section',
id: 'section_guides',
title: 'Guides',
default: true,
siteSpaces: [siteSpace('guides', 'Guides', 'https://docs.example.com/')],
},
{
object: 'site-section-group',
id: 'group_products',
title: 'Products',
icon: 'box',
children: [
{
object: 'site-section',
id: 'section_api',
title: 'API',
icon: 'code',
siteSpaces: [
siteSpace('v2', 'v2', 'https://docs.example.com/api/', { default: true }),
siteSpace('v1', 'v1', 'https://docs.example.com/api/v1/'),
siteSpace('v0', 'v0', 'https://docs.example.com/api/v0/', { hidden: true }),
],
},
],
},
],
} as unknown as SiteStructure;
function space(id: string, score: number, pages: Partial<SearchPageResult>[]): SearchSpaceResult {
return {
type: 'space',
id: `space_${id}`,
title: id,
score,
pages: pages.map((page) => ({
title: `Page ${page.id}`,
description: 'Synthetic result',
score: 10 - (page.rank ?? 0),
ancestors: [],
urls: { app: 'https://app.gitbook.com/o/org/s/space' },
...page,
})) as SearchPageResult[],
};
}
const items: (SearchSpaceResult | SearchRecordResult)[] = [
space('v2', 3, [
{
id: 'auth',
path: 'authentication',
rank: 2,
ancestors: [{ title: 'Getting started' }],
sections: [
{
id: 'tokens',
title: 'Tokens',
body: 'Create a token',
path: 'authentication#tokens',
score: 4,
urls: { app: 'https://app.gitbook.com/o/org/s/space' },
},
],
},
]),
space('guides', 2, [
{ id: 'intro', path: '', rank: 1 },
{ id: 'status', path: 'https://status.example.com/', rank: 3 },
]),
space('v1', 1, [{ id: 'legacy', path: 'legacy/authentication', rank: 4 }]),
{
type: 'record',
id: 'record_1',
title: 'Support ticket',
description: 'Reset a token',
url: 'https://support.example.com/1',
score: 2,
} as SearchRecordResult,
];
describe('computePublishedSearchResults', () => {
it.each([
['url-host', false],
['url-host', true],
['url', false],
['url', true],
] as const)('gives the route results (%s, embedded: %p)', (urlMode, asEmbeddable) => {
const context = getBaseContext({
siteURL: 'https://docs.example.com/api/',
siteURLData: {
organization: 'org_1',
site: 'site_1',
siteSection: 'section_api',
siteSpace: 'ss_v2',
space: 'space_v2',
siteBasePath: '/',
basePath: '/api/',
apiToken: 'page-token',
imagesContextId: 'images',
} as Parameters<typeof getBaseContext>[0]['siteURLData'],
urlMode,
});
const directSearch = context.directSearch!;
const embed = (linker: typeof context.linker) =>
asEmbeddable ? getEmbeddableLinker(linker) : linker;
const route = computeSearchResults(items, (spaceItem, pageItem) => {
const found = findSiteSpaceBy(structure, (s) => s.space.id === spaceItem.id);
return transformSitePageResult({
asEmbeddable,
linker: embed(context.linker),
pageItem,
spaceItem,
siteSpace: found?.siteSpace,
siteSection: found?.siteSection ?? undefined,
siteSectionGroup: found?.siteSectionGroup ?? undefined,
});
});
const browser = computePublishedSearchResults({
asEmbeddable,
linker: embed(createSiteLinker(directSearch.linker)),
searchResults: items,
spaces: getSearchSpaceLookup(structure),
});
expect(JSON.stringify(directSearch)).not.toContain('page-token');
expect(browser).toEqual(route);
expect(browser?.map((result) => result.id)).toEqual([
'space_guides/intro',
'space_v2/auth',
'space_guides/status',
'space_v1/legacy',
'record_1',
]);
});
it('leaves hidden variants out of the lookup, so their results go to the route', () => {
const spaces = getSearchSpaceLookup(structure);
expect(Object.keys(spaces).sort()).toEqual(['space_guides', 'space_v1', 'space_v2']);
expect(JSON.stringify(spaces)).not.toContain('/api/v0/');
const linker = createSiteLinker({
urlMode: 'url-host',
siteHost: 'docs.example.com',
siteBasePath: '/',
spaceBasePath: '/api/',
gitbookURL: undefined,
});
const searchResults = [...items, space('v0', 1, [{ id: 'draft', path: 'draft', rank: 5 }])];
expect(
computePublishedSearchResults({ asEmbeddable: false, linker, searchResults, spaces })
).toBeNull();
});
});
-225
View File
@@ -1,225 +0,0 @@
import type {
GitBookAPI,
SearchPageResult,
SearchRecordResult,
SearchSpaceResult,
SiteSearchScope,
} from '@gitbook/api';
import { orderSearchResultGroups } from '@/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/search/orderSearchResults';
import type {
ComputedPageResult,
ComputedSectionResult,
OrderedComputedResult,
SearchSiteContentScope,
} from '@/components/Search/search-types';
import { toEmbeddableLinkForPublishedContent } from '@/lib/embeddable-linker';
import type { GitBookLinker, SiteLinkerInput } from '@/lib/links';
import { joinPathWithBaseURL } from '@/lib/paths';
import { checkIsHttpURL } from '@/lib/urls';
export type SearchResultBreadcrumb = NonNullable<ComputedPageResult['breadcrumbs']>[number];
/**
* What a search result needs from the site structure for the space it belongs to.
*/
export type SearchSpaceEntry = {
siteSpaceId?: string;
spaceURL?: string;
breadcrumbs: SearchResultBreadcrumb[];
};
/**
* Search space entries keyed by space ID, for the first site space showing that space.
*/
export type SearchSpaceLookup = Record<string, SearchSpaceEntry>;
/**
* Everything the browser needs, besides a search pass, to query the search API of a site directly.
*/
export type DirectSearchProps = {
apiURL: string;
organizationId: string;
siteId: string;
linker: SiteLinkerInput;
spaces: SearchSpaceLookup;
/** Site route that hands out a search pass. */
passURL: string;
};
/**
* Body of the site search API request for a query in a scope.
*/
export function toSearchSiteContentBody(
query: string,
scope: SearchSiteContentScope
): Parameters<GitBookAPI['orgs']['searchSiteContent']>[2] {
return {
query,
...(scope.mode === 'current' && scope.restrictTo
? {
// `restrictTo` only exists in the newer `scope` request shape,
// and the published @gitbook/api types don't include it yet.
scope: {
mode: 'default',
currentSiteSpace: scope.siteSpaceId,
restrictTo: scope.restrictTo,
} as SiteSearchScope,
}
: scope),
};
}
/**
* Turn the search API items into ordered results, with page results computed by the caller.
*/
export function computeSearchResults(
searchResults: (SearchSpaceResult | SearchRecordResult)[],
getPageResult: (
spaceItem: SearchSpaceResult,
pageItem: SearchPageResult
) => ComputedPageResult | null
): OrderedComputedResult[] {
return orderSearchResultGroups<OrderedComputedResult>(
searchResults.map((resultItem) => {
if (resultItem.type === 'record') {
const result: OrderedComputedResult = {
type: 'record',
id: resultItem.id,
title: resultItem.title,
description: resultItem.description,
href: resultItem.url,
score: resultItem.score,
};
return { type: 'context' as const, results: [result] };
}
return {
type: 'pages' as const,
results: resultItem.pages.flatMap((pageItem) => {
const result = getPageResult(resultItem, pageItem);
return result ? [{ rank: pageItem.rank, result }] : [];
}),
};
})
);
}
/**
* Compute the results of the published content of a site, resolving spaces from a lookup.
* Returns null when a page result belongs to a space missing from the lookup.
*/
export function computePublishedSearchResults(input: {
asEmbeddable: boolean;
linker: GitBookLinker;
searchResults: (SearchSpaceResult | SearchRecordResult)[];
spaces: SearchSpaceLookup;
}): OrderedComputedResult[] | null {
const { asEmbeddable, linker, searchResults, spaces } = input;
const hasUnknownSpace = searchResults.some(
(item) => item.type === 'space' && item.pages.length > 0 && !spaces[item.id]
);
if (hasUnknownSpace) {
return null;
}
return computeSearchResults(searchResults, (spaceItem, pageItem) => {
const entry: SearchSpaceEntry | undefined = spaces[spaceItem.id];
const toHref = (pagePath: string) =>
getPublishedPageHref({ asEmbeddable, linker, pagePath, spaceURL: entry?.spaceURL });
return buildSearchPageResult({
pageItem,
spaceItem,
entry,
pageHref: toHref(pageItem.path),
getSectionHref: toHref,
});
});
}
/**
* Build the result for a page, with its best section.
*/
export function buildSearchPageResult(input: {
pageItem: SearchPageResult;
spaceItem: SearchSpaceResult;
entry: SearchSpaceEntry | undefined;
pageHref: string;
getSectionHref: (sectionPath: string) => string;
}): ComputedPageResult {
const { pageItem, spaceItem, entry, pageHref, getSectionHref } = input;
const page: ComputedPageResult = {
type: 'page',
id: `${spaceItem.id}/${pageItem.id}`,
title: pageItem.title,
description: pageItem.description,
href: pageHref,
pageId: pageItem.id,
spaceId: spaceItem.id,
siteSpaceId: entry?.siteSpaceId,
score: pageItem.score,
rank: pageItem.rank,
resultType: pageItem.resultType,
breadcrumbs: [
...(entry?.breadcrumbs ?? []),
...pageItem.ancestors.map((ancestor) => ({
label: ancestor.title,
})),
],
};
const pageSections =
pageItem.sections
?.filter((section) => section.title || section.body)
.map<ComputedSectionResult>((section) => ({
type: 'section',
id: `${page.id}/${section.id}`,
title: section.title,
href: getSectionHref(section.path),
body: section.body,
pageId: pageItem.id,
spaceId: spaceItem.id,
score: section.score,
})) ?? [];
// The API returns at most one section per page, ordered for use as the section destination preview.
const bestSection = pageSections[0];
if (bestSection) {
page.bestSection = {
href: bestSection.href,
title: bestSection.title,
body: bestSection.body,
score: bestSection.score,
};
}
return page;
}
/**
* Link to a page path of the published space at `spaceURL`.
*/
export function getPublishedPageHref(input: {
asEmbeddable: boolean;
linker: GitBookLinker;
pagePath: string;
spaceURL: string | undefined;
}) {
const { asEmbeddable, linker, pagePath, spaceURL } = input;
if (checkIsHttpURL(pagePath)) {
return pagePath;
}
if (!spaceURL) {
return linker.toPathInSpace(pagePath);
}
return asEmbeddable
? toEmbeddableLinkForPublishedContent(linker, spaceURL, pagePath)
: linker.toLinkForContent(joinPathWithBaseURL(spaceURL, pagePath));
}
+96 -74
View File
@@ -6,21 +6,16 @@ import type {
SiteSection,
SiteSectionGroup,
SiteSpace,
SiteStructure,
} from '@gitbook/api';
import type { IconName } from '@gitbook/icons';
import type { ComputedPageResult } from '@/components/Search/search-types';
import type { ComputedPageResult, ComputedSectionResult } from '@/components/Search/search-types';
import { toEmbeddableLinkForPublishedContent } from '@/lib/embeddable-linker';
import type { GitBookLinker } from '@/lib/links';
import { resolvePageId } from '@/lib/pages';
import {
type SearchResultBreadcrumb,
type SearchSpaceEntry,
type SearchSpaceLookup,
buildSearchPageResult,
getPublishedPageHref,
} from '@/lib/search-results';
import { findSiteSpaceBy, getLocalizedTitle, listAllSiteSpaces } from '@/lib/sites';
import { joinPathWithBaseURL } from '@/lib/paths';
import { getLocalizedTitle } from '@/lib/sites';
import { checkIsHttpURL } from '@/lib/urls';
export function transformSitePageResult(args: {
asEmbeddable: boolean;
@@ -42,77 +37,20 @@ export function transformSitePageResult(args: {
linker,
revisionPages,
} = args;
const entry = getSearchSpaceEntry({ siteSpace, siteSection, siteSectionGroup });
const currentLanguage = siteSpace?.space.language;
const spaceURL = siteSpace?.urls.published;
const breadcrumbs: NonNullable<ComputedPageResult['breadcrumbs']> = [];
let revisionTarget: { pages: Revision['pages']; page: RevisionPageDocument } | undefined;
if (revisionPages) {
const resolved = resolvePageId(revisionPages, pageItem.id);
if (!resolved || resolved.page.id !== pageItem.id) {
return null;
}
const revisionTarget: { pages: Revision['pages']; page: RevisionPageDocument } = {
pages: revisionPages,
page: resolved.page,
};
return buildSearchPageResult({
pageItem,
spaceItem,
entry,
pageHref: linker.toPathForPage(revisionTarget),
getSectionHref: (sectionPath) =>
linker.toPathForPage({
...revisionTarget,
anchor: getRetainableSectionAnchor(sectionPath, pageItem.path),
}),
});
revisionTarget = { pages: revisionPages, page: resolved.page };
}
const toHref = (pagePath: string) =>
getPublishedPageHref({ asEmbeddable, linker, pagePath, spaceURL: entry.spaceURL });
return buildSearchPageResult({
pageItem,
spaceItem,
entry,
pageHref: toHref(pageItem.path),
getSectionHref: toHref,
});
}
/**
* Index the visible site spaces by space, resolving each space as the search route does.
*/
export function getSearchSpaceLookup(structure: SiteStructure): SearchSpaceLookup {
const lookup: SearchSpaceLookup = {};
for (const { space } of listAllSiteSpaces(structure)) {
if (lookup[space.id]) {
continue;
}
const found = findSiteSpaceBy(structure, (siteSpace) => siteSpace.space.id === space.id);
if (found && !found.siteSpace.hidden) {
lookup[space.id] = getSearchSpaceEntry({
siteSpace: found.siteSpace,
siteSection: found.siteSection ?? undefined,
siteSectionGroup: found.siteSectionGroup ?? undefined,
});
}
}
return lookup;
}
function getSearchSpaceEntry(input: {
siteSpace?: SiteSpace;
siteSection?: SiteSection;
siteSectionGroup?: SiteSectionGroup | null;
}): SearchSpaceEntry {
const { siteSpace, siteSection, siteSectionGroup } = input;
const currentLanguage = siteSpace?.space.language;
const breadcrumbs: SearchResultBreadcrumb[] = [];
if (siteSectionGroup) {
breadcrumbs.push({
icon: siteSectionGroup.icon as IconName,
@@ -141,11 +79,74 @@ function getSearchSpaceEntry(input: {
});
}
return {
breadcrumbs.push(
...pageItem.ancestors.map((ancestor) => ({
label: ancestor.title,
}))
);
const pageHref = revisionTarget
? linker.toPathForPage(revisionTarget)
: getPublishedPageHref({ asEmbeddable, linker, pagePath: pageItem.path, spaceURL });
const page: ComputedPageResult = {
type: 'page',
id: `${spaceItem.id}/${pageItem.id}`,
title: pageItem.title,
description: pageItem.description,
href: pageHref,
pageId: pageItem.id,
spaceId: spaceItem.id,
siteSpaceId: siteSpace?.id,
spaceURL: siteSpace?.urls.published,
score: pageItem.score,
rank: pageItem.rank,
resultType: pageItem.resultType,
breadcrumbs,
};
const pageSections =
pageItem.sections
?.filter((section) => section.title || section.body)
.map<ComputedSectionResult>((section) => {
const anchor = revisionTarget
? getRetainableSectionAnchor(section.path, pageItem.path)
: undefined;
const sectionHref = revisionTarget
? linker.toPathForPage({
...revisionTarget,
anchor,
})
: getPublishedPageHref({
asEmbeddable,
linker,
pagePath: section.path,
spaceURL,
});
return {
type: 'section',
id: `${page.id}/${section.id}`,
title: section.title,
href: sectionHref,
body: section.body,
pageId: pageItem.id,
spaceId: spaceItem.id,
score: section.score,
};
}) ?? [];
// The API returns at most one section per page, ordered for use as the section destination preview.
const bestSection = pageSections[0];
if (bestSection) {
page.bestSection = {
href: bestSection.href,
title: bestSection.title,
body: bestSection.body,
score: bestSection.score,
};
}
return page;
}
function getRetainableSectionAnchor(sectionPath: string, pagePath: string): string | undefined {
@@ -156,3 +157,24 @@ function getRetainableSectionAnchor(sectionPath: string, pagePath: string): stri
return sectionPath.slice(hashIndex + 1) || undefined;
}
function getPublishedPageHref(input: {
asEmbeddable: boolean;
linker: GitBookLinker;
pagePath: string;
spaceURL: string | undefined;
}) {
const { asEmbeddable, linker, pagePath, spaceURL } = input;
if (checkIsHttpURL(pagePath)) {
return pagePath;
}
if (!spaceURL) {
return linker.toPathInSpace(pagePath);
}
return asEmbeddable
? toEmbeddableLinkForPublishedContent(linker, spaceURL, pagePath)
: linker.toLinkForContent(joinPathWithBaseURL(spaceURL, pagePath));
}
+2 -4
View File
@@ -8,12 +8,10 @@ export function getRevisionTags(revision: Revision | undefined): RevisionTag[] {
}
/**
* Resolve an assignment to a normal tag, ignoring categories and missing definitions.
* Resolve a tag slug to its full RevisionTag.
*/
export function resolveTag(slug: string, revisionTags: RevisionTag[]): RevisionTag | undefined {
return revisionTags.find(
(tag) => tag.slug === slug && !('kind' in tag && tag.kind === 'category')
);
return revisionTags.find((t) => t.slug === slug);
}
/**
+3 -43
View File
@@ -1,13 +1,7 @@
import { describe, expect, it } from 'bun:test';
import {
DocumentTextColor,
type JSONDocument,
type Revision,
type RevisionTag,
} from '@gitbook/api';
import type { JSONDocument, Revision } from '@gitbook/api';
import { getRevisionTags, resolveBlockTags, resolveTag } from './tags';
import {
UPDATES_TAG_FILTER_CAP,
getDocumentFilterableTags,
@@ -15,40 +9,6 @@ import {
} from './updates';
describe('getDocumentFilterableTags', () => {
it('ignores category and deleted references without removing their stored assignments', () => {
const document = createUpdatesDocument([['category', 'constructor', 'missing']]);
const category = { ...createTag('category', 'Products'), kind: 'category' };
const normal = createTag('constructor', 'Constructor');
const revision = { tags: [category, normal] } as unknown as Revision;
const before = JSON.stringify(document);
expect(getDocumentFilterableTags(document, revision).map((tag) => tag.slug)).toEqual([
'constructor',
]);
expect(resolveTag('category', revision.tags)).toBeUndefined();
expect(resolveTag('missing', revision.tags)).toBeUndefined();
expect(resolveTag('constructor', revision.tags)).toEqual(normal);
expect(getRevisionTags(revision)).toEqual([category, normal]);
expect(JSON.stringify(document)).toBe(before);
});
it('rejects category assignments even when definitions are passed directly to the resolver', () => {
const normal = createTag('beta', 'Beta');
const tags = [
{ ...createTag('products', 'Products'), kind: 'category' },
normal,
] satisfies (RevisionTag & { kind?: 'category' })[];
expect(
resolveBlockTags(
[
{ kind: 'tag', tag: 'products' },
{ kind: 'tag', tag: 'beta' },
{ kind: 'tag', tag: 'missing' },
],
tags
)
).toEqual([normal]);
});
it('returns unique update tags in document order', () => {
const document = createUpdatesDocument([
['improvements', 'fixes'],
@@ -117,11 +77,11 @@ function createUpdatesDocument(entryTags: string[][]): JSONDocument {
};
}
function createTag(slug: string, label: string): RevisionTag {
function createTag(slug: string, label: string) {
return {
slug,
label,
color: DocumentTextColor.Default,
color: 'default',
icon: undefined,
};
}
-67
View File
@@ -7,7 +7,6 @@ import rison from 'rison';
import { getDomain } from 'tldts';
import {
ContentType,
CustomizationDefaultThemeMode,
CustomizationThemeMode,
type PublishedSiteContent,
@@ -386,10 +385,6 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
return await serveSearchPrewarm(request, siteURLData);
}
if (isSearchPassRequest(siteURLData)) {
return await serveSearchPass(request, siteURLData, visitorToken?.token);
}
//
// Render and serve the content
//
@@ -837,68 +832,6 @@ async function serveSearchPrewarm(request: NextRequest, siteURLData: PublishedSi
return new NextResponse(null, { status: 204 });
}
const SEARCH_PASS_TIMEOUT_MS = 1_500;
function isSearchPassRequest(siteURLData: PublishedSiteContent) {
return removeLeadingSlash(removeTrailingSlash(siteURLData.pathname)) === '~gitbook/search-pass';
}
/**
* Give the visitor's browser a short-lived pass to search the site with the API directly.
*/
async function serveSearchPass(
request: NextRequest,
siteURLData: PublishedSiteContent,
visitorToken: string | undefined
) {
if (request.method !== 'POST') {
return new NextResponse(null, { status: 405, headers: { allow: 'POST' } });
}
const api = apiClient({ apiToken: siteURLData.apiToken ?? null });
// The published API client has no method for this operation yet.
const pass = api
.request({
path: `/orgs/${siteURLData.organization}/sites/${siteURLData.site}/search/pass`,
method: 'POST',
secure: true,
// The API caps the pass at the visitor token's expiry.
...(visitorToken ? { body: { visitorToken }, type: ContentType.Json } : {}),
})
.then(
(response) =>
response.json() as Promise<{
searchPass?: { token?: unknown; expiresAt?: unknown };
}>
)
.catch(() => null);
let timeout: ReturnType<typeof setTimeout> | undefined;
const reply = await Promise.race([
pass,
new Promise<null>((resolve) => {
timeout = setTimeout(() => resolve(null), SEARCH_PASS_TIMEOUT_MS);
}),
]);
clearTimeout(timeout);
const { token, expiresAt } = reply?.searchPass ?? {};
// The browser adds this lifetime to its own clock, so its clock skew does not matter.
const expiresIn =
typeof expiresAt === 'string' ? Date.parse(expiresAt) - Date.now() : Number.NaN;
if (typeof token !== 'string' || !token || !(expiresIn > 0)) {
return new NextResponse(null, {
status: 204,
headers: { 'cache-control': 'private, no-store' },
});
}
return NextResponse.json(
{ token, expiresIn },
{ headers: { 'cache-control': 'private, no-store' } }
);
}
/**
* Encode path in a site content.
* Special paths are not encoded and passed to be handled by the route handlers.
@@ -0,0 +1,49 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import crypto from 'node:crypto';
interface JsonBody {
// The paths need to be the rewritten one, `res.revalidate` call don't go through the middleware
paths: string[];
}
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
// Only allow POST requests
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const signatureHeader = req.headers['x-gitbook-signature'] as string | undefined;
if (!signatureHeader) {
return res.status(400).json({ error: 'Missing signature header' });
}
// We cannot use env from `@/v2/lib/env` here as it make it crash because of the import "server-only" in the file.
if (process.env.GITBOOK_SECRET) {
try {
const computedSignature = crypto
.createHmac('sha256', process.env.GITBOOK_SECRET)
.update(JSON.stringify(req.body))
.digest('hex');
if (computedSignature === signatureHeader) {
const results = await Promise.allSettled(
(req.body as JsonBody).paths.map((path) => {
// oxlint-disable-next-line no-console
console.log(`Revalidating path: ${path}`);
return res.revalidate(path);
})
);
return res.status(200).json({
success: results.every((result) => result.status === 'fulfilled'),
errors: results
.filter((result) => result.status === 'rejected')
.map((result) => (result as PromiseRejectedResult).reason),
});
}
return res.status(401).json({ error: 'Invalid signature' });
} catch (error) {
console.error('Error during revalidation:', error);
return res.status(400).json({ error: 'Invalid request or unable to parse JSON' });
}
}
// If no secret is set, we do not allow revalidation
return res.status(403).json({ error: 'Revalidation is disabled' });
}
@@ -24,7 +24,6 @@ function createContext(options: {
spaceRedirectPageId?: string;
}) {
return {
displayAgentInstructions: false,
organizationId: 'org-1',
site: { id: 'site-1' },
siteSpace: { id: 'site-space-1' },
@@ -37,9 +36,6 @@ function createContext(options: {
spaceBasePath: '/docs/',
}),
dataFetcher: {
getRevisionPageMarkdown: async () => ({
data: '# New page\n\n[Read this page](/pages/page-1)\n',
}),
getSiteRedirectBySource: async ({ source }: { source: string }) =>
options.siteRedirect && source === '/old-page'
? {
@@ -58,14 +54,6 @@ function createContext(options: {
}
describe('servePageMarkdown', () => {
it('serves page links that can be opened without the source origin', async () => {
const response = await servePageMarkdown(createContext({}), 'new-page');
expect(response.status).toBe(200);
expect(await response.text()).toContain(
'[Read this page](https://docs.example.com/docs/new-page.md)'
);
});
it('redirects to the markdown version of a site redirect target', async () => {
const context = createContext({
siteRedirect: { target: 'https://docs.example.com/docs/new-page', permanent: true },
+6 -1
View File
@@ -1,4 +1,9 @@
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"regions": ["cle1"]
"regions": ["cle1"],
"functions": {
"src/app/sites/dynamic/**/~gitbook/search/route.ts": {
"supportsCancellation": true
}
}
}
-2
View File
@@ -53,8 +53,6 @@ async function main() {
await Promise.all([
writeDataFile('styles-map', JSON.stringify(onlyStyles, null, 2)),
writeDataFile('icons', JSON.stringify(result, null, 2)),
// Names only, so validating an icon name doesn't load every label and search term.
writeDataFile('icon-names', JSON.stringify(result.map((entry) => entry.icon))),
]);
// oxlint-disable-next-line no-console
-5
View File
@@ -12,11 +12,6 @@
"development": "./src/icons.ts",
"default": "./dist/icons.js"
},
"./validateIconName": {
"types": "./dist/validateIconName.d.ts",
"development": "./src/validateIconName.ts",
"default": "./dist/validateIconName.js"
},
"./types": {
"types": "./dist/types.d.ts",
"development": "./src/types.ts",
+12 -1
View File
@@ -18,4 +18,15 @@ export interface IconDefinition {
// @ts-ignore
export const icons: IconDefinition[] = rawIcons;
export { validateIconName } from './validateIconName';
let iconNamesSet: Set<string> | null = null;
/**
* Validate that the icon name is valid.
*/
export function validateIconName(icon: IconName | string): icon is IconName {
if (!iconNamesSet) {
iconNamesSet = new Set(icons.map((icon) => icon.icon));
}
return iconNamesSet.has(icon);
}
-15
View File
@@ -1,15 +0,0 @@
import iconNames from './data/icon-names.json' with { type: 'json' };
import type { IconName } from './types';
let iconNamesSet: Set<string> | null = null;
/**
* Validate that the icon name is valid.
*/
export function validateIconName(icon: IconName | string): icon is IconName {
if (!iconNamesSet) {
iconNamesSet = new Set(iconNames);
}
return iconNamesSet.has(icon);
}
-1
View File
@@ -5,7 +5,6 @@ export default defineConfig([
entry: [
'src/index.ts',
'src/icons.ts',
'src/validateIconName.ts',
'src/types.ts',
'src/getIconStyle.ts',
'src/IconSources.ts',
@@ -36,14 +36,11 @@ export function OpenAPIPathMultipleServers(
};
});
// The store outlives client-side navigation, so it can hold a server from another spec.
const selectedServer = items.find((item) => item.key === key)?.key ?? defaultServer;
return (
<OpenAPIPathItem
copyType="button"
{...props}
value={`${withServer ? selectedServer : ''}${path}`}
value={`${withServer ? key : ''}${path}`}
context={context}
>
{withServer ? (