Skip to content

Commit be08024

Browse files
authored
Block search indexation in deployment configuration (#4203)
1 parent d3d5150 commit be08024

9 files changed

Lines changed: 25 additions & 11 deletions

File tree

.github/composite/deploy-cloudflare/action.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ runs:
6464
run: bun run turbo build:cloudflare
6565
env:
6666
GITBOOK_RUNTIME: cloudflare
67+
GITBOOK_BLOCK_SEARCH_INDEXATION: ${{ inputs.environment == 'preview' && 'true' || '' }}
6768
shell: bash
6869

6970
- name: Upload the DO worker

.github/composite/deploy-vercel/action.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ runs:
7070
echo "resolved HEAD_SHA: $HEAD_SHA"
7171
echo "GITBOOK_HEAD_SHA=$HEAD_SHA" >> .vercel/.env.${{ inputs.environment }}.local
7272
echo "GITBOOK_RUNTIME=vercel" >> .vercel/.env.${{ inputs.environment }}.local
73+
echo "GITBOOK_BLOCK_SEARCH_INDEXATION=true" >> .vercel/.env.${{ inputs.environment }}.local
7374
echo "--- .vercel/.env.${{ inputs.environment }}.local after inject ---"
7475
cat .vercel/.env.${{ inputs.environment }}.local
7576
- name: Build Project Artifacts

packages/gitbook/next.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const nextConfig = {
5959
GITBOOK_IMAGE_RESIZE_MODE: process.env.GITBOOK_IMAGE_RESIZE_MODE,
6060
GITBOOK_FONTS_URL: process.env.GITBOOK_FONTS_URL,
6161
GITBOOK_RUNTIME: process.env.GITBOOK_RUNTIME,
62+
GITBOOK_BLOCK_SEARCH_INDEXATION: process.env.GITBOOK_BLOCK_SEARCH_INDEXATION,
6263

6364
// Next.js envs
6465
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY,

packages/gitbook/src/components/SiteLayout/SiteLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,6 @@ export async function generateSiteLayoutMetadata(context: GitBookSiteContext): P
193193
? 'black'
194194
: 'default',
195195
},
196-
robots: (await isSiteIndexable(context)) ? 'index, follow' : 'noindex, nofollow',
196+
robots: isSiteIndexable(context) ? 'index, follow' : 'noindex, nofollow',
197197
};
198198
}

packages/gitbook/src/components/SitePage/SitePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export async function generateSitePageMetadata(props: SitePageProps): Promise<Me
228228
],
229229
},
230230
robots:
231-
(await isSiteIndexable(context)) && isPageIndexable(ancestors, page)
231+
isSiteIndexable(context) && isPageIndexable(ancestors, page)
232232
? 'index, follow'
233233
: 'noindex, nofollow',
234234
};

packages/gitbook/src/lib/context.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export type SiteURLData = Pick<
5959
* By knowing it's a fallback, we can redirect to the space base path instead of returning a 404.
6060
*/
6161
isFallback?: boolean;
62+
63+
/**
64+
* Whether search indexation is blocked for this deployment.
65+
* Computed in middleware from GITBOOK_BLOCK_SEARCH_INDEXATION env var and x-gitbook-search-indexation header.
66+
*/
67+
noIndexSearch?: boolean;
6268
};
6369

6470
/**
@@ -150,6 +156,9 @@ export type GitBookSiteContext = GitBookSpaceContext & {
150156

151157
/** Whether this request is a fallback rendering. */
152158
isFallback: boolean;
159+
160+
/** Whether search indexation is blocked for this deployment. */
161+
noIndexSearch: boolean;
153162
};
154163

155164
/**
@@ -229,6 +238,7 @@ export async function fetchSiteContextByURLLookup(
229238
revision: data.revision,
230239
contextId: data.contextId,
231240
isFallback: data.isFallback ?? false,
241+
noIndexSearch: data.noIndexSearch ?? false,
232242
});
233243
}
234244

@@ -248,6 +258,7 @@ export async function fetchSiteContextByIds(
248258
revision: string | undefined;
249259
contextId?: string;
250260
isFallback: boolean;
261+
noIndexSearch: boolean;
251262
}
252263
): Promise<GitBookSiteContext> {
253264
const { dataFetcher } = baseContext;
@@ -371,6 +382,7 @@ export async function fetchSiteContextByIds(
371382
scripts,
372383
contextId: ids.contextId,
373384
isFallback: ids.isFallback,
385+
noIndexSearch: ids.noIndexSearch,
374386
};
375387
}
376388

packages/gitbook/src/lib/seo.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { GitBookSiteContext } from '@/lib/context';
22
import { type RevisionPageDocument, type RevisionPageGroup, SiteVisibility } from '@gitbook/api';
3-
import { headers } from 'next/headers';
43

54
/**
65
* Return true if a page is indexable in search.
@@ -24,13 +23,8 @@ export function isPageIndexable(
2423
/**
2524
* Return true if a space should be indexed by search engines.
2625
*/
27-
export async function isSiteIndexable(context: GitBookSiteContext) {
28-
const headersList = await headers();
29-
30-
if (
31-
process.env.GITBOOK_BLOCK_SEARCH_INDEXATION &&
32-
!headersList.has('x-gitbook-search-indexation')
33-
) {
26+
export function isSiteIndexable(context: GitBookSiteContext) {
27+
if (context.noIndexSearch) {
3428
return false;
3529
}
3630

packages/gitbook/src/middleware.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
369369
imagesContextId: imagesContextId,
370370
contextId: siteURLData.contextId,
371371
isFallback: requestURL.searchParams.get('fallback') === 'true' ? true : undefined,
372+
noIndexSearch:
373+
Boolean(process.env.GITBOOK_BLOCK_SEARCH_INDEXATION) &&
374+
!requestURL.searchParams.has('x-gitbook-search-indexation')
375+
? true
376+
: undefined,
372377
};
373378

374379
const requestHeaders = new Headers(request.headers);

packages/gitbook/src/routes/robots.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function serveRobotsTxt(context: GitBookSiteContext) {
88
const { linker } = context;
99

1010
const isRoot = checkIsRootSiteContext(context);
11-
const isIndexable = await isSiteIndexable(context);
11+
const isIndexable = isSiteIndexable(context);
1212

1313
const sitemapPath = linker.toPathInSpace(isRoot ? '/sitemap.xml' : '/sitemap-pages.xml');
1414
const sitemapUrl = linker.toAbsoluteURL(sitemapPath);

0 commit comments

Comments
 (0)