diff --git a/next.config.js b/next.config.js index 78d1da062..36b84a4f1 100644 --- a/next.config.js +++ b/next.config.js @@ -5,7 +5,7 @@ const nextConfig = { ignoreBuildErrors: true, }, env: { - BUILD_ID: (process.env.GITHUB_SHA ?? '').slice(0, 7), + BUILD_VERSION: (process.env.GITHUB_SHA ?? '').slice(0, 7), }, }; diff --git a/src/app/[spaceId]/(content)/layout.tsx b/src/app/[spaceId]/(content)/layout.tsx index 17630f225..e09df154c 100644 --- a/src/app/[spaceId]/(content)/layout.tsx +++ b/src/app/[spaceId]/(content)/layout.tsx @@ -6,6 +6,7 @@ import React from 'react'; import { AdminToolbar } from '@/components/AdminToolbar'; import { CookiesToast } from '@/components/Cookies'; import { SpaceLayout } from '@/components/SpaceLayout'; +import { buildVersion } from '@/lib/build'; import { getContentSecurityPolicyNonce } from '@/lib/csp'; import { absoluteHref, baseUrl } from '@/lib/links'; import { shouldIndexSpace } from '@/lib/seo'; @@ -83,7 +84,7 @@ export async function generateMetadata({ params }: { params: SpaceParams }): Pro return { title: `${space.title}`, - generator: 'GitBook', + generator: `GitBook (${buildVersion()})`, // We pass `metadataBase` to avoid warnings from Next, but we still use absolute URLs // as metadataBase doesn't seem to work well on next-on-cloudflare. metadataBase: new URL(baseUrl()), diff --git a/src/lib/api.ts b/src/lib/api.ts index 8833a2002..e6818bb76 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -10,6 +10,7 @@ import { import assertNever from 'assert-never'; import { headers } from 'next/headers'; +import { buildVersion } from './build'; import { cache, cacheResponse, noCacheFetchOptions, parseCacheResponse } from './cache'; export interface ContentPointer { @@ -579,7 +580,7 @@ export function userAgent(): string { return process.env.GITBOOK_USER_AGENT; } - let result = `GitBook-Open/${process.env.BUILD_ID}`; + let result = `GitBook-Open/${buildVersion()}`; if (process.env.GITBOOK_USER_AGENT_COMMENT) { result += ` (${process.env.GITBOOK_USER_AGENT_COMMENT})`; } diff --git a/src/lib/build.ts b/src/lib/build.ts new file mode 100644 index 000000000..73dbcc4a5 --- /dev/null +++ b/src/lib/build.ts @@ -0,0 +1,6 @@ +/** + * Get a string representing the build version. + */ +export function buildVersion(): string { + return process.env.BUILD_VERSION || '0.0.0'; +} diff --git a/src/middleware.ts b/src/middleware.ts index d0713af32..423826744 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -13,6 +13,7 @@ import { } from '@/lib/api'; import { createContentSecurityPolicyNonce, getContentSecurityPolicy } from '@/lib/csp'; +import { buildVersion } from './lib/build'; import { getURLLookupAlternatives } from './lib/middleware'; export const config = { @@ -84,8 +85,11 @@ export async function middleware(request: NextRequest) { () => lookupSpaceForURL(mode, inputURL, visitorAuthToken), ); if (!resolved) { - return new NextResponse(`Not found`, { + return new NextResponse(`Content not found`, { status: 404, + headers: { + 'x-gitbook-version': buildVersion(), + }, }); } @@ -149,6 +153,8 @@ export async function middleware(request: NextRequest) { }, }); + response.headers.set('x-gitbook-version', buildVersion()); + // Add Content Security Policy header response.headers.set('content-security-policy', csp);