From be080248b177dfd32186ccf483379d8fe1645796 Mon Sep 17 00:00:00 2001 From: conico974 Date: Mon, 20 Apr 2026 16:22:34 +0200 Subject: [PATCH] Block search indexation in deployment configuration (#4203) --- .github/composite/deploy-cloudflare/action.yaml | 1 + .github/composite/deploy-vercel/action.yaml | 1 + packages/gitbook/next.config.mjs | 1 + .../gitbook/src/components/SiteLayout/SiteLayout.tsx | 2 +- .../gitbook/src/components/SitePage/SitePage.tsx | 2 +- packages/gitbook/src/lib/context.ts | 12 ++++++++++++ packages/gitbook/src/lib/seo.ts | 10 ++-------- packages/gitbook/src/middleware.ts | 5 +++++ packages/gitbook/src/routes/robots.ts | 2 +- 9 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.github/composite/deploy-cloudflare/action.yaml b/.github/composite/deploy-cloudflare/action.yaml index bfd258b17..8ade51001 100644 --- a/.github/composite/deploy-cloudflare/action.yaml +++ b/.github/composite/deploy-cloudflare/action.yaml @@ -64,6 +64,7 @@ runs: run: bun run turbo build:cloudflare env: GITBOOK_RUNTIME: cloudflare + GITBOOK_BLOCK_SEARCH_INDEXATION: ${{ inputs.environment == 'preview' && 'true' || '' }} shell: bash - name: Upload the DO worker diff --git a/.github/composite/deploy-vercel/action.yaml b/.github/composite/deploy-vercel/action.yaml index 7e695d801..163a847fb 100644 --- a/.github/composite/deploy-vercel/action.yaml +++ b/.github/composite/deploy-vercel/action.yaml @@ -70,6 +70,7 @@ runs: echo "resolved HEAD_SHA: $HEAD_SHA" echo "GITBOOK_HEAD_SHA=$HEAD_SHA" >> .vercel/.env.${{ inputs.environment }}.local echo "GITBOOK_RUNTIME=vercel" >> .vercel/.env.${{ inputs.environment }}.local + echo "GITBOOK_BLOCK_SEARCH_INDEXATION=true" >> .vercel/.env.${{ inputs.environment }}.local echo "--- .vercel/.env.${{ inputs.environment }}.local after inject ---" cat .vercel/.env.${{ inputs.environment }}.local - name: Build Project Artifacts diff --git a/packages/gitbook/next.config.mjs b/packages/gitbook/next.config.mjs index d920920b6..8ac936ce1 100644 --- a/packages/gitbook/next.config.mjs +++ b/packages/gitbook/next.config.mjs @@ -59,6 +59,7 @@ const nextConfig = { GITBOOK_IMAGE_RESIZE_MODE: process.env.GITBOOK_IMAGE_RESIZE_MODE, GITBOOK_FONTS_URL: process.env.GITBOOK_FONTS_URL, GITBOOK_RUNTIME: process.env.GITBOOK_RUNTIME, + GITBOOK_BLOCK_SEARCH_INDEXATION: process.env.GITBOOK_BLOCK_SEARCH_INDEXATION, // Next.js envs NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY, diff --git a/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx b/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx index e0fc6b4b8..723dcba29 100644 --- a/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx +++ b/packages/gitbook/src/components/SiteLayout/SiteLayout.tsx @@ -193,6 +193,6 @@ export async function generateSiteLayoutMetadata(context: GitBookSiteContext): P ? 'black' : 'default', }, - robots: (await isSiteIndexable(context)) ? 'index, follow' : 'noindex, nofollow', + robots: isSiteIndexable(context) ? 'index, follow' : 'noindex, nofollow', }; } diff --git a/packages/gitbook/src/components/SitePage/SitePage.tsx b/packages/gitbook/src/components/SitePage/SitePage.tsx index 89301ccf2..2284b1fce 100644 --- a/packages/gitbook/src/components/SitePage/SitePage.tsx +++ b/packages/gitbook/src/components/SitePage/SitePage.tsx @@ -228,7 +228,7 @@ export async function generateSitePageMetadata(props: SitePageProps): Promise { const { dataFetcher } = baseContext; @@ -371,6 +382,7 @@ export async function fetchSiteContextByIds( scripts, contextId: ids.contextId, isFallback: ids.isFallback, + noIndexSearch: ids.noIndexSearch, }; } diff --git a/packages/gitbook/src/lib/seo.ts b/packages/gitbook/src/lib/seo.ts index b1aa30dca..cafc49c53 100644 --- a/packages/gitbook/src/lib/seo.ts +++ b/packages/gitbook/src/lib/seo.ts @@ -1,6 +1,5 @@ import type { GitBookSiteContext } from '@/lib/context'; import { type RevisionPageDocument, type RevisionPageGroup, SiteVisibility } from '@gitbook/api'; -import { headers } from 'next/headers'; /** * Return true if a page is indexable in search. @@ -24,13 +23,8 @@ export function isPageIndexable( /** * Return true if a space should be indexed by search engines. */ -export async function isSiteIndexable(context: GitBookSiteContext) { - const headersList = await headers(); - - if ( - process.env.GITBOOK_BLOCK_SEARCH_INDEXATION && - !headersList.has('x-gitbook-search-indexation') - ) { +export function isSiteIndexable(context: GitBookSiteContext) { + if (context.noIndexSearch) { return false; } diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index a09a999dd..071dfab76 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -369,6 +369,11 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { imagesContextId: imagesContextId, contextId: siteURLData.contextId, isFallback: requestURL.searchParams.get('fallback') === 'true' ? true : undefined, + noIndexSearch: + Boolean(process.env.GITBOOK_BLOCK_SEARCH_INDEXATION) && + !requestURL.searchParams.has('x-gitbook-search-indexation') + ? true + : undefined, }; const requestHeaders = new Headers(request.headers); diff --git a/packages/gitbook/src/routes/robots.ts b/packages/gitbook/src/routes/robots.ts index c93f63b86..87898c62c 100644 --- a/packages/gitbook/src/routes/robots.ts +++ b/packages/gitbook/src/routes/robots.ts @@ -8,7 +8,7 @@ export async function serveRobotsTxt(context: GitBookSiteContext) { const { linker } = context; const isRoot = checkIsRootSiteContext(context); - const isIndexable = await isSiteIndexable(context); + const isIndexable = isSiteIndexable(context); const sitemapPath = linker.toPathInSpace(isRoot ? '/sitemap.xml' : '/sitemap-pages.xml'); const sitemapUrl = linker.toAbsoluteURL(sitemapPath);