mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Store the build version in the generator meta tag (#104)
* Store the build version in the generator meta tag * Format * Send header with version * Add header on 404 as well
This commit is contained in:
+1
-1
@@ -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),
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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()),
|
||||
|
||||
+2
-1
@@ -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})`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Get a string representing the build version.
|
||||
*/
|
||||
export function buildVersion(): string {
|
||||
return process.env.BUILD_VERSION || '0.0.0';
|
||||
}
|
||||
+7
-1
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user