diff --git a/.changeset/icons-validate-icon-name-entry.md b/.changeset/icons-validate-icon-name-entry.md new file mode 100644 index 000000000..bcc919d40 --- /dev/null +++ b/.changeset/icons-validate-icon-name-entry.md @@ -0,0 +1,5 @@ +--- +"@gitbook/icons": minor +--- + +Add a `@gitbook/icons/validateIconName` entry that validates icon names without loading the icon labels and search terms. diff --git a/.changeset/lighter-opennext-workers.md b/.changeset/lighter-opennext-workers.md new file mode 100644 index 000000000..e77b87d24 --- /dev/null +++ b/.changeset/lighter-opennext-workers.md @@ -0,0 +1,5 @@ +--- +"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. diff --git a/packages/gitbook/next.config.mjs b/packages/gitbook/next.config.mjs index ce1c0113b..7b8fd7524 100644 --- a/packages/gitbook/next.config.mjs +++ b/packages/gitbook/next.config.mjs @@ -1,6 +1,7 @@ // @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 @@ -119,6 +120,17 @@ 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 [ { diff --git a/packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc b/packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc index 9e08e264d..63dea1b92 100644 --- a/packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc +++ b/packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc @@ -3,6 +3,7 @@ "name": "gitbook-open-v2", "compatibility_date": "2026-04-02", "keep_names": false, + "minify": true, "compatibility_flags": [ "nodejs_compat", "allow_importable_env", diff --git a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts index 97c234b0c..c3645c08e 100644 --- a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts +++ b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts @@ -50,6 +50,7 @@ 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, } ); diff --git a/packages/gitbook/src/app/utils.ts b/packages/gitbook/src/app/utils.ts index 727105cd7..2655e28ba 100644 --- a/packages/gitbook/src/app/utils.ts +++ b/packages/gitbook/src/app/utils.ts @@ -1,5 +1,6 @@ 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'; @@ -27,7 +28,17 @@ export type RouteParams = RouteLayoutParams & { /** * Get the static context when rendering statically a site. */ -export async function getStaticSiteContext(params: RouteLayoutParams) { +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) { const siteURL = getSiteURLFromParams(params); const siteURLData = getSiteURLDataFromParams(params); @@ -57,7 +68,15 @@ export async function getStaticSiteContext(params: RouteLayoutParams) { * Get the site context when rendering dynamically. * The context will depend on the request. */ -export async function getDynamicSiteContext(params: RouteLayoutParams) { +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) { const siteURL = getSiteURLFromParams(params); const siteURLData = getSiteURLDataFromParams(params); diff --git a/packages/gitbook/src/components/DocumentView/Hint.tsx b/packages/gitbook/src/components/DocumentView/Hint.tsx index 4e043272a..e54dbb70d 100644 --- a/packages/gitbook/src/components/DocumentView/Hint.tsx +++ b/packages/gitbook/src/components/DocumentView/Hint.tsx @@ -1,6 +1,6 @@ import type { DocumentBlockHint } from '@gitbook/api'; import { Icon, type IconName } from '@gitbook/icons'; -import { validateIconName } from '@gitbook/icons/icons'; +import { validateIconName } from '@gitbook/icons/validateIconName'; import { Block, type BlockProps } from './Block'; import { Blocks } from './Blocks'; diff --git a/packages/gitbook/src/components/DocumentView/Prompt/Prompt.tsx b/packages/gitbook/src/components/DocumentView/Prompt/Prompt.tsx index 27610eb34..7a6fc3a8d 100644 --- a/packages/gitbook/src/components/DocumentView/Prompt/Prompt.tsx +++ b/packages/gitbook/src/components/DocumentView/Prompt/Prompt.tsx @@ -4,7 +4,7 @@ import { type DocumentBlockPrompt, type SiteCustomizationSettings, } from '@gitbook/api'; -import { validateIconName } from '@gitbook/icons/icons'; +import { validateIconName } from '@gitbook/icons/validateIconName'; import type { BlockProps } from '../Block'; import { CodeBlock } from '../CodeBlock'; diff --git a/packages/gitbook/src/components/DocumentView/Tabs/Tabs.tsx b/packages/gitbook/src/components/DocumentView/Tabs/Tabs.tsx index 76daf3a30..4c85f0a4a 100644 --- a/packages/gitbook/src/components/DocumentView/Tabs/Tabs.tsx +++ b/packages/gitbook/src/components/DocumentView/Tabs/Tabs.tsx @@ -1,6 +1,6 @@ import type { DocumentBlockTabs } from '@gitbook/api'; import type { IconName } from '@gitbook/icons'; -import { validateIconName } from '@gitbook/icons/icons'; +import { validateIconName } from '@gitbook/icons/validateIconName'; import type { BlockProps } from '../Block'; import { Blocks } from '../Blocks'; diff --git a/packages/gitbook/src/lib/icons/inline.ts b/packages/gitbook/src/lib/icons/inline.ts index c4f925e47..b424dd8b4 100644 --- a/packages/gitbook/src/lib/icons/inline.ts +++ b/packages/gitbook/src/lib/icons/inline.ts @@ -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'; diff --git a/packages/gitbook/src/lib/mcp/redis-stub.ts b/packages/gitbook/src/lib/mcp/redis-stub.ts new file mode 100644 index 000000000..a510648dc --- /dev/null +++ b/packages/gitbook/src/lib/mcp/redis-stub.ts @@ -0,0 +1,7 @@ +/** + * 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'); +} diff --git a/packages/gitbook/src/lib/openapi/enrich.test.ts b/packages/gitbook/src/lib/openapi/enrich.test.ts index c0524de7d..171f7563f 100644 --- a/packages/gitbook/src/lib/openapi/enrich.test.ts +++ b/packages/gitbook/src/lib/openapi/enrich.test.ts @@ -17,4 +17,22 @@ describe('#enrichFilesystem', () => { '
Social platform
Hello
constructor
'); + expect(paths['/b'].get['x-gitbook-description-html']).toBe('constructor
'); + }); }); diff --git a/packages/gitbook/src/lib/openapi/enrich.ts b/packages/gitbook/src/lib/openapi/enrich.ts index 570fc6ce2..527230938 100644 --- a/packages/gitbook/src/lib/openapi/enrich.ts +++ b/packages/gitbook/src/lib/openapi/enrich.ts @@ -30,14 +30,17 @@ export async function enrichFilesystem(filesystem: Filesystem) { /** * Create a markdown parser that caches the results of parsing. */ -const createMarkdownParser = () => (input: string) => { - const cache: Record