Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2: search and ask AI #2889

Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pass token to insights
  • Loading branch information
SamyPesse committed Mar 2, 2025
commit 7b8b76cbd767180b7e9396093e867d60e031bf5c
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import {
} from '@/components/SiteLayout';
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
import { GITBOOK_DISABLE_TRACKING } from '@v2/lib/env';
import { getThemeFromMiddleware } from '@v2/lib/middleware';
import { getThemeFromMiddleware, getVisitorAuthTokenFromMiddleware } from '@v2/lib/middleware';

interface SiteDynamicLayoutProps {
params: Promise<RouteLayoutParams>;
@@ -18,13 +18,15 @@ export default async function SiteDynamicLayout({
}: React.PropsWithChildren<SiteDynamicLayoutProps>) {
const context = await getDynamicSiteContext(await params);
const forcedTheme = await getThemeFromMiddleware();
const visitorAuthToken = await getVisitorAuthTokenFromMiddleware();

return (
<CustomizationRootLayout customization={context.customization}>
<SiteLayout
context={context}
forcedTheme={forcedTheme}
withTracking={!GITBOOK_DISABLE_TRACKING}
visitorAuthToken={visitorAuthToken}
>
{children}
</SiteLayout>
Original file line number Diff line number Diff line change
@@ -25,7 +25,11 @@ export default async function SiteStaticLayout({

return (
<CustomizationRootLayout customization={context.customization}>
<SiteLayout context={context} withTracking={!GITBOOK_DISABLE_TRACKING}>
<SiteLayout
context={context}
withTracking={!GITBOOK_DISABLE_TRACKING}
visitorAuthToken={null}
>
{children}
</SiteLayout>
</CustomizationRootLayout>
19 changes: 19 additions & 0 deletions packages/gitbook-v2/src/lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -36,6 +36,11 @@ export enum MiddlewareHeaders {
* Token to use for the API.
*/
APIToken = 'x-gitbook-token',

/**
* The visitor token used to access this content
*/
VisitorAuthToken = 'x-gitbook-visitor-token',
}

/**
@@ -98,3 +103,17 @@ export async function getThemeFromMiddleware() {
? CustomizationThemeMode.Light
: CustomizationThemeMode.Dark;
}

/**
* Get the visitor auth token from the middleware headers.
* This function should only be called in a dynamic route.
*/
export async function getVisitorAuthTokenFromMiddleware(): Promise<string | null> {
const headersList = await headers();
const visitorAuthToken = headersList.get(MiddlewareHeaders.VisitorAuthToken);
if (!visitorAuthToken) {
return null;
}

return visitorAuthToken;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getThemeFromMiddleware } from '@v2/lib/middleware';
import { getThemeFromMiddleware, getVisitorAuthTokenFromMiddleware } from '@v2/lib/middleware';
import type { Metadata, Viewport } from 'next';
import type React from 'react';

@@ -22,12 +22,14 @@ export default async function ContentLayout(props: { children: React.ReactNode }

const context = await fetchLayoutData();
const queryStringTheme = await getThemeFromMiddleware();
const visitorAuthToken = await getVisitorAuthTokenFromMiddleware();

return (
<SiteLayout
context={context}
forcedTheme={queryStringTheme}
withTracking={await shouldTrackEvents()}
visitorAuthToken={visitorAuthToken}
>
{children}
</SiteLayout>
9 changes: 7 additions & 2 deletions packages/gitbook/src/components/SiteLayout/SiteLayout.tsx
Original file line number Diff line number Diff line change
@@ -24,9 +24,10 @@ export async function SiteLayout(props: {
context: GitBookSiteContext;
forcedTheme?: CustomizationThemeMode | null;
withTracking: boolean;
visitorAuthToken: string | null;
children: React.ReactNode;
}) {
const { context, nonce, forcedTheme, withTracking, children } = props;
const { context, nonce, forcedTheme, withTracking, visitorAuthToken, children } = props;

const { scripts, customization } = context;

@@ -51,7 +52,11 @@ export async function SiteLayout(props: {
(customization.themes.toggeable ? undefined : customization.themes.default)
}
>
<SpaceLayout context={context} withTracking={withTracking}>
<SpaceLayout
context={context}
withTracking={withTracking}
visitorAuthToken={visitorAuthToken}
>
{children}
</SpaceLayout>

7 changes: 5 additions & 2 deletions packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx
Original file line number Diff line number Diff line change
@@ -24,10 +24,13 @@ export function SpaceLayout(props: {
/** Whether to enable tracking of events into site insights. */
withTracking: boolean;

/** The visitor token used to access this content */
visitorAuthToken: string | null;

/** The children of the layout. */
children: React.ReactNode;
}) {
const { context, withTracking, children } = props;
const { context, withTracking, visitorAuthToken, children } = props;
const { siteSpace, customization, sections, siteSpaces } = context;

const withTopHeader = customization.header.preset !== CustomizationHeaderPreset.None;
@@ -45,7 +48,7 @@ export function SpaceLayout(props: {
<InsightsProvider
enabled={withTracking}
apiHost={context.dataFetcher.apiEndpoint}
visitorAuthToken={context.visitorAuthToken}
visitorAuthToken={visitorAuthToken}
organizationId={context.organizationId}
siteId={context.site.id}
siteSectionId={context.sections?.current?.id ?? null}
4 changes: 4 additions & 0 deletions packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -207,6 +207,10 @@ export async function middleware(request: NextRequest) {
headers.set(MiddlewareHeaders.SiteURLData, JSON.stringify(resolved));
}

if (resolved.visitorToken) {
headers.set(MiddlewareHeaders.VisitorAuthToken, resolved.visitorToken);
}

// For tests, we make it possible to enable search indexation
// using a query parameter.
const xGitBookSearchIndexation =