mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Cleanup headers usage (#2717)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'gitbook': patch
|
||||
---
|
||||
|
||||
Fix multiple bugs due to headers read in an anarchic way in the app.
|
||||
@@ -1,5 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import {
|
||||
CURRENT_SIGNATURE_VERSION,
|
||||
isSignatureVersion,
|
||||
@@ -39,7 +40,8 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
// Verify the signature
|
||||
const verified = await verifyImageSignature(url, { signature, version: signatureVersion });
|
||||
const ctx = getGitBookContextFromHeaders(request.headers);
|
||||
const verified = await verifyImageSignature(ctx, url, { signature, version: signatureVersion });
|
||||
if (!verified) {
|
||||
return new Response(`Invalid signature "${signature ?? ''}" for "${url}"`, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { TrackPageViewEvent } from '@/components/Insights';
|
||||
import { getSpaceLanguage, t } from '@/intl/server';
|
||||
import { getSiteData, getSpaceContentData } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getSiteContentPointer } from '@/lib/pointer';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
export default async function NotFound() {
|
||||
const pointer = await getSiteContentPointer();
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const pointer = getSiteContentPointer(ctx);
|
||||
const [{ space }, { customization }] = await Promise.all([
|
||||
getSpaceContentData(pointer, pointer.siteShareKey),
|
||||
getSiteData(pointer),
|
||||
getSpaceContentData(ctx, pointer, pointer.siteShareKey),
|
||||
getSiteData(ctx, pointer),
|
||||
]);
|
||||
|
||||
const language = getSpaceLanguage(customization);
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { CustomizationHeaderPreset, CustomizationThemeMode } from '@gitbook/api';
|
||||
import { Metadata, Viewport } from 'next';
|
||||
import { headers } from 'next/headers';
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
import React from 'react';
|
||||
|
||||
import { PageAside } from '@/components/PageAside';
|
||||
import { PageBody, PageCover } from '@/components/PageBody';
|
||||
import { getGitBookContextFromHeaders, GitBookContext } from '@/lib/gitbook-context';
|
||||
import { PageHrefContext, getAbsoluteHref, getPageHref } from '@/lib/links';
|
||||
import { getPagePath, resolveFirstDocument } from '@/lib/pages';
|
||||
import { ContentRefContext } from '@/lib/references';
|
||||
@@ -16,17 +18,21 @@ import { PagePathParams, fetchPageData, getPathnameParam, normalizePathname } fr
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
type Props = {
|
||||
params: Promise<PagePathParams>;
|
||||
searchParams: Promise<{ fallback?: string }>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch and render a page.
|
||||
*/
|
||||
export default async function Page(props: {
|
||||
params: Promise<PagePathParams>;
|
||||
searchParams: Promise<{ fallback?: string }>;
|
||||
}) {
|
||||
const { params: rawParams, searchParams: rawSearchParams } = props;
|
||||
|
||||
const params = await rawParams;
|
||||
const searchParams = await rawSearchParams;
|
||||
export default async function Page(props: Props) {
|
||||
const [headersList, params, searchParams] = await Promise.all([
|
||||
headers(),
|
||||
props.params,
|
||||
props.searchParams,
|
||||
]);
|
||||
const ctx = getGitBookContextFromHeaders(headersList);
|
||||
|
||||
const {
|
||||
content: contentPointer,
|
||||
@@ -39,7 +45,7 @@ export default async function Page(props: {
|
||||
page,
|
||||
ancestors,
|
||||
document,
|
||||
} = await getPageDataWithFallback({
|
||||
} = await getPageDataWithFallback(ctx, {
|
||||
pagePathParams: params,
|
||||
searchParams,
|
||||
redirectOnFallback: true,
|
||||
@@ -52,12 +58,12 @@ export default async function Page(props: {
|
||||
if (pathname !== rawPathname) {
|
||||
// If the pathname was not normalized, redirect to the normalized version
|
||||
// before trying to resolve the page again
|
||||
redirect(await getAbsoluteHref(pathname));
|
||||
redirect(getAbsoluteHref(ctx, pathname));
|
||||
} else {
|
||||
notFound();
|
||||
}
|
||||
} else if (getPagePath(pages, page) !== rawPathname) {
|
||||
redirect(await getPageHref(pages, page, linksContext));
|
||||
redirect(getPageHref(ctx, pages, page, linksContext));
|
||||
}
|
||||
|
||||
const withTopHeader = customization.header.preset !== CustomizationHeaderPreset.None;
|
||||
@@ -121,12 +127,10 @@ export default async function Page(props: {
|
||||
);
|
||||
}
|
||||
|
||||
export async function generateViewport({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<PagePathParams>;
|
||||
}): Promise<Viewport> {
|
||||
const { customization } = await fetchPageData(await params);
|
||||
export async function generateViewport(props: Props): Promise<Viewport> {
|
||||
const [params, headersList] = await Promise.all([props.params, headers()]);
|
||||
const ctx = getGitBookContextFromHeaders(headersList);
|
||||
const { customization } = await fetchPageData(ctx, params);
|
||||
return {
|
||||
colorScheme: customization.themes.toggeable
|
||||
? customization.themes.default === CustomizationThemeMode.Dark
|
||||
@@ -136,17 +140,20 @@ export async function generateViewport({
|
||||
};
|
||||
}
|
||||
|
||||
export async function generateMetadata({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<PagePathParams>;
|
||||
searchParams: Promise<{ fallback?: string }>;
|
||||
}): Promise<Metadata> {
|
||||
const { space, pages, page, customization, site, ancestors } = await getPageDataWithFallback({
|
||||
pagePathParams: await params,
|
||||
searchParams: await searchParams,
|
||||
});
|
||||
export async function generateMetadata(props: Props): Promise<Metadata> {
|
||||
const [params, searchParams, headersList] = await Promise.all([
|
||||
props.params,
|
||||
props.searchParams,
|
||||
headers(),
|
||||
]);
|
||||
const ctx = getGitBookContextFromHeaders(headersList);
|
||||
const { space, pages, page, customization, site, ancestors } = await getPageDataWithFallback(
|
||||
ctx,
|
||||
{
|
||||
pagePathParams: params,
|
||||
searchParams: searchParams,
|
||||
},
|
||||
);
|
||||
|
||||
if (!page) {
|
||||
notFound();
|
||||
@@ -159,17 +166,16 @@ export async function generateMetadata({
|
||||
description: page.description ?? '',
|
||||
alternates: {
|
||||
// Trim trailing slashes in canonical URL to match the redirect behavior
|
||||
canonical: (await getAbsoluteHref(getPagePath(pages, page), true)).replace(/\/+$/, ''),
|
||||
canonical: getAbsoluteHref(ctx, getPagePath(pages, page), true).replace(/\/+$/, ''),
|
||||
},
|
||||
openGraph: {
|
||||
images: [
|
||||
customization.socialPreview.url ??
|
||||
(await getAbsoluteHref(`~gitbook/ogimage/${page.id}`, true)),
|
||||
getAbsoluteHref(ctx, `~gitbook/ogimage/${page.id}`, true),
|
||||
],
|
||||
},
|
||||
robots:
|
||||
(await isSpaceIndexable({ space, site: site ?? null })) &&
|
||||
isPageIndexable(ancestors, page)
|
||||
isSpaceIndexable(ctx, { space, site: site ?? null }) && isPageIndexable(ancestors, page)
|
||||
? 'index, follow'
|
||||
: 'noindex, nofollow',
|
||||
};
|
||||
@@ -178,14 +184,17 @@ export async function generateMetadata({
|
||||
/**
|
||||
* Fetches the page data matching the requested pathname and fallback to root page when page is not found.
|
||||
*/
|
||||
async function getPageDataWithFallback(args: {
|
||||
pagePathParams: PagePathParams;
|
||||
searchParams: { fallback?: string };
|
||||
redirectOnFallback?: boolean;
|
||||
}) {
|
||||
async function getPageDataWithFallback(
|
||||
ctx: GitBookContext,
|
||||
args: {
|
||||
pagePathParams: PagePathParams;
|
||||
searchParams: { fallback?: string };
|
||||
redirectOnFallback?: boolean;
|
||||
},
|
||||
) {
|
||||
const { pagePathParams, searchParams, redirectOnFallback = false } = args;
|
||||
|
||||
const { pages, page: targetPage, ...otherPageData } = await fetchPageData(pagePathParams);
|
||||
const { pages, page: targetPage, ...otherPageData } = await fetchPageData(ctx, pagePathParams);
|
||||
|
||||
let page = targetPage;
|
||||
const canFallback = !!searchParams.fallback;
|
||||
@@ -193,7 +202,7 @@ async function getPageDataWithFallback(args: {
|
||||
const rootPage = resolveFirstDocument(pages, []);
|
||||
|
||||
if (redirectOnFallback && rootPage?.page) {
|
||||
redirect(await getPageHref(pages, rootPage?.page));
|
||||
redirect(getPageHref(ctx, pages, rootPage?.page));
|
||||
}
|
||||
|
||||
page = rootPage?.page;
|
||||
|
||||
@@ -13,6 +13,7 @@ import { api } from '@/lib/api';
|
||||
import { assetsDomain } from '@/lib/assets';
|
||||
import { buildVersion } from '@/lib/build';
|
||||
import { getContentSecurityPolicyNonce } from '@/lib/csp';
|
||||
import { GitBookContext, getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref, getBaseUrl } from '@/lib/links';
|
||||
import { isSpaceIndexable } from '@/lib/seo';
|
||||
import { getContentTitle } from '@/lib/utils';
|
||||
@@ -27,9 +28,10 @@ export const runtime = 'edge';
|
||||
* Layout when rendering the content.
|
||||
*/
|
||||
export default async function ContentLayout(props: { children: React.ReactNode }) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { children } = props;
|
||||
|
||||
const nonce = await getContentSecurityPolicyNonce();
|
||||
const nonce = getContentSecurityPolicyNonce(ctx);
|
||||
const {
|
||||
content,
|
||||
space,
|
||||
@@ -41,10 +43,9 @@ export default async function ContentLayout(props: { children: React.ReactNode }
|
||||
ancestors,
|
||||
scripts,
|
||||
sections,
|
||||
} = await fetchContentData();
|
||||
} = await fetchContentData(ctx);
|
||||
|
||||
const apiCtx = await api();
|
||||
ReactDOM.preconnect(apiCtx.client.endpoint);
|
||||
ReactDOM.preconnect(api(ctx).client.endpoint);
|
||||
if (assetsDomain) {
|
||||
ReactDOM.preconnect(assetsDomain);
|
||||
}
|
||||
@@ -56,7 +57,7 @@ export default async function ContentLayout(props: { children: React.ReactNode }
|
||||
});
|
||||
});
|
||||
|
||||
const queryStringTheme = await getQueryStringTheme();
|
||||
const queryStringTheme = getQueryStringTheme(ctx);
|
||||
|
||||
return (
|
||||
<NuqsAdapter>
|
||||
@@ -105,7 +106,8 @@ export default async function ContentLayout(props: { children: React.ReactNode }
|
||||
}
|
||||
|
||||
export async function generateViewport(): Promise<Viewport> {
|
||||
const { customization } = await fetchContentData();
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { customization } = await fetchContentData(ctx);
|
||||
return {
|
||||
colorScheme: customization.themes.toggeable
|
||||
? customization.themes.default === CustomizationThemeMode.Dark
|
||||
@@ -116,46 +118,43 @@ export async function generateViewport(): Promise<Viewport> {
|
||||
}
|
||||
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
const { space, site, customization } = await fetchContentData();
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { space, site, customization } = await fetchContentData(ctx);
|
||||
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
|
||||
|
||||
return {
|
||||
title: getContentTitle(space, customization, site),
|
||||
generator: `GitBook (${buildVersion()})`,
|
||||
metadataBase: new URL(await getBaseUrl()),
|
||||
metadataBase: new URL(getBaseUrl(ctx)),
|
||||
icons: {
|
||||
icon: [
|
||||
{
|
||||
url:
|
||||
customIcon?.light ??
|
||||
(await getAbsoluteHref('~gitbook/icon?size=small&theme=light', true)),
|
||||
getAbsoluteHref(ctx, '~gitbook/icon?size=small&theme=light', true),
|
||||
type: 'image/png',
|
||||
media: '(prefers-color-scheme: light)',
|
||||
},
|
||||
{
|
||||
url:
|
||||
customIcon?.dark ??
|
||||
(await getAbsoluteHref('~gitbook/icon?size=small&theme=dark', true)),
|
||||
getAbsoluteHref(ctx, '~gitbook/icon?size=small&theme=dark', true),
|
||||
type: 'image/png',
|
||||
media: '(prefers-color-scheme: dark)',
|
||||
},
|
||||
],
|
||||
},
|
||||
robots: (await isSpaceIndexable({ space, site })) ? 'index, follow' : 'noindex, nofollow',
|
||||
robots: isSpaceIndexable(ctx, { space, site }) ? 'index, follow' : 'noindex, nofollow',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* For preview, the theme can be set via query string (?theme=light).
|
||||
*/
|
||||
async function getQueryStringTheme() {
|
||||
const headersList = await headers();
|
||||
const queryStringTheme = headersList.get('x-gitbook-theme');
|
||||
if (!queryStringTheme) {
|
||||
function getQueryStringTheme(ctx: GitBookContext) {
|
||||
if (!ctx.theme) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return queryStringTheme === 'light'
|
||||
? CustomizationThemeMode.Light
|
||||
: CustomizationThemeMode.Dark;
|
||||
return ctx.theme === 'light' ? CustomizationThemeMode.Light : CustomizationThemeMode.Dark;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
import { getSpace, getSite } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref } from '@/lib/links';
|
||||
import { getSiteContentPointer } from '@/lib/pointer';
|
||||
import { isSpaceIndexable } from '@/lib/seo';
|
||||
@@ -11,17 +12,18 @@ export const runtime = 'edge';
|
||||
* Generate a robots.txt for the current space.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
const pointer = await getSiteContentPointer();
|
||||
const ctx = getGitBookContextFromHeaders(req.headers);
|
||||
const pointer = getSiteContentPointer(ctx);
|
||||
const [site, space] = await Promise.all([
|
||||
getSite(pointer.organizationId, pointer.siteId),
|
||||
getSpace(pointer.spaceId, pointer.siteShareKey),
|
||||
getSite(ctx, pointer.organizationId, pointer.siteId),
|
||||
getSpace(ctx, pointer.spaceId, pointer.siteShareKey),
|
||||
]);
|
||||
|
||||
const lines = [
|
||||
`User-agent: *`,
|
||||
'Disallow: /~gitbook/',
|
||||
...((await isSpaceIndexable({ space, site }))
|
||||
? [`Allow: /`, `Sitemap: ${await getAbsoluteHref(`/sitemap.xml`, true)}`]
|
||||
...(isSpaceIndexable(ctx, { space, site })
|
||||
? [`Allow: /`, `Sitemap: ${getAbsoluteHref(ctx, `/sitemap.xml`, true)}`]
|
||||
: [`Disallow: /`]),
|
||||
];
|
||||
const content = lines.join('\n');
|
||||
|
||||
@@ -3,6 +3,7 @@ import jsontoxml from 'jsontoxml';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
import { getSpaceContentData } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref } from '@/lib/links';
|
||||
import { getPagePath } from '@/lib/pages';
|
||||
import { getSiteContentPointer } from '@/lib/pointer';
|
||||
@@ -14,33 +15,32 @@ export const runtime = 'edge';
|
||||
* Generate a sitemap.xml for the current space.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
const pointer = await getSiteContentPointer();
|
||||
const { pages: rootPages } = await getSpaceContentData(pointer, pointer.siteShareKey);
|
||||
const ctx = getGitBookContextFromHeaders(req.headers);
|
||||
const pointer = getSiteContentPointer(ctx);
|
||||
const { pages: rootPages } = await getSpaceContentData(ctx, pointer, pointer.siteShareKey);
|
||||
|
||||
const pages = flattenPages(rootPages, (page) => !page.hidden && isPageIndexable([], page));
|
||||
const urls = await Promise.all(
|
||||
pages.map(async ({ page, depth }) => {
|
||||
// Decay priority with depth
|
||||
const priority = Math.pow(2, -0.25 * depth);
|
||||
// Normalize to keep 2 decimals
|
||||
const normalizedPriority = Math.floor(100 * priority) / 100;
|
||||
const urls = pages.map(({ page, depth }) => {
|
||||
// Decay priority with depth
|
||||
const priority = Math.pow(2, -0.25 * depth);
|
||||
// Normalize to keep 2 decimals
|
||||
const normalizedPriority = Math.floor(100 * priority) / 100;
|
||||
|
||||
const lastModified = page.updatedAt || page.createdAt;
|
||||
const lastModified = page.updatedAt || page.createdAt;
|
||||
|
||||
return {
|
||||
url: {
|
||||
loc: await getAbsoluteHref(getPagePath(rootPages, page), true),
|
||||
priority: normalizedPriority,
|
||||
...(lastModified
|
||||
? {
|
||||
// lastmod format is YYYY-MM-DD
|
||||
lastmod: new Date(lastModified).toISOString().split('T')[0],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
};
|
||||
}),
|
||||
);
|
||||
return {
|
||||
url: {
|
||||
loc: getAbsoluteHref(ctx, getPagePath(rootPages, page), true),
|
||||
priority: normalizedPriority,
|
||||
...(lastModified
|
||||
? {
|
||||
// lastmod format is YYYY-MM-DD
|
||||
lastmod: new Date(lastModified).toISOString().split('T')[0],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const xml = jsontoxml(
|
||||
[
|
||||
|
||||
@@ -5,6 +5,7 @@ import React from 'react';
|
||||
|
||||
import { getSite, getSiteData, getSpace } from '@/lib/api';
|
||||
import { getEmojiForCode } from '@/lib/emojis';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getSiteContentPointer } from '@/lib/pointer';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
import { getContentTitle } from '@/lib/utils';
|
||||
@@ -32,17 +33,18 @@ const SIZES = {
|
||||
* Render an icon for the space.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
const ctx = getGitBookContextFromHeaders(req.headers);
|
||||
const options = getOptions(req.url);
|
||||
const size = SIZES[options.size];
|
||||
|
||||
const pointer = await getSiteContentPointer();
|
||||
const pointer = getSiteContentPointer(ctx);
|
||||
const spaceId = pointer.spaceId;
|
||||
|
||||
const [space, { customization }] = await Promise.all([
|
||||
getSpace(spaceId, pointer.siteShareKey),
|
||||
getSiteData(pointer),
|
||||
getSpace(ctx, spaceId, pointer.siteShareKey),
|
||||
getSiteData(ctx, pointer),
|
||||
]);
|
||||
const site = await getSite(pointer.organizationId, pointer.siteId);
|
||||
const site = await getSite(ctx, pointer.organizationId, pointer.siteId);
|
||||
const contentTitle = getContentTitle(space, customization, site);
|
||||
|
||||
return new ImageResponse(
|
||||
|
||||
@@ -5,6 +5,7 @@ import { NextRequest } from 'next/server';
|
||||
import colorContrast from 'postcss-color-contrast/js';
|
||||
import React from 'react';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref } from '@/lib/links';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
import { getContentTitle } from '@/lib/utils';
|
||||
@@ -17,7 +18,8 @@ export const runtime = 'edge';
|
||||
* Render the OpenGraph image for a space.
|
||||
*/
|
||||
export async function GET(req: NextRequest, { params }: { params: Promise<PageIdParams> }) {
|
||||
const { space, page, customization, site } = await fetchPageData(await params);
|
||||
const ctx = getGitBookContextFromHeaders(req.headers);
|
||||
const { space, page, customization, site } = await fetchPageData(ctx, await params);
|
||||
|
||||
if (customization.socialPreview.url) {
|
||||
// If user configured a custom social preview, we redirect to it.
|
||||
@@ -51,10 +53,8 @@ export async function GET(req: NextRequest, { params }: { params: Promise<PageId
|
||||
body: baseColors[useLightTheme ? 'dark' : 'light'], // Invert text on background
|
||||
};
|
||||
|
||||
const [gridWhite, gridBlack] = await Promise.all([
|
||||
getAbsoluteHref('~gitbook/static/images/ogimage-grid-white.png', true),
|
||||
getAbsoluteHref('~gitbook/static/images/ogimage-grid-black.png', true),
|
||||
]);
|
||||
const gridWhite = getAbsoluteHref(ctx, '~gitbook/static/images/ogimage-grid-white.png', true);
|
||||
const gridBlack = getAbsoluteHref(ctx, '~gitbook/static/images/ogimage-grid-black.png', true);
|
||||
|
||||
let gridAsset = useLightTheme ? gridBlack : gridWhite;
|
||||
|
||||
@@ -92,7 +92,7 @@ export async function GET(req: NextRequest, { params }: { params: Promise<PageId
|
||||
break;
|
||||
}
|
||||
|
||||
const favicon = await (async () => {
|
||||
const favicon = (() => {
|
||||
if ('icon' in customization.favicon)
|
||||
return (
|
||||
<img
|
||||
@@ -109,7 +109,8 @@ export async function GET(req: NextRequest, { params }: { params: Promise<PageId
|
||||
{String.fromCodePoint(parseInt('0x' + customization.favicon.emoji))}
|
||||
</span>
|
||||
);
|
||||
const src = await getAbsoluteHref(
|
||||
const src = getAbsoluteHref(
|
||||
ctx,
|
||||
`~gitbook/icon?size=medium&theme=${customization.themes.default}`,
|
||||
true,
|
||||
);
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
getSiteData,
|
||||
getSiteRedirectBySource,
|
||||
} from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { resolvePagePath, resolvePageId } from '@/lib/pages';
|
||||
import { getSiteContentPointer } from '@/lib/pointer';
|
||||
|
||||
@@ -22,13 +23,13 @@ export interface PageIdParams {
|
||||
/**
|
||||
* Fetch all the data needed to render the content layout.
|
||||
*/
|
||||
export async function fetchContentData() {
|
||||
const content = await getSiteContentPointer();
|
||||
export async function fetchContentData(ctx: GitBookContext) {
|
||||
const content = getSiteContentPointer(ctx);
|
||||
|
||||
const [{ space, contentTarget, pages }, { customization, site, sections, spaces, scripts }] =
|
||||
await Promise.all([
|
||||
getSpaceContentData(content, content.siteShareKey),
|
||||
getSiteData(content),
|
||||
getSpaceContentData(ctx, content, content.siteShareKey),
|
||||
getSiteData(ctx, content),
|
||||
]);
|
||||
|
||||
// we grab the space attached to the parent as it contains overriden customizations
|
||||
@@ -53,10 +54,10 @@ export async function fetchContentData() {
|
||||
* Fetch all the data needed to render the content.
|
||||
* Optimized to fetch in parallel as much as possible.
|
||||
*/
|
||||
export async function fetchPageData(params: PagePathParams | PageIdParams) {
|
||||
const contentData = await fetchContentData();
|
||||
export async function fetchPageData(ctx: GitBookContext, params: PagePathParams | PageIdParams) {
|
||||
const contentData = await fetchContentData(ctx);
|
||||
|
||||
const page = await resolvePage({
|
||||
const page = await resolvePage(ctx, {
|
||||
organizationId: contentData.space.organization,
|
||||
siteId: contentData.site.id,
|
||||
spaceId: contentData.contentTarget.spaceId,
|
||||
@@ -66,7 +67,7 @@ export async function fetchPageData(params: PagePathParams | PageIdParams) {
|
||||
params,
|
||||
});
|
||||
const document = page?.page.documentId
|
||||
? await getDocument(contentData.space.id, page.page.documentId)
|
||||
? await getDocument(ctx, contentData.space.id, page.page.documentId)
|
||||
: null;
|
||||
|
||||
return {
|
||||
@@ -80,15 +81,18 @@ export async function fetchPageData(params: PagePathParams | PageIdParams) {
|
||||
* Resolve a page from the params.
|
||||
* If the path can't be found, we try to resolve it from the API to handle redirects.
|
||||
*/
|
||||
async function resolvePage(input: {
|
||||
organizationId: string;
|
||||
siteId: string;
|
||||
spaceId: string;
|
||||
revisionId: string;
|
||||
shareKey: string | undefined;
|
||||
pages: RevisionPage[];
|
||||
params: PagePathParams | PageIdParams;
|
||||
}) {
|
||||
async function resolvePage(
|
||||
ctx: GitBookContext,
|
||||
input: {
|
||||
organizationId: string;
|
||||
siteId: string;
|
||||
spaceId: string;
|
||||
revisionId: string;
|
||||
shareKey: string | undefined;
|
||||
pages: RevisionPage[];
|
||||
params: PagePathParams | PageIdParams;
|
||||
},
|
||||
) {
|
||||
const { organizationId, siteId, spaceId, revisionId, pages, shareKey, params } = input;
|
||||
|
||||
if ('pageId' in params) {
|
||||
@@ -109,13 +113,13 @@ async function resolvePage(input: {
|
||||
// If page can't be found, we try with the API, in case we have a redirect at space level.
|
||||
// We use the raw pathname to handle special/malformed redirects setup by users in the GitSync.
|
||||
// The page rendering will take care of redirecting to a normalized pathname.
|
||||
const resolved = await getRevisionPageByPath(spaceId, revisionId, rawPathname);
|
||||
const resolved = await getRevisionPageByPath(ctx, spaceId, revisionId, rawPathname);
|
||||
if (resolved) {
|
||||
return resolvePageId(pages, resolved.id);
|
||||
}
|
||||
|
||||
// If a page still can't be found, we try with the API, in case we have a redirect at site level.
|
||||
const resolvedSiteRedirect = await getSiteRedirectBySource({
|
||||
const resolvedSiteRedirect = await getSiteRedirectBySource(ctx, {
|
||||
organizationId,
|
||||
siteId,
|
||||
source: rawPathname.startsWith('/') ? rawPathname : `/${rawPathname}`,
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { CustomizationRootLayout } from '@/components/RootLayout';
|
||||
import { getSiteData } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getSiteContentPointer } from '@/lib/pointer';
|
||||
|
||||
/**
|
||||
@@ -8,9 +11,10 @@ import { getSiteContentPointer } from '@/lib/pointer';
|
||||
*/
|
||||
export default async function SiteRootLayout(props: { children: React.ReactNode }) {
|
||||
const { children } = props;
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
|
||||
const pointer = await getSiteContentPointer();
|
||||
const { customization } = await getSiteData(pointer);
|
||||
const pointer = getSiteContentPointer(ctx);
|
||||
const { customization } = await getSiteData(ctx, pointer);
|
||||
|
||||
return (
|
||||
<CustomizationRootLayout customization={customization}>{children}</CustomizationRootLayout>
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { SpaceIntegrationScript } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { CustomizationRootLayout } from '@/components/RootLayout';
|
||||
import { getSiteData, getSpaceCustomization } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders, GitBookContext } from '@/lib/gitbook-context';
|
||||
|
||||
import { getSiteOrSpacePointerForPDF } from './pointer';
|
||||
|
||||
@@ -10,12 +12,13 @@ import { getSiteOrSpacePointerForPDF } from './pointer';
|
||||
* site or space and initializes the CustomizationRootLayout with it.
|
||||
*/
|
||||
export default async function PDFRootLayout(props: { children: React.ReactNode }) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { children } = props;
|
||||
|
||||
const pointer = await getSiteOrSpacePointerForPDF();
|
||||
const pointer = getSiteOrSpacePointerForPDF(ctx);
|
||||
const { customization } = await ('siteId' in pointer
|
||||
? getSiteData(pointer)
|
||||
: getSpaceLayoutData());
|
||||
? getSiteData(ctx, pointer)
|
||||
: getSpaceLayoutData(ctx));
|
||||
|
||||
return (
|
||||
<CustomizationRootLayout customization={customization}>{children}</CustomizationRootLayout>
|
||||
@@ -25,14 +28,11 @@ export default async function PDFRootLayout(props: { children: React.ReactNode }
|
||||
/**
|
||||
* Fetch all the layout data about a space at once.
|
||||
*/
|
||||
async function getSpaceLayoutData() {
|
||||
const [{ customization }, scripts] = await Promise.all([
|
||||
getSpaceCustomization(),
|
||||
[] as SpaceIntegrationScript[],
|
||||
]);
|
||||
async function getSpaceLayoutData(ctx: GitBookContext) {
|
||||
const { customization } = await getSpaceCustomization(ctx);
|
||||
|
||||
return {
|
||||
customization,
|
||||
scripts,
|
||||
scripts: [] as SpaceIntegrationScript[],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from '@gitbook/api';
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import { Metadata } from 'next';
|
||||
import { headers } from 'next/headers';
|
||||
import { notFound } from 'next/navigation';
|
||||
import * as React from 'react';
|
||||
|
||||
@@ -23,6 +24,7 @@ import {
|
||||
getSpaceContentData,
|
||||
getSiteData,
|
||||
} from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getPagePDFContainerId, PageHrefContext, getAbsoluteHref } from '@/lib/links';
|
||||
import { resolvePageId } from '@/lib/pages';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
@@ -39,10 +41,11 @@ const DEFAULT_LIMIT = 100;
|
||||
export const runtime = 'edge';
|
||||
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
const pointer = await getSiteOrSpacePointerForPDF();
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const pointer = getSiteOrSpacePointerForPDF(ctx);
|
||||
const [space, { customization }] = await Promise.all([
|
||||
getSpace(pointer.spaceId, 'siteId' in pointer ? pointer.siteShareKey : undefined),
|
||||
'siteId' in pointer ? getSiteData(pointer) : getSpaceCustomization(),
|
||||
getSpace(ctx, pointer.spaceId, 'siteId' in pointer ? pointer.siteShareKey : undefined),
|
||||
'siteId' in pointer ? getSiteData(ctx, pointer) : getSpaceCustomization(ctx),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -57,19 +60,20 @@ export async function generateMetadata(): Promise<Metadata> {
|
||||
export default async function PDFHTMLOutput(props: {
|
||||
searchParams: Promise<{ [key: string]: string }>;
|
||||
}) {
|
||||
const pointer = await getSiteOrSpacePointerForPDF();
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const pointer = getSiteOrSpacePointerForPDF(ctx);
|
||||
|
||||
const searchParams = new URLSearchParams(await props.searchParams);
|
||||
const pdfParams = getPDFSearchParams(new URLSearchParams(searchParams));
|
||||
|
||||
// Build current PDF URL and preserve all search params
|
||||
let currentPDFUrl = await getAbsoluteHref('~gitbook/pdf', true);
|
||||
let currentPDFUrl = getAbsoluteHref(ctx, '~gitbook/pdf', true);
|
||||
currentPDFUrl += '?' + searchParams.toString();
|
||||
|
||||
// Load the content,
|
||||
const [{ customization }, { space, contentTarget, pages: rootPages }] = await Promise.all([
|
||||
'siteId' in pointer ? getSiteData(pointer) : getSpaceCustomization(),
|
||||
getSpaceContentData(pointer, 'siteId' in pointer ? pointer.siteShareKey : undefined),
|
||||
'siteId' in pointer ? getSiteData(ctx, pointer) : getSpaceCustomization(ctx),
|
||||
getSpaceContentData(ctx, pointer, 'siteId' in pointer ? pointer.siteShareKey : undefined),
|
||||
]);
|
||||
const language = getSpaceLanguage(customization);
|
||||
|
||||
@@ -88,7 +92,7 @@ export default async function PDFHTMLOutput(props: {
|
||||
<div className={tcls('fixed', 'left-12', 'top-12', 'print:hidden', 'z-50')}>
|
||||
<a
|
||||
title={tString(language, 'pdf_goback')}
|
||||
href={pdfParams.back ?? (await getAbsoluteHref(''))}
|
||||
href={pdfParams.back ?? getAbsoluteHref(ctx, '')}
|
||||
className={tcls(
|
||||
'flex',
|
||||
'flex-row',
|
||||
@@ -226,9 +230,10 @@ async function PDFPageDocument(props: {
|
||||
page: RevisionPageDocument;
|
||||
refContext: ContentRefContext;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { space, page, refContext } = props;
|
||||
|
||||
const document = page.documentId ? await getDocument(space.id, page.documentId) : null;
|
||||
const document = page.documentId ? await getDocument(ctx, space.id, page.documentId) : null;
|
||||
|
||||
return (
|
||||
<PrintPage id={getPagePDFContainerId(page)}>
|
||||
@@ -249,7 +254,7 @@ async function PDFPageDocument(props: {
|
||||
revisionId: refContext.revisionId,
|
||||
},
|
||||
contentRefContext: refContext,
|
||||
resolveContentRef: (ref) => resolveContentRef(ref, refContext),
|
||||
resolveContentRef: (ref) => resolveContentRef(ctx, ref, refContext),
|
||||
getId: (id) => getPagePDFContainerId(page, id),
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { SiteContentPointer, SpaceContentPointer } from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { getSiteContentPointer, getSpacePointer } from '@/lib/pointer';
|
||||
|
||||
/**
|
||||
@@ -8,12 +9,12 @@ import { getSiteContentPointer, getSpacePointer } from '@/lib/pointer';
|
||||
*
|
||||
* This function returns the pointer depending on the context.
|
||||
*/
|
||||
export async function getSiteOrSpacePointerForPDF(): Promise<
|
||||
SiteContentPointer | SpaceContentPointer
|
||||
> {
|
||||
export function getSiteOrSpacePointerForPDF(
|
||||
ctx: GitBookContext,
|
||||
): SiteContentPointer | SpaceContentPointer {
|
||||
try {
|
||||
return await getSiteContentPointer();
|
||||
return getSiteContentPointer(ctx);
|
||||
} catch (error) {
|
||||
return getSpacePointer();
|
||||
return getSpacePointer(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { Space } from '@gitbook/api';
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import { headers } from 'next/headers';
|
||||
import React from 'react';
|
||||
|
||||
import { getChangeRequest, getRevision, SiteContentPointer } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { RefreshChangeRequestButton } from './RefreshChangeRequestButton';
|
||||
@@ -68,8 +70,9 @@ export function AdminToolbar(props: AdminToolbarProps) {
|
||||
|
||||
async function ChangeRequestToolbar(props: { spaceId: string; changeRequestId: string }) {
|
||||
const { spaceId, changeRequestId } = props;
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
|
||||
const changeRequest = await getChangeRequest(spaceId, changeRequestId);
|
||||
const changeRequest = await getChangeRequest(ctx, spaceId, changeRequestId);
|
||||
|
||||
return (
|
||||
<Toolbar>
|
||||
@@ -89,6 +92,7 @@ async function ChangeRequestToolbar(props: { spaceId: string; changeRequestId: s
|
||||
<Icon icon="arrow-up-right-from-square" className="size-4" />
|
||||
</ToolbarButton>
|
||||
<RefreshChangeRequestButton
|
||||
ctx={ctx}
|
||||
spaceId={spaceId}
|
||||
changeRequestId={changeRequestId}
|
||||
revisionId={changeRequest.revision}
|
||||
@@ -101,8 +105,9 @@ async function ChangeRequestToolbar(props: { spaceId: string; changeRequestId: s
|
||||
|
||||
async function RevisionToolbar(props: { spaceId: string; revisionId: string }) {
|
||||
const { spaceId, revisionId } = props;
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
|
||||
const revision = await getRevision(spaceId, revisionId, {
|
||||
const revision = await getRevision(ctx, spaceId, revisionId, {
|
||||
metadata: true,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
'use client';
|
||||
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import React from 'react';
|
||||
|
||||
import { useCheckForContentUpdate } from '@/components/AutoRefreshContent';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { ToolbarButton } from './Toolbar';
|
||||
@@ -14,6 +16,7 @@ const minInterval = 1000 * 30; // 5 minutes
|
||||
* Button to refresh the page if the content has been updated.
|
||||
*/
|
||||
export function RefreshChangeRequestButton(props: {
|
||||
ctx: GitBookContext;
|
||||
spaceId: string;
|
||||
changeRequestId: string;
|
||||
revisionId: string;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
'use client';
|
||||
|
||||
import { SiteAds, SiteAdsStatus } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
import * as React from 'react';
|
||||
|
||||
import { t, useLanguage } from '@/intl/client';
|
||||
import { getIpAndUserAgentFromHeaders, IpAndUserAgent } from '@/lib/gitbook-context';
|
||||
import { ClassValue, tcls } from '@/lib/tailwind';
|
||||
|
||||
import { renderAd } from './renderAd';
|
||||
@@ -18,6 +20,7 @@ const PREVIEW_ZONE_ID = 'CVAIKKQM';
|
||||
* https://docs.buysellads.com/ad-serving-api
|
||||
*/
|
||||
export function Ad({
|
||||
ipAndUserAgent,
|
||||
zoneId,
|
||||
spaceId,
|
||||
placement,
|
||||
@@ -26,6 +29,7 @@ export function Ad({
|
||||
style,
|
||||
mode = 'auto',
|
||||
}: {
|
||||
ipAndUserAgent: IpAndUserAgent;
|
||||
zoneId: string | null;
|
||||
spaceId: string;
|
||||
placement: string;
|
||||
@@ -92,7 +96,7 @@ export function Ad({
|
||||
|
||||
(async () => {
|
||||
const result = showPlaceholderAd
|
||||
? await renderAd({ source: 'placeholder' })
|
||||
? await renderAd({ source: 'placeholder', ipAndUserAgent })
|
||||
: realZoneId
|
||||
? await renderAd({
|
||||
placement,
|
||||
@@ -100,6 +104,7 @@ export function Ad({
|
||||
zoneId: realZoneId,
|
||||
mode,
|
||||
source: 'live',
|
||||
ipAndUserAgent,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
@@ -115,7 +120,7 @@ export function Ad({
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [visible, zoneId, ignore, placement, mode, siteAdsStatus]);
|
||||
}, [visible, zoneId, ignore, placement, mode, siteAdsStatus, ipAndUserAgent]);
|
||||
|
||||
return (
|
||||
<div ref={containerRef} className={tcls(style)} data-visual-test="removed">
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { headers } from 'next/headers';
|
||||
import * as React from 'react';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getResizedImageURL } from '@/lib/images';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -9,10 +11,11 @@ import { AdItem } from './types';
|
||||
* Classic rendering for an ad.
|
||||
*/
|
||||
export async function AdClassicRendering({ ad }: { ad: AdItem }) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const smallImgSrc =
|
||||
'smallImage' in ad ? await getResizedImageURL(ad.smallImage, { width: 192, dpr: 2 }) : null;
|
||||
'smallImage' in ad ? getResizedImageURL(ctx, ad.smallImage, { width: 192, dpr: 2 }) : null;
|
||||
const logoSrc =
|
||||
'logo' in ad ? await getResizedImageURL(ad.logo, { width: 192 - 48, dpr: 2 }) : null;
|
||||
'logo' in ad ? getResizedImageURL(ctx, ad.logo, { width: 192 - 48, dpr: 2 }) : null;
|
||||
return (
|
||||
<a
|
||||
className={tcls(
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { headers } from 'next/headers';
|
||||
import * as React from 'react';
|
||||
|
||||
import { hexToRgba } from '@/lib/colors';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getResizedImageURL } from '@/lib/images';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -10,7 +12,8 @@ import { AdCover } from './types';
|
||||
* Cover rendering for an ad.
|
||||
*/
|
||||
export async function AdCoverRendering({ ad }: { ad: AdCover }) {
|
||||
const largeImage = await getResizedImageURL(ad.largeImage, { width: 128, dpr: 2 });
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const largeImage = await getResizedImageURL(ctx, ad.largeImage, { width: 128, dpr: 2 });
|
||||
|
||||
return (
|
||||
<a
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use server';
|
||||
|
||||
import { headers } from 'next/headers';
|
||||
import { IpAndUserAgent } from '@/lib/gitbook-context';
|
||||
|
||||
import { AdClassicRendering } from './AdClassicRendering';
|
||||
import { AdCoverRendering } from './AdCoverRendering';
|
||||
@@ -23,6 +23,8 @@ interface FetchLiveAdOptions {
|
||||
placement: string;
|
||||
/** If true, we'll not track it as an impression */
|
||||
ignore: boolean;
|
||||
/** IP and User-Agent to use for the request */
|
||||
ipAndUserAgent: IpAndUserAgent;
|
||||
}
|
||||
|
||||
interface FetchPlaceholderAdOptions {
|
||||
@@ -30,6 +32,8 @@ interface FetchPlaceholderAdOptions {
|
||||
* Source of the ad (placeholder: static placeholder ad)
|
||||
*/
|
||||
source: 'placeholder';
|
||||
/** IP and User-Agent to use for the request */
|
||||
ipAndUserAgent: IpAndUserAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +44,8 @@ interface FetchPlaceholderAdOptions {
|
||||
export async function renderAd(options: FetchAdOptions) {
|
||||
const mode = options.source === 'live' ? options.mode : 'classic';
|
||||
|
||||
const result = options.source === 'live' ? await fetchAd(options) : await getPlaceholderAd();
|
||||
const result =
|
||||
options.source === 'live' ? await fetchAd(options) : await getPlaceholderAd(options);
|
||||
if (!result || !result.ad.description || !result.ad.statlink) {
|
||||
return null;
|
||||
}
|
||||
@@ -60,12 +65,12 @@ export async function renderAd(options: FetchAdOptions) {
|
||||
}
|
||||
|
||||
async function fetchAd({
|
||||
ipAndUserAgent,
|
||||
zoneId,
|
||||
placement,
|
||||
ignore,
|
||||
}: FetchLiveAdOptions): Promise<{ ad: AdItem; ip: string } | null> {
|
||||
const { ip, userAgent } = await getUserAgentAndIp();
|
||||
|
||||
const { ip, userAgent } = ipAndUserAgent;
|
||||
const url = new URL(`https://srv.buysellads.com/ads/${zoneId}.json`);
|
||||
url.searchParams.set('segment', `placement:${placement}`);
|
||||
url.searchParams.set('v', 'true');
|
||||
@@ -86,9 +91,7 @@ async function fetchAd({
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getPlaceholderAd(): Promise<{ ad: AdItem; ip: string }> {
|
||||
const { ip } = await getUserAgentAndIp();
|
||||
|
||||
function getPlaceholderAd(options: FetchPlaceholderAdOptions): { ad: AdItem; ip: string } {
|
||||
return {
|
||||
ad: {
|
||||
active: '1',
|
||||
@@ -115,20 +118,6 @@ async function getPlaceholderAd(): Promise<{ ad: AdItem; ip: string }> {
|
||||
zoneid: '',
|
||||
zonekey: '',
|
||||
},
|
||||
ip,
|
||||
ip: options.ipAndUserAgent.ip,
|
||||
};
|
||||
}
|
||||
|
||||
async function getUserAgentAndIp() {
|
||||
const headersSet = await headers();
|
||||
const ip =
|
||||
headersSet.get('x-gitbook-ipv4') ??
|
||||
headersSet.get('x-gitbook-ip') ??
|
||||
headersSet.get('cf-pseudo-ipv4') ??
|
||||
headersSet.get('cf-connecting-ip') ??
|
||||
headersSet.get('x-forwarded-for') ??
|
||||
'';
|
||||
const userAgent = headersSet.get('user-agent') ?? '';
|
||||
|
||||
return { ip, userAgent };
|
||||
}
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
'use server';
|
||||
|
||||
import { getChangeRequest } from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
|
||||
/**
|
||||
* Return true if a change-request has been updated.
|
||||
*/
|
||||
export async function hasContentBeenUpdated(props: {
|
||||
spaceId: string;
|
||||
changeRequestId: string;
|
||||
revisionId: string;
|
||||
}) {
|
||||
const changeRequest = await getChangeRequest.revalidate(props.spaceId, props.changeRequestId);
|
||||
export async function hasContentBeenUpdated(
|
||||
ctx: GitBookContext,
|
||||
props: {
|
||||
spaceId: string;
|
||||
changeRequestId: string;
|
||||
revisionId: string;
|
||||
},
|
||||
) {
|
||||
const changeRequest = await getChangeRequest.revalidate(
|
||||
ctx,
|
||||
props.spaceId,
|
||||
props.changeRequestId,
|
||||
);
|
||||
return changeRequest.revision !== props.revisionId;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import React from 'react';
|
||||
import { useEventCallback } from 'usehooks-ts';
|
||||
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
|
||||
import { hasContentBeenUpdated } from './server-actions';
|
||||
|
||||
@@ -8,17 +11,23 @@ import { hasContentBeenUpdated } from './server-actions';
|
||||
* Return a callback to check if a change request has been updated and to refresh the page if it has.
|
||||
*/
|
||||
export function useCheckForContentUpdate(props: {
|
||||
ctx: GitBookContext;
|
||||
spaceId: string;
|
||||
changeRequestId: string;
|
||||
revisionId: string;
|
||||
}) {
|
||||
const { spaceId, changeRequestId, revisionId } = props;
|
||||
const { ctx, spaceId, changeRequestId, revisionId } = props;
|
||||
const getCtx = useEventCallback(() => ctx);
|
||||
|
||||
return React.useCallback(async () => {
|
||||
const updated = await hasContentBeenUpdated({ spaceId, changeRequestId, revisionId });
|
||||
const updated = await hasContentBeenUpdated(getCtx(), {
|
||||
spaceId,
|
||||
changeRequestId,
|
||||
revisionId,
|
||||
});
|
||||
|
||||
if (updated) {
|
||||
window.location.reload();
|
||||
}
|
||||
}, [spaceId, changeRequestId, revisionId]);
|
||||
}, [spaceId, changeRequestId, revisionId, getCtx]);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { DocumentBlockContentRef } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { Card } from '@/components/primitives';
|
||||
import { getSpaceCustomization, ignoreAPIError } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { ResolvedContentRef } from '@/lib/references';
|
||||
|
||||
import { BlockProps } from './Block';
|
||||
@@ -52,7 +54,9 @@ async function SpaceRefCard(
|
||||
return null;
|
||||
}
|
||||
|
||||
const { customization: spaceCustomization } = await getSpaceCustomization();
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
|
||||
const { customization: spaceCustomization } = await getSpaceCustomization(ctx);
|
||||
const customFavicon = spaceCustomization?.favicon;
|
||||
const customEmoji = customFavicon && 'emoji' in customFavicon ? customFavicon.emoji : undefined;
|
||||
const customIcon = customFavicon && 'icon' in customFavicon ? customFavicon.icon : undefined;
|
||||
|
||||
@@ -5,6 +5,7 @@ import ReactDOM from 'react-dom';
|
||||
|
||||
import { Card } from '@/components/primitives';
|
||||
import { getEmbedByUrlInSpace, getEmbedByUrl } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { BlockProps } from './Block';
|
||||
@@ -12,15 +13,15 @@ import { Caption } from './Caption';
|
||||
import { IntegrationBlock } from './Integration';
|
||||
|
||||
export async function Embed(props: BlockProps<gitbookAPI.DocumentBlockEmbed>) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { block, context, ...otherProps } = props;
|
||||
const headersList = await headers();
|
||||
const nonce = headersList.get('x-nonce') || undefined;
|
||||
const nonce = ctx.nonce || undefined;
|
||||
|
||||
ReactDOM.preload('https://cdn.iframe.ly/embed.js', { as: 'script', nonce });
|
||||
|
||||
const embed = await (context.content
|
||||
? getEmbedByUrlInSpace(context.content.spaceId, block.data.url)
|
||||
: getEmbedByUrl(block.data.url));
|
||||
? getEmbedByUrlInSpace(ctx, context.content.spaceId, block.data.url)
|
||||
: getEmbedByUrl(ctx, block.data.url));
|
||||
|
||||
return (
|
||||
<Caption {...props}>
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { ContentKitContext, DocumentBlockIntegration } from '@gitbook/api';
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import { ContentKit, ContentKitOutput, ContentKitServerContext } from '@gitbook/react-contentkit';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { ignoreAPIError, renderIntegrationUi } from '@/lib/api';
|
||||
import { INTEGRATIONS_HOST } from '@/lib/csp';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { parseMarkdown } from '@/lib/markdown';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -45,6 +47,8 @@ export async function IntegrationBlock(props: BlockProps<DocumentBlockIntegratio
|
||||
throw new Error('integration block requires a content.spaceId');
|
||||
}
|
||||
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
|
||||
const contentKitContext: ContentKitContext = {
|
||||
type: 'document',
|
||||
spaceId: context.content.spaceId,
|
||||
@@ -60,9 +64,10 @@ export async function IntegrationBlock(props: BlockProps<DocumentBlockIntegratio
|
||||
};
|
||||
|
||||
const initialOutput = await ignoreAPIError(
|
||||
renderIntegrationUi(block.data.integration, initialInput),
|
||||
renderIntegrationUi(ctx, block.data.integration, initialInput),
|
||||
true,
|
||||
);
|
||||
|
||||
if (!initialOutput || initialOutput.type === 'complete') {
|
||||
return null;
|
||||
}
|
||||
@@ -76,7 +81,7 @@ export async function IntegrationBlock(props: BlockProps<DocumentBlockIntegratio
|
||||
render={async (request) => {
|
||||
'use server';
|
||||
|
||||
const output = await renderIntegrationUi(block.data.integration, request);
|
||||
const output = await renderIntegrationUi(ctx, block.data.integration, request);
|
||||
|
||||
return {
|
||||
children: <ContentKitOutput output={output} context={outputContext} />,
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { DocumentBlockReusableContent } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { getDocument } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
|
||||
import { BlockProps } from './Block';
|
||||
import { UnwrappedBlocks } from './Blocks';
|
||||
|
||||
export async function ReusableContent(props: BlockProps<DocumentBlockReusableContent>) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { block, context, ancestorBlocks } = props;
|
||||
|
||||
if (!context.content) {
|
||||
@@ -17,7 +20,11 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
|
||||
return null;
|
||||
}
|
||||
|
||||
const document = await getDocument(context.content.spaceId, resolved.reusableContent.document);
|
||||
const document = await getDocument(
|
||||
ctx,
|
||||
context.content.spaceId,
|
||||
resolved.reusableContent.document,
|
||||
);
|
||||
|
||||
if (!document) {
|
||||
return null;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { CustomizationContentLink, CustomizationFooterGroup } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -22,8 +24,9 @@ export function FooterLinksGroup(props: {
|
||||
}
|
||||
|
||||
async function FooterLink(props: { link: CustomizationContentLink; context: ContentRefContext }) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { link, context } = props;
|
||||
const resolved = await resolveContentRef(link.to, context);
|
||||
const resolved = await resolveContentRef(ctx, link.to, context);
|
||||
|
||||
if (!resolved) {
|
||||
return null;
|
||||
|
||||
@@ -7,7 +7,9 @@ import {
|
||||
ContentRef,
|
||||
} from '@gitbook/api';
|
||||
import assertNever from 'assert-never';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -25,9 +27,10 @@ export async function HeaderLink(props: {
|
||||
link: CustomizationHeaderItem;
|
||||
customization: CustomizationSettings | SiteCustomizationSettings;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { context, link, customization } = props;
|
||||
|
||||
const target = link.to ? await resolveContentRef(link.to, context) : null;
|
||||
const target = link.to ? await resolveContentRef(ctx, link.to, context) : null;
|
||||
const headerPreset = customization.header.preset;
|
||||
const linkStyle = link.style ?? 'link';
|
||||
|
||||
@@ -207,9 +210,10 @@ async function SubHeaderLink(props: {
|
||||
context: ContentRefContext;
|
||||
link: CustomizationContentLink;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { context, link } = props;
|
||||
|
||||
const target = await resolveContentRef(link.to, context);
|
||||
const target = await resolveContentRef(ctx, link.to, context);
|
||||
|
||||
if (!target) {
|
||||
return null;
|
||||
|
||||
@@ -6,8 +6,10 @@ import {
|
||||
SiteCustomizationSettings,
|
||||
} from '@gitbook/api';
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import { headers } from 'next/headers';
|
||||
import React from 'react';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -67,9 +69,10 @@ async function MoreMenuLink(props: {
|
||||
context: ContentRefContext;
|
||||
link: CustomizationHeaderItem | CustomizationContentLink;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { context, link } = props;
|
||||
|
||||
const target = link.to ? await resolveContentRef(link.to, context) : null;
|
||||
const target = link.to ? await resolveContentRef(ctx, link.to, context) : null;
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -5,8 +5,10 @@ import {
|
||||
SiteCustomizationSettings,
|
||||
Space,
|
||||
} from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { Image } from '@/components/utils';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref } from '@/lib/links';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
import { getContentTitle } from '@/lib/utils';
|
||||
@@ -25,8 +27,9 @@ interface HeaderLogoProps {
|
||||
*/
|
||||
|
||||
export async function HeaderLogo(props: HeaderLogoProps) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { customization } = props;
|
||||
const href = await getAbsoluteHref('');
|
||||
const href = getAbsoluteHref(ctx, '');
|
||||
|
||||
return (
|
||||
<Link
|
||||
|
||||
@@ -8,11 +8,13 @@ import {
|
||||
Space,
|
||||
} from '@gitbook/api';
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import { headers } from 'next/headers';
|
||||
import React from 'react';
|
||||
import urlJoin from 'url-join';
|
||||
|
||||
import { t, getSpaceLanguage } from '@/intl/server';
|
||||
import { getDocumentSections } from '@/lib/document';
|
||||
import { getGitBookContextFromHeaders, getIpAndUserAgentFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref } from '@/lib/links';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
@@ -48,6 +50,9 @@ export async function PageAside(props: {
|
||||
withFullPageCover: boolean;
|
||||
withPageFeedback: boolean;
|
||||
}) {
|
||||
const headersList = await headers();
|
||||
const ctx = getGitBookContextFromHeaders(headersList);
|
||||
const ipAndUserAgent = getIpAndUserAgentFromHeaders(headersList);
|
||||
const {
|
||||
space,
|
||||
site,
|
||||
@@ -61,7 +66,8 @@ export async function PageAside(props: {
|
||||
const language = getSpaceLanguage(customization);
|
||||
|
||||
const topOffset = getTopOffset(withHeaderOffset);
|
||||
const pdfHref = await getAbsoluteHref(
|
||||
const pdfHref = getAbsoluteHref(
|
||||
ctx,
|
||||
`~gitbook/pdf?${getPDFUrlSearchParams({
|
||||
page: page.id,
|
||||
only: true,
|
||||
@@ -174,7 +180,7 @@ export async function PageAside(props: {
|
||||
>
|
||||
{withPageFeedback ? (
|
||||
<React.Suspense fallback={null}>
|
||||
<PageFeedbackForm pageId={page.id} className={tcls('mt-2')} />
|
||||
<PageFeedbackForm ctx={ctx} pageId={page.id} className={tcls('mt-2')} />
|
||||
</React.Suspense>
|
||||
) : null}
|
||||
{customization.git.showEditLink && space.gitSync?.url && page.git ? (
|
||||
@@ -223,6 +229,7 @@ export async function PageAside(props: {
|
||||
</div>
|
||||
</div>
|
||||
<Ad
|
||||
ipAndUserAgent={ipAndUserAgent}
|
||||
zoneId={
|
||||
site?.ads && site.ads.status === SiteAdsStatus.Live ? site.ads.zoneId : null
|
||||
}
|
||||
@@ -237,9 +244,12 @@ export async function PageAside(props: {
|
||||
}
|
||||
|
||||
async function PageAsideSections(props: { document: JSONDocument; context: ContentRefContext }) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { document, context } = props;
|
||||
|
||||
const sections = await getDocumentSections(document, (ref) => resolveContentRef(ref, context));
|
||||
const sections = await getDocumentSections(document, (ref) =>
|
||||
resolveContentRef(ctx, ref, context),
|
||||
);
|
||||
|
||||
return sections.length > 1 ? <ScrollSectionsList sections={sections} /> : null;
|
||||
}
|
||||
|
||||
@@ -5,12 +5,14 @@ import {
|
||||
SiteCustomizationSettings,
|
||||
Space,
|
||||
} from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
import React from 'react';
|
||||
|
||||
import { getSpaceLanguage } from '@/intl/server';
|
||||
import { t } from '@/intl/translate';
|
||||
import { ContentTarget, SiteContentPointer, api } from '@/lib/api';
|
||||
import { ContentTarget, SiteContentPointer } from '@/lib/api';
|
||||
import { hasFullWidthBlock, isNodeEmpty } from '@/lib/document';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { AncestorRevisionPage } from '@/lib/pages';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
@@ -25,7 +27,7 @@ import { TrackPageViewEvent } from '../Insights';
|
||||
import { PageFeedbackForm } from '../PageFeedback';
|
||||
import { DateRelative } from '../primitives';
|
||||
|
||||
export function PageBody(props: {
|
||||
export async function PageBody(props: {
|
||||
space: Space;
|
||||
pointer: SiteContentPointer;
|
||||
contentTarget: ContentTarget;
|
||||
@@ -36,6 +38,7 @@ export function PageBody(props: {
|
||||
context: ContentRefContext;
|
||||
withPageFeedback: boolean;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const {
|
||||
space,
|
||||
contentTarget,
|
||||
@@ -99,7 +102,7 @@ export function PageBody(props: {
|
||||
content: contentTarget,
|
||||
contentRefContext: context,
|
||||
resolveContentRef: (ref, options) =>
|
||||
resolveContentRef(ref, context, options),
|
||||
resolveContentRef(ctx, ref, context, options),
|
||||
}}
|
||||
/>
|
||||
</React.Suspense>
|
||||
@@ -140,7 +143,7 @@ export function PageBody(props: {
|
||||
</p>
|
||||
) : null}
|
||||
{withPageFeedback ? (
|
||||
<PageFeedbackForm orientation="horizontal" pageId={page.id} />
|
||||
<PageFeedbackForm ctx={ctx} orientation="horizontal" pageId={page.id} />
|
||||
) : null}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { RevisionPage, RevisionPageDocument, RevisionPageType } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { Card } from '@/components/primitives';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getPageHref } from '@/lib/links';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
@@ -15,6 +17,7 @@ export async function PageBodyBlankslate(props: {
|
||||
rootPages: RevisionPage[];
|
||||
context: ContentRefContext;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { page, rootPages, context } = props;
|
||||
|
||||
const pages = page.pages.filter((child) =>
|
||||
@@ -35,7 +38,7 @@ export async function PageBodyBlankslate(props: {
|
||||
'Unexpected computed page, it should have been computed in the API',
|
||||
);
|
||||
} else if (child.type === RevisionPageType.Link) {
|
||||
const resolved = await resolveContentRef(child.target, context);
|
||||
const resolved = await resolveContentRef(ctx, child.target, context);
|
||||
if (!resolved) {
|
||||
return null;
|
||||
}
|
||||
@@ -53,7 +56,7 @@ export async function PageBodyBlankslate(props: {
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
const href = await getPageHref(rootPages, child);
|
||||
const href = getPageHref(ctx, rootPages, child);
|
||||
return <Card key={child.id} title={child.title} leadingIcon={icon} href={href} />;
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { RevisionPageDocument, RevisionPageDocumentCover } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { Image, ImageSize } from '@/components/utils';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -17,8 +19,9 @@ export async function PageCover(props: {
|
||||
cover: RevisionPageDocumentCover;
|
||||
context: ContentRefContext;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { as, page, cover, context } = props;
|
||||
const resolved = cover.ref ? await resolveContentRef(cover.ref, context) : null;
|
||||
const resolved = cover.ref ? await resolveContentRef(ctx, cover.ref, context) : null;
|
||||
|
||||
return (
|
||||
<div
|
||||
|
||||
@@ -6,9 +6,11 @@ import {
|
||||
Space,
|
||||
} from '@gitbook/api';
|
||||
import { Icon, IconName } from '@gitbook/icons';
|
||||
import { headers } from 'next/headers';
|
||||
import React from 'react';
|
||||
|
||||
import { t, getSpaceLanguage } from '@/intl/server';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getPageHref } from '@/lib/links';
|
||||
import { resolvePrevNextPages } from '@/lib/pages';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
@@ -24,11 +26,12 @@ export async function PageFooterNavigation(props: {
|
||||
pages: Revision['pages'];
|
||||
page: RevisionPageDocument;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { customization, pages, page } = props;
|
||||
const { previous, next } = resolvePrevNextPages(pages, page);
|
||||
const language = getSpaceLanguage(customization);
|
||||
const previousHref = previous ? await getPageHref(pages, previous) : '';
|
||||
const nextHref = next ? await getPageHref(pages, next) : '';
|
||||
const previousHref = previous ? getPageHref(ctx, pages, previous) : '';
|
||||
const nextHref = next ? getPageHref(ctx, pages, next) : '';
|
||||
|
||||
return (
|
||||
<div
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { RevisionPage, RevisionPageDocument } from '@gitbook/api';
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import { headers } from 'next/headers';
|
||||
import { Fragment } from 'react';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getPageHref } from '@/lib/links';
|
||||
import { AncestorRevisionPage } from '@/lib/pages';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
@@ -14,6 +16,7 @@ export async function PageHeader(props: {
|
||||
ancestors: AncestorRevisionPage[];
|
||||
pages: RevisionPage[];
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { page, ancestors, pages } = props;
|
||||
|
||||
if (!page.layout.title && !page.layout.description) {
|
||||
@@ -22,7 +25,7 @@ export async function PageHeader(props: {
|
||||
|
||||
const ancestorElements = await Promise.all(
|
||||
ancestors.map(async (breadcrumb, index) => {
|
||||
const href = await getPageHref(pages, breadcrumb);
|
||||
const href = await getPageHref(ctx, pages, breadcrumb);
|
||||
return (
|
||||
<Fragment key={breadcrumb.id}>
|
||||
<li key={breadcrumb.id}>
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
'use client';
|
||||
|
||||
import { PageFeedbackRating } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
import React from 'react';
|
||||
|
||||
import { useLanguage } from '@/intl/client';
|
||||
import { t, tString } from '@/intl/translate';
|
||||
import { getGitBookContextFromHeaders, GitBookContext } from '@/lib/gitbook-context';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { getVisitorId, useTrackEvent } from '../Insights';
|
||||
@@ -14,11 +16,12 @@ import { postPageFeedback } from './server-actions';
|
||||
* Form to submit feedback on a page.
|
||||
*/
|
||||
export function PageFeedbackForm(props: {
|
||||
ctx: GitBookContext;
|
||||
orientation?: 'horizontal' | 'vertical';
|
||||
pageId: string;
|
||||
className?: string;
|
||||
}) {
|
||||
const { orientation = 'vertical', pageId, className } = props;
|
||||
const { ctx, orientation = 'vertical', pageId, className } = props;
|
||||
const languages = useLanguage();
|
||||
const trackEvent = useTrackEvent();
|
||||
const [submitted, setSubmitted] = React.useState(false);
|
||||
@@ -26,7 +29,7 @@ export function PageFeedbackForm(props: {
|
||||
const onSubmit = async (rating: PageFeedbackRating) => {
|
||||
setSubmitted(true);
|
||||
const visitorId = await getVisitorId();
|
||||
await postPageFeedback({ pageId, visitorId, rating });
|
||||
await postPageFeedback(ctx, { pageId, visitorId, rating });
|
||||
|
||||
trackEvent({
|
||||
type: 'page_post_feedback',
|
||||
|
||||
@@ -4,23 +4,25 @@ import { PageFeedbackRating } from '@gitbook/api';
|
||||
import { assert } from 'ts-essentials';
|
||||
|
||||
import { api } from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { getSiteContentPointer } from '@/lib/pointer';
|
||||
|
||||
export async function postPageFeedback(args: {
|
||||
pageId: string;
|
||||
visitorId: string;
|
||||
rating: PageFeedbackRating;
|
||||
}) {
|
||||
const { organizationId, siteId, siteSpaceId } = await getSiteContentPointer();
|
||||
export async function postPageFeedback(
|
||||
ctx: GitBookContext,
|
||||
args: {
|
||||
pageId: string;
|
||||
visitorId: string;
|
||||
rating: PageFeedbackRating;
|
||||
},
|
||||
) {
|
||||
const { organizationId, siteId, siteSpaceId } = getSiteContentPointer(ctx);
|
||||
|
||||
assert(
|
||||
siteSpaceId,
|
||||
`No siteSpaceId in pointer. organizationId: ${organizationId}, siteId: ${siteId}, pageId: ${args.pageId}`,
|
||||
);
|
||||
|
||||
const apiCtx = await api();
|
||||
|
||||
await apiCtx.client.orgs.createSitesPageFeedback(
|
||||
await api(ctx).client.orgs.createSitesPageFeedback(
|
||||
organizationId,
|
||||
siteId,
|
||||
siteSpaceId,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import React from 'react';
|
||||
import { useEventCallback } from 'usehooks-ts';
|
||||
|
||||
import { Loading } from '@/components/primitives';
|
||||
import { useLanguage } from '@/intl/client';
|
||||
@@ -9,6 +10,7 @@ import { t } from '@/intl/translate';
|
||||
import { TranslationLanguage } from '@/intl/translations';
|
||||
import { iterateStreamResponse } from '@/lib/actions';
|
||||
import { SiteContentPointer } from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { AskAnswerResult, AskAnswerSource, streamAskQuestion } from './server-actions';
|
||||
@@ -32,14 +34,19 @@ export type SearchAskState =
|
||||
/**
|
||||
* Fetch and render the answers to a question.
|
||||
*/
|
||||
export function SearchAskAnswer(props: { pointer: SiteContentPointer; query: string }) {
|
||||
const { pointer, query } = props;
|
||||
export function SearchAskAnswer(props: {
|
||||
ctx: GitBookContext;
|
||||
pointer: SiteContentPointer;
|
||||
query: string;
|
||||
}) {
|
||||
const { ctx, pointer, query } = props;
|
||||
|
||||
const language = useLanguage();
|
||||
const trackEvent = useTrackEvent();
|
||||
const [, setSearchState] = useSearch();
|
||||
const [askState, setAskState] = useSearchAskContext();
|
||||
const { organizationId, siteId, siteSpaceId } = pointer;
|
||||
const getCtx = useEventCallback(() => ctx);
|
||||
|
||||
React.useEffect(() => {
|
||||
let cancelled = false;
|
||||
@@ -52,7 +59,13 @@ export function SearchAskAnswer(props: { pointer: SiteContentPointer; query: str
|
||||
query,
|
||||
});
|
||||
|
||||
const response = streamAskQuestion(organizationId, siteId, siteSpaceId ?? null, query);
|
||||
const response = streamAskQuestion(
|
||||
getCtx(),
|
||||
organizationId,
|
||||
siteId,
|
||||
siteSpaceId ?? null,
|
||||
query,
|
||||
);
|
||||
const stream = iterateStreamResponse(response);
|
||||
|
||||
// When we pass in "ask" mode, the query could still be updated by the client
|
||||
@@ -81,7 +94,16 @@ export function SearchAskAnswer(props: { pointer: SiteContentPointer; query: str
|
||||
cancelled = true;
|
||||
}
|
||||
};
|
||||
}, [organizationId, siteId, siteSpaceId, query, setAskState, setSearchState, trackEvent]);
|
||||
}, [
|
||||
organizationId,
|
||||
siteId,
|
||||
siteSpaceId,
|
||||
query,
|
||||
setAskState,
|
||||
setSearchState,
|
||||
trackEvent,
|
||||
getCtx,
|
||||
]);
|
||||
|
||||
React.useEffect(() => {
|
||||
return () => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { useHotkeys } from 'react-hotkeys-hook';
|
||||
|
||||
import { tString, useLanguage } from '@/intl/client';
|
||||
import { SiteContentPointer } from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { SearchAskAnswer } from './SearchAskAnswer';
|
||||
@@ -18,6 +19,7 @@ import { SearchState, UpdateSearchState, useSearch } from './useSearch';
|
||||
import { LoadingPane } from '../primitives/LoadingPane';
|
||||
|
||||
interface SearchModalProps {
|
||||
ctx: GitBookContext;
|
||||
spaceId: string;
|
||||
revisionId: string;
|
||||
spaceTitle: string;
|
||||
@@ -308,6 +310,7 @@ function SearchModalBody(
|
||||
{!state.ask || !withAsk ? (
|
||||
<SearchResults
|
||||
ref={resultsRef}
|
||||
ctx={props.ctx}
|
||||
pointer={pointer}
|
||||
spaceId={spaceId}
|
||||
revisionId={revisionId}
|
||||
@@ -318,7 +321,7 @@ function SearchModalBody(
|
||||
></SearchResults>
|
||||
) : null}
|
||||
{state.query && state.ask && withAsk ? (
|
||||
<SearchAskAnswer pointer={pointer} query={state.query} />
|
||||
<SearchAskAnswer ctx={props.ctx} pointer={pointer} query={state.query} />
|
||||
) : null}
|
||||
</motion.div>
|
||||
);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { captureException } from '@sentry/nextjs';
|
||||
'use client';
|
||||
import assertNever from 'assert-never';
|
||||
import React from 'react';
|
||||
import { useEventCallback } from 'usehooks-ts';
|
||||
|
||||
import { t, useLanguage } from '@/intl/client';
|
||||
import { SiteContentPointer } from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { SearchPageResultItem } from './SearchPageResultItem';
|
||||
@@ -37,6 +39,7 @@ type ResultType =
|
||||
*/
|
||||
export const SearchResults = React.forwardRef(function SearchResults(
|
||||
props: {
|
||||
ctx: GitBookContext;
|
||||
children?: React.ReactNode;
|
||||
query: string;
|
||||
spaceId: string;
|
||||
@@ -48,7 +51,8 @@ export const SearchResults = React.forwardRef(function SearchResults(
|
||||
},
|
||||
ref: React.Ref<SearchResultsRef>,
|
||||
) {
|
||||
const { children, query, pointer, spaceId, revisionId, withAsk, global, onSwitchToAsk } = props;
|
||||
const { ctx, children, query, pointer, spaceId, revisionId, withAsk, global, onSwitchToAsk } =
|
||||
props;
|
||||
|
||||
const language = useLanguage();
|
||||
const trackEvent = useTrackEvent();
|
||||
@@ -59,6 +63,7 @@ export const SearchResults = React.forwardRef(function SearchResults(
|
||||
const [cursor, setCursor] = React.useState<number | null>(null);
|
||||
const refs = React.useRef<(null | HTMLAnchorElement)[]>([]);
|
||||
const suggestedQuestionsRef = React.useRef<null | ResultType[]>(null);
|
||||
const getCtx = useEventCallback(() => ctx);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!query) {
|
||||
@@ -75,7 +80,7 @@ export const SearchResults = React.forwardRef(function SearchResults(
|
||||
let cancelled = false;
|
||||
|
||||
setResultsState({ results: [], fetching: true });
|
||||
getRecommendedQuestions(spaceId).then((questions) => {
|
||||
getRecommendedQuestions(getCtx(), spaceId).then((questions) => {
|
||||
const results = questions.map((question) => ({
|
||||
type: 'recommended-question',
|
||||
id: question,
|
||||
@@ -99,8 +104,8 @@ export const SearchResults = React.forwardRef(function SearchResults(
|
||||
let cancelled = false;
|
||||
const timeout = setTimeout(async () => {
|
||||
const results = await (global
|
||||
? searchAllSiteContent(query, pointer)
|
||||
: searchSiteSpaceContent(query, pointer, revisionId));
|
||||
? searchAllSiteContent(getCtx(), query, pointer)
|
||||
: searchSiteSpaceContent(getCtx(), query, pointer, revisionId));
|
||||
|
||||
if (cancelled) {
|
||||
return;
|
||||
@@ -119,7 +124,7 @@ export const SearchResults = React.forwardRef(function SearchResults(
|
||||
clearTimeout(timeout);
|
||||
};
|
||||
}
|
||||
}, [query, global, pointer, spaceId, revisionId, withAsk, trackEvent]);
|
||||
}, [query, global, pointer, spaceId, revisionId, withAsk, trackEvent, getCtx]);
|
||||
|
||||
const results: ResultType[] = React.useMemo(() => {
|
||||
if (!withAsk) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { assert } from 'ts-essentials';
|
||||
|
||||
import { streamResponse } from '@/lib/actions';
|
||||
import * as api from '@/lib/api';
|
||||
import { GitBookContext } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref, getPageHref } from '@/lib/links';
|
||||
import { resolvePageId } from '@/lib/pages';
|
||||
import { filterOutNullable } from '@/lib/typescript';
|
||||
@@ -48,15 +49,18 @@ export interface AskAnswerResult {
|
||||
/**
|
||||
* Search for content in a site by scoping the search to all content, a specific spaces or current space.
|
||||
*/
|
||||
async function searchSiteContent(args: {
|
||||
pointer: api.SiteContentPointer;
|
||||
query: string;
|
||||
scope:
|
||||
| { mode: 'all' }
|
||||
| { mode: 'current'; siteSpaceId: string }
|
||||
| { mode: 'specific'; siteSpaceIds: string[] };
|
||||
cacheBust?: string;
|
||||
}): Promise<OrderedComputedResult[]> {
|
||||
async function searchSiteContent(
|
||||
ctx: GitBookContext,
|
||||
args: {
|
||||
pointer: api.SiteContentPointer;
|
||||
query: string;
|
||||
scope:
|
||||
| { mode: 'all' }
|
||||
| { mode: 'current'; siteSpaceId: string }
|
||||
| { mode: 'specific'; siteSpaceIds: string[] };
|
||||
cacheBust?: string;
|
||||
},
|
||||
): Promise<OrderedComputedResult[]> {
|
||||
const { pointer, scope, query, cacheBust } = args;
|
||||
|
||||
if (query.length <= 1) {
|
||||
@@ -69,8 +73,8 @@ async function searchSiteContent(args: {
|
||||
(scope.mode === 'specific' && scope.siteSpaceIds.length > 1);
|
||||
|
||||
const [searchResults, siteData] = await Promise.all([
|
||||
api.searchSiteContent(pointer.organizationId, pointer.siteId, query, scope, cacheBust),
|
||||
needsStructure ? api.getSiteData(pointer) : null,
|
||||
api.searchSiteContent(ctx, pointer.organizationId, pointer.siteId, query, scope, cacheBust),
|
||||
needsStructure ? api.getSiteData(ctx, pointer) : null,
|
||||
]);
|
||||
const siteStructure = siteData?.structure;
|
||||
|
||||
@@ -93,38 +97,31 @@ async function searchSiteContent(args: {
|
||||
|
||||
if (siteSpaces) {
|
||||
// We are searching all of this Site's content
|
||||
return (
|
||||
await Promise.all(
|
||||
searchResults.items.map(async (spaceItem) => {
|
||||
const siteSpace = siteSpaces.find(
|
||||
(siteSpace) => siteSpace.space.id === spaceItem.id,
|
||||
);
|
||||
return searchResults.items
|
||||
.map((spaceItem) => {
|
||||
const siteSpace = siteSpaces.find(
|
||||
(siteSpace) => siteSpace.space.id === spaceItem.id,
|
||||
);
|
||||
|
||||
return Promise.all(
|
||||
spaceItem.pages.map((item) => transformSitePageResult(item, siteSpace)),
|
||||
);
|
||||
}),
|
||||
)
|
||||
).flat(2);
|
||||
return spaceItem.pages.map((item) => transformSitePageResult(ctx, item, siteSpace));
|
||||
})
|
||||
.flat(2);
|
||||
}
|
||||
|
||||
return (
|
||||
await Promise.all(
|
||||
searchResults.items.map((spaceItem) => {
|
||||
return Promise.all(spaceItem.pages.map((item) => transformPageResult(item)));
|
||||
}),
|
||||
)
|
||||
).flat(2);
|
||||
return searchResults.items
|
||||
.map((spaceItem) => spaceItem.pages.map((item) => transformPageResult(ctx, item)))
|
||||
.flat(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Server action to search content in the entire site.
|
||||
*/
|
||||
export async function searchAllSiteContent(
|
||||
ctx: GitBookContext,
|
||||
query: string,
|
||||
pointer: api.SiteContentPointer,
|
||||
): Promise<OrderedComputedResult[]> {
|
||||
return await searchSiteContent({
|
||||
return await searchSiteContent(ctx, {
|
||||
pointer,
|
||||
query,
|
||||
scope: { mode: 'all' },
|
||||
@@ -135,6 +132,7 @@ export async function searchAllSiteContent(
|
||||
* Server action to search content in a space.
|
||||
*/
|
||||
export async function searchSiteSpaceContent(
|
||||
ctx: GitBookContext,
|
||||
query: string,
|
||||
pointer: api.SiteContentPointer,
|
||||
revisionId: string,
|
||||
@@ -142,7 +140,7 @@ export async function searchSiteSpaceContent(
|
||||
const siteSpaceId = pointer.siteSpaceId;
|
||||
assert(siteSpaceId, 'Expected siteSpaceId for searchSiteSpaceContent');
|
||||
|
||||
return await searchSiteContent({
|
||||
return await searchSiteContent(ctx, {
|
||||
pointer,
|
||||
query,
|
||||
// If we have a siteSectionId that means its a sections site use `current` mode
|
||||
@@ -159,13 +157,13 @@ export async function searchSiteSpaceContent(
|
||||
* Server action to ask a question in a space.
|
||||
*/
|
||||
export const streamAskQuestion = streamResponse(async function* (
|
||||
ctx: GitBookContext,
|
||||
organizationId: string,
|
||||
siteId: string,
|
||||
siteSpaceId: string | null,
|
||||
question: string,
|
||||
) {
|
||||
const apiCtx = await api.api();
|
||||
const stream = apiCtx.client.orgs.streamAskInSite(
|
||||
const stream = api.api(ctx).client.orgs.streamAskInSite(
|
||||
organizationId,
|
||||
siteId,
|
||||
{
|
||||
@@ -199,7 +197,9 @@ export const streamAskQuestion = streamResponse(async function* (
|
||||
if (!spacePromises.has(source.space)) {
|
||||
spacePromises.set(
|
||||
source.space,
|
||||
api.getRevisionPages(source.space, source.revision, { metadata: false }),
|
||||
api.getRevisionPages(ctx, source.space, source.revision, {
|
||||
metadata: false,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -221,48 +221,50 @@ export const streamAskQuestion = streamResponse(async function* (
|
||||
return map;
|
||||
}, new Map<string, RevisionPage[]>());
|
||||
});
|
||||
yield await transformAnswer(chunk.answer, pages);
|
||||
yield transformAnswer(ctx, chunk.answer, pages);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* List suggested questions for a space.
|
||||
*/
|
||||
export async function getRecommendedQuestions(spaceId: string): Promise<string[]> {
|
||||
const data = await api.getRecommendedQuestionsInSpace(spaceId);
|
||||
export async function getRecommendedQuestions(
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
): Promise<string[]> {
|
||||
const data = await api.getRecommendedQuestionsInSpace(ctx, spaceId);
|
||||
return data.questions;
|
||||
}
|
||||
|
||||
async function transformAnswer(
|
||||
function transformAnswer(
|
||||
ctx: GitBookContext,
|
||||
answer: SearchAIAnswer,
|
||||
spacePages: Map<string, RevisionPage[]>,
|
||||
): Promise<AskAnswerResult> {
|
||||
const sources = (
|
||||
await Promise.all(
|
||||
answer.sources.map(async (source) => {
|
||||
if (source.type !== 'page') {
|
||||
return null;
|
||||
}
|
||||
): AskAnswerResult {
|
||||
const sources = answer.sources
|
||||
.map((source) => {
|
||||
if (source.type !== 'page') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pages = spacePages.get(source.space);
|
||||
const pages = spacePages.get(source.space);
|
||||
|
||||
if (!pages) {
|
||||
return null;
|
||||
}
|
||||
if (!pages) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const page = resolvePageId(pages, source.page);
|
||||
if (!page) {
|
||||
return null;
|
||||
}
|
||||
const page = resolvePageId(pages, source.page);
|
||||
if (!page) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
id: source.page,
|
||||
title: page.page.title,
|
||||
href: await getPageHref(pages, page.page),
|
||||
};
|
||||
}),
|
||||
)
|
||||
).filter(filterOutNullable);
|
||||
return {
|
||||
id: source.page,
|
||||
title: page.page.title,
|
||||
href: getPageHref(ctx, pages, page.page),
|
||||
};
|
||||
})
|
||||
.filter(filterOutNullable);
|
||||
|
||||
return {
|
||||
body:
|
||||
@@ -283,16 +285,19 @@ async function transformAnswer(
|
||||
};
|
||||
}
|
||||
|
||||
async function transformSectionsAndPage(args: {
|
||||
item: SearchPageResult;
|
||||
space?: Space;
|
||||
spaceURL?: string;
|
||||
}): Promise<[ComputedPageResult, ComputedSectionResult[]]> {
|
||||
function transformSectionsAndPage(
|
||||
ctx: GitBookContext,
|
||||
args: {
|
||||
item: SearchPageResult;
|
||||
space?: Space;
|
||||
spaceURL?: string;
|
||||
},
|
||||
): [ComputedPageResult, ComputedSectionResult[]] {
|
||||
const { item, space, spaceURL } = args;
|
||||
|
||||
// Resolve a relative path to an absolute URL
|
||||
// if the search result is relative to another space, we use the space URL
|
||||
const getURL = async (path: string, spaceURL?: string) => {
|
||||
const getURL = (path: string, spaceURL?: string) => {
|
||||
if (spaceURL) {
|
||||
if (!spaceURL.endsWith('/')) {
|
||||
spaceURL += '/';
|
||||
@@ -302,33 +307,36 @@ async function transformSectionsAndPage(args: {
|
||||
}
|
||||
return spaceURL + path;
|
||||
} else {
|
||||
return getAbsoluteHref(path);
|
||||
return getAbsoluteHref(ctx, path);
|
||||
}
|
||||
};
|
||||
|
||||
const sections = await Promise.all(
|
||||
item.sections?.map<Promise<ComputedSectionResult>>(async (section) => ({
|
||||
const sections =
|
||||
item.sections?.map<ComputedSectionResult>((section) => ({
|
||||
type: 'section',
|
||||
id: item.id + '/' + section.id,
|
||||
title: section.title,
|
||||
href: await getURL(section.path, spaceURL),
|
||||
href: getURL(section.path, spaceURL),
|
||||
body: section.body,
|
||||
})) ?? [],
|
||||
);
|
||||
})) ?? [];
|
||||
|
||||
const page: ComputedPageResult = {
|
||||
type: 'page',
|
||||
id: item.id,
|
||||
title: item.title,
|
||||
href: await getURL(item.path, spaceURL),
|
||||
href: getURL(item.path, spaceURL),
|
||||
spaceTitle: space?.title,
|
||||
};
|
||||
|
||||
return [page, sections];
|
||||
}
|
||||
|
||||
async function transformSitePageResult(item: SearchPageResult, siteSpace?: SiteSpace) {
|
||||
const [page, sections] = await transformSectionsAndPage({
|
||||
function transformSitePageResult(
|
||||
ctx: GitBookContext,
|
||||
item: SearchPageResult,
|
||||
siteSpace?: SiteSpace,
|
||||
) {
|
||||
const [page, sections] = transformSectionsAndPage(ctx, {
|
||||
item,
|
||||
space: siteSpace?.space,
|
||||
spaceURL: siteSpace?.urls.published,
|
||||
@@ -337,8 +345,8 @@ async function transformSitePageResult(item: SearchPageResult, siteSpace?: SiteS
|
||||
return [page, ...sections];
|
||||
}
|
||||
|
||||
async function transformPageResult(item: SearchPageResult, space?: Space) {
|
||||
const [page, sections] = await transformSectionsAndPage({
|
||||
function transformPageResult(ctx: GitBookContext, item: SearchPageResult, space?: Space) {
|
||||
const [page, sections] = transformSectionsAndPage(ctx, {
|
||||
item,
|
||||
space,
|
||||
spaceURL: space?.urls.published ?? space?.urls.app,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { CustomizationThemedURL } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { Image } from '@/components/utils';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getAbsoluteHref } from '@/lib/links';
|
||||
|
||||
import { Emoji } from '../primitives';
|
||||
@@ -14,6 +16,7 @@ export async function SpaceIcon(
|
||||
'sources'
|
||||
>,
|
||||
) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { icon, emoji, alt, ...imageProps } = props;
|
||||
|
||||
if (emoji && !icon) {
|
||||
@@ -37,14 +40,16 @@ export async function SpaceIcon(
|
||||
}
|
||||
: {
|
||||
light: {
|
||||
src: await getAbsoluteHref(
|
||||
src: getAbsoluteHref(
|
||||
ctx,
|
||||
'~gitbook/icon?size=medium&theme=light',
|
||||
true,
|
||||
),
|
||||
size: { width: 256, height: 256 },
|
||||
},
|
||||
dark: {
|
||||
src: await getAbsoluteHref(
|
||||
src: getAbsoluteHref(
|
||||
ctx,
|
||||
'~gitbook/icon?size=medium&theme=dark',
|
||||
true,
|
||||
),
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
SiteCustomizationSettings,
|
||||
Space,
|
||||
} from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
import React from 'react';
|
||||
|
||||
import { Footer } from '@/components/Footer';
|
||||
@@ -19,10 +20,10 @@ import { TableOfContents } from '@/components/TableOfContents';
|
||||
import { getSpaceLanguage } from '@/intl/server';
|
||||
import { t } from '@/intl/translate';
|
||||
import { api, ContentTarget, type SectionsList, SiteContentPointer } from '@/lib/api';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { ContentRefContext } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
import { shouldTrackEvents } from '@/lib/tracking';
|
||||
import { getCurrentVisitorToken } from '@/lib/visitor-token';
|
||||
|
||||
import { SpacesDropdown } from '../Header/SpacesDropdown';
|
||||
import { InsightsProvider } from '../Insights';
|
||||
@@ -42,6 +43,7 @@ export async function SpaceLayout(props: {
|
||||
ancestors: Array<RevisionPageDocument | RevisionPageGroup>;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const {
|
||||
space,
|
||||
contentTarget,
|
||||
@@ -73,9 +75,9 @@ export async function SpaceLayout(props: {
|
||||
'sidebar' in customization.styling &&
|
||||
customization.styling.sidebar.background === CustomizationSidebarBackgroundStyle.Filled,
|
||||
};
|
||||
const apiHost = (await api()).client.endpoint;
|
||||
const visitorAuthToken = await getCurrentVisitorToken();
|
||||
const enabled = await shouldTrackEvents();
|
||||
const apiHost = api(ctx).client.endpoint;
|
||||
const visitorAuthToken = ctx.visitorToken;
|
||||
const enabled = shouldTrackEvents(ctx);
|
||||
|
||||
return (
|
||||
<InsightsProvider
|
||||
@@ -176,6 +178,7 @@ export async function SpaceLayout(props: {
|
||||
|
||||
<React.Suspense fallback={null}>
|
||||
<SearchModal
|
||||
ctx={ctx}
|
||||
spaceId={contentTarget.spaceId}
|
||||
revisionId={contentTarget.revisionId}
|
||||
spaceTitle={customization.title ?? space.title}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { RevisionPage, RevisionPageDocument, RevisionPageGroup } from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { getPageHref } from '@/lib/links';
|
||||
import { getPagePath } from '@/lib/pages';
|
||||
import { ContentRefContext } from '@/lib/references';
|
||||
@@ -15,8 +17,9 @@ export async function PageDocumentItem(props: {
|
||||
ancestors: Array<RevisionPageDocument | RevisionPageGroup>;
|
||||
context: ContentRefContext;
|
||||
}) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { rootPages, page, ancestors, context } = props;
|
||||
const href = await getPageHref(rootPages, page);
|
||||
const href = await getPageHref(ctx, rootPages, page);
|
||||
|
||||
return (
|
||||
<li className={tcls('flex', 'flex-col')}>
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import { RevisionPageLink } from '@gitbook/api';
|
||||
import { Icon } from '@gitbook/icons';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { Link } from '@/components/primitives';
|
||||
import { getGitBookContextFromHeaders } from '@/lib/gitbook-context';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { TOCPageIcon } from './TOCPageIcon';
|
||||
|
||||
export async function PageLinkItem(props: { page: RevisionPageLink; context: ContentRefContext }) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
const { page, context } = props;
|
||||
|
||||
const resolved = await resolveContentRef(page.target, context);
|
||||
const resolved = await resolveContentRef(ctx, page.target, context);
|
||||
|
||||
return (
|
||||
<li className={tcls('flex', 'flex-col')}>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* eslint-disable @next/next/no-img-element */
|
||||
import { headers } from 'next/headers';
|
||||
import ReactDOM from 'react-dom';
|
||||
|
||||
import { getGitBookContextFromHeaders, GitBookContext } from '@/lib/gitbook-context';
|
||||
import { checkIsHttpURL, getImageSize, getResizedImageURLFactory } from '@/lib/images';
|
||||
import { ClassValue, tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -191,6 +193,8 @@ async function ImagePictureSized(
|
||||
} & ImageCommonProps
|
||||
>,
|
||||
) {
|
||||
const ctx = getGitBookContextFromHeaders(await headers());
|
||||
|
||||
const {
|
||||
source,
|
||||
sizes,
|
||||
@@ -210,7 +214,7 @@ async function ImagePictureSized(
|
||||
throw new Error('You must provide at least one size for the image.');
|
||||
}
|
||||
|
||||
const attrs = await getImageAttributes({ sizes, source, quality, resize });
|
||||
const attrs = await getImageAttributes(ctx, { sizes, source, quality, resize });
|
||||
const canBeFetched = checkIsHttpURL(attrs.src);
|
||||
const fetchPriority = canBeFetched ? getFetchPriority(priority) : undefined;
|
||||
const loading = priority === 'lazy' ? 'lazy' : undefined;
|
||||
@@ -243,12 +247,15 @@ async function ImagePictureSized(
|
||||
* Get the attributes for an image.
|
||||
* src, srcSet, sizes, width, height, etc.
|
||||
*/
|
||||
async function getImageAttributes(params: {
|
||||
sizes: ImageResponsiveSize[];
|
||||
source: ImageSourceSized;
|
||||
quality: number;
|
||||
resize: boolean;
|
||||
}): Promise<{
|
||||
async function getImageAttributes(
|
||||
ctx: GitBookContext,
|
||||
params: {
|
||||
sizes: ImageResponsiveSize[];
|
||||
source: ImageSourceSized;
|
||||
quality: number;
|
||||
resize: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
src: string;
|
||||
srcSet?: string;
|
||||
sizes?: string;
|
||||
@@ -258,7 +265,7 @@ async function getImageAttributes(params: {
|
||||
const { sizes, source, quality, resize } = params;
|
||||
let src = source.src;
|
||||
|
||||
const getURL = resize ? await getResizedImageURLFactory(source.src) : null;
|
||||
const getURL = resize ? getResizedImageURLFactory(ctx, source.src) : null;
|
||||
|
||||
if (!getURL) {
|
||||
return {
|
||||
|
||||
+168
-141
@@ -32,6 +32,7 @@ import {
|
||||
noCacheFetchOptions,
|
||||
parseCacheResponse,
|
||||
} from './cache';
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
import { defaultCustomizationForSpace } from './utils';
|
||||
|
||||
/**
|
||||
@@ -110,16 +111,14 @@ export const DEFAULT_API_ENDPOINT = process.env.GITBOOK_API_URL ?? 'https://api.
|
||||
/**
|
||||
* Create a new API client with a token.
|
||||
*/
|
||||
export async function apiWithToken(
|
||||
export function apiWithToken(
|
||||
apiToken: string,
|
||||
contextId: string | undefined,
|
||||
): Promise<GitBookAPIContext> {
|
||||
const headersList = await headers();
|
||||
const apiEndpoint = headersList.get('x-gitbook-api') ?? DEFAULT_API_ENDPOINT;
|
||||
|
||||
ctx: GitBookContext,
|
||||
): GitBookAPIContext {
|
||||
const gitbook = new GitBookAPI({
|
||||
authToken: apiToken,
|
||||
endpoint: apiEndpoint,
|
||||
endpoint: ctx.apiEndpoint,
|
||||
userAgent: userAgent(),
|
||||
});
|
||||
|
||||
@@ -129,23 +128,19 @@ export async function apiWithToken(
|
||||
/**
|
||||
* Create an API client for the current request.
|
||||
*/
|
||||
export async function api(): Promise<GitBookAPIContext> {
|
||||
export function api(ctx: GitBookContext): GitBookAPIContext {
|
||||
const existing = apiSyncStorage.getStore();
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
const headersList = await headers();
|
||||
const apiToken = headersList.get('x-gitbook-token');
|
||||
const contextId = headersList.get('x-gitbook-token-context') ?? undefined;
|
||||
|
||||
if (!apiToken) {
|
||||
if (!ctx.apiToken) {
|
||||
throw new Error(
|
||||
'Missing GitBook API token, please check that the request is correctly processed by the middleware',
|
||||
);
|
||||
}
|
||||
|
||||
return apiWithToken(apiToken, contextId);
|
||||
return apiWithToken(ctx.apiToken, ctx.apiTokenContextId ?? undefined, ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,15 +172,14 @@ export type PublishedContentWithCache =
|
||||
*/
|
||||
export const getUserById = cache({
|
||||
name: 'api.getUserById',
|
||||
tag: (userId) =>
|
||||
tag: (_ctx, userId) =>
|
||||
getAPICacheTag({
|
||||
tag: 'user',
|
||||
user: userId,
|
||||
}),
|
||||
get: async (userId: string, options: CacheFunctionOptions) => {
|
||||
get: async (ctx: GitBookContext, userId: string, options: CacheFunctionOptions) => {
|
||||
try {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.users.getUserById(userId, {
|
||||
const response = await api(ctx).client.users.getUserById(userId, {
|
||||
signal: options.signal,
|
||||
...noCacheFetchOptions,
|
||||
});
|
||||
@@ -210,12 +204,13 @@ export const getUserById = cache({
|
||||
*/
|
||||
export const getPublishedContentByUrl = cache({
|
||||
name: 'api.getPublishedContentByUrl.v4',
|
||||
tag: (url) =>
|
||||
tag: (_ctx, url) =>
|
||||
getAPICacheTag({
|
||||
tag: 'url',
|
||||
hostname: new URL(url).hostname,
|
||||
}),
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
url: string,
|
||||
visitorAuthToken: string | undefined,
|
||||
// Prefer undefined for a better cache key.
|
||||
@@ -223,8 +218,7 @@ export const getPublishedContentByUrl = cache({
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
try {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.urls.getPublishedContentByUrl(
|
||||
const response = await api(ctx).client.urls.getPublishedContentByUrl(
|
||||
{
|
||||
url,
|
||||
visitorAuthToken,
|
||||
@@ -272,10 +266,14 @@ export const getPublishedContentByUrl = cache({
|
||||
*/
|
||||
export const getSpace = cache({
|
||||
name: 'api.getSpace',
|
||||
tag: (spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
get: async (spaceId: string, shareKey: string | undefined, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getSpaceById(
|
||||
tag: (_ctx, spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
shareKey: string | undefined,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const response = await api(ctx).client.spaces.getSpaceById(
|
||||
spaceId,
|
||||
{
|
||||
shareKey,
|
||||
@@ -296,14 +294,22 @@ export const getSpace = cache({
|
||||
*/
|
||||
export const getChangeRequest = cache({
|
||||
name: 'api.getChangeRequest',
|
||||
tag: (spaceId, changeRequestId) =>
|
||||
tag: (_ctx, spaceId, changeRequestId) =>
|
||||
getAPICacheTag({ tag: 'change-request', space: spaceId, changeRequest: changeRequestId }),
|
||||
get: async (spaceId: string, changeRequestId: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getChangeRequestById(spaceId, changeRequestId, {
|
||||
...noCacheFetchOptions,
|
||||
signal: options.signal,
|
||||
});
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
changeRequestId: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const response = await api(ctx).client.spaces.getChangeRequestById(
|
||||
spaceId,
|
||||
changeRequestId,
|
||||
{
|
||||
...noCacheFetchOptions,
|
||||
signal: options.signal,
|
||||
},
|
||||
);
|
||||
return cacheResponse(response, {
|
||||
ttl: 60 * 60,
|
||||
revalidateBefore: 10 * 60,
|
||||
@@ -321,27 +327,22 @@ interface GetRevisionOptions {
|
||||
metadata: boolean;
|
||||
}
|
||||
|
||||
const getAPIContextId = async () => {
|
||||
const apiCtx = await api();
|
||||
return apiCtx.contextId;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a revision by its ID.
|
||||
*/
|
||||
export const getRevision = cache({
|
||||
name: 'api.getRevision.v2',
|
||||
tag: (spaceId, revisionId) =>
|
||||
tag: (_ctx, spaceId, revisionId) =>
|
||||
getAPICacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
revisionId: string,
|
||||
fetchOptions: GetRevisionOptions,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getRevisionById(
|
||||
const response = await api(ctx).client.spaces.getRevisionById(
|
||||
spaceId,
|
||||
revisionId,
|
||||
{
|
||||
@@ -363,17 +364,17 @@ export const getRevision = cache({
|
||||
*/
|
||||
export const getRevisionPages = cache({
|
||||
name: 'api.getRevisionPages.v4',
|
||||
tag: (spaceId, revisionId) =>
|
||||
tag: (_ctx, spaceId, revisionId) =>
|
||||
getAPICacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
revisionId: string,
|
||||
fetchOptions: GetRevisionOptions,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.listPagesInRevisionById(
|
||||
const response = await api(ctx).client.spaces.listPagesInRevisionById(
|
||||
spaceId,
|
||||
revisionId,
|
||||
{
|
||||
@@ -398,10 +399,11 @@ export const getRevisionPages = cache({
|
||||
*/
|
||||
export const getRevisionPageByPath = cache({
|
||||
name: 'api.getRevisionPageByPath.v3',
|
||||
tag: (spaceId, revisionId) =>
|
||||
tag: (_ctx, spaceId, revisionId) =>
|
||||
getAPICacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
revisionId: string,
|
||||
pagePath: string,
|
||||
@@ -410,8 +412,7 @@ export const getRevisionPageByPath = cache({
|
||||
const encodedPath = encodeURIComponent(pagePath);
|
||||
|
||||
try {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getPageInRevisionByPath(
|
||||
const response = await api(ctx).client.spaces.getPageInRevisionByPath(
|
||||
spaceId,
|
||||
revisionId,
|
||||
encodedPath,
|
||||
@@ -444,17 +445,17 @@ export const getRevisionPageByPath = cache({
|
||||
*/
|
||||
const getRevisionFileById = cache({
|
||||
name: 'api.getRevisionFile.v3',
|
||||
tag: (spaceId, revisionId) =>
|
||||
tag: (_ctx, spaceId, revisionId) =>
|
||||
getAPICacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
revisionId: string,
|
||||
fileId: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
try {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getFileInRevisionById(
|
||||
const response = await api(ctx).client.spaces.getFileInRevisionById(
|
||||
spaceId,
|
||||
revisionId,
|
||||
fileId,
|
||||
@@ -480,18 +481,18 @@ const getRevisionFileById = cache({
|
||||
|
||||
const getRevisionReusableContentById = cache({
|
||||
name: 'api.getRevisionReusableContentById.v1',
|
||||
tag: (spaceId, revisionId) =>
|
||||
tag: (_ctx, spaceId, revisionId) =>
|
||||
getAPICacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
revisionId: string,
|
||||
reusableContentId: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
try {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getReusableContentInRevisionById(
|
||||
const response = await api(ctx).client.spaces.getReusableContentInRevisionById(
|
||||
spaceId,
|
||||
revisionId,
|
||||
reusableContentId,
|
||||
@@ -521,13 +522,17 @@ const getRevisionReusableContentById = cache({
|
||||
*/
|
||||
const getRevisionAllFiles = cache({
|
||||
name: 'api.getRevisionAllFiles.v2',
|
||||
tag: (spaceId, revisionId) =>
|
||||
tag: (_ctx, spaceId, revisionId) =>
|
||||
getAPICacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
|
||||
get: async (spaceId: string, revisionId: string, options: CacheFunctionOptions) => {
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
revisionId: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const response = await getAll(
|
||||
async (params) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.listFilesInRevisionById(
|
||||
const response = await api(ctx).client.spaces.listFilesInRevisionById(
|
||||
spaceId,
|
||||
revisionId,
|
||||
{
|
||||
@@ -561,35 +566,39 @@ const getRevisionAllFiles = cache({
|
||||
* The approach is optimized to use the entire list of files in the revision if it has been fetched
|
||||
* or to use a per-file approach if not.
|
||||
*/
|
||||
export const getRevisionFile = batch<[string, string, string], RevisionFile | null>(
|
||||
export const getRevisionFile = batch<[GitBookContext, string, string, string], RevisionFile | null>(
|
||||
async (executions) => {
|
||||
const [spaceId, revisionId] = executions[0];
|
||||
const [ctx, spaceId, revisionId] = executions[0];
|
||||
|
||||
const hasRevisionInMemory = await getRevision.hasInMemory(spaceId, revisionId, {
|
||||
const hasRevisionInMemory = await getRevision.hasInMemory(ctx, spaceId, revisionId, {
|
||||
metadata: false,
|
||||
});
|
||||
const hasRevisionFilesInMemory = await getRevisionAllFiles.hasInMemory(spaceId, revisionId);
|
||||
const hasRevisionFilesInMemory = await getRevisionAllFiles.hasInMemory(
|
||||
ctx,
|
||||
spaceId,
|
||||
revisionId,
|
||||
);
|
||||
|
||||
// When fetching more than 5 files, we should bundle them all into one call to get the entire revision
|
||||
if (executions.length > 5 || hasRevisionFilesInMemory || hasRevisionInMemory) {
|
||||
let files: Record<string, RevisionFile> = {};
|
||||
|
||||
if (hasRevisionInMemory) {
|
||||
const revision = await getRevision(spaceId, revisionId, { metadata: false });
|
||||
const revision = await getRevision(ctx, spaceId, revisionId, { metadata: false });
|
||||
files = {};
|
||||
revision.files.forEach((file) => {
|
||||
files[file.id] = file;
|
||||
});
|
||||
} else {
|
||||
files = await getRevisionAllFiles(spaceId, revisionId);
|
||||
files = await getRevisionAllFiles(ctx, spaceId, revisionId);
|
||||
}
|
||||
|
||||
return executions.map(([spaceId, revisionId, fileId]) => files[fileId] ?? null);
|
||||
return executions.map(([ctx, spaceId, revisionId, fileId]) => files[fileId] ?? null);
|
||||
} else {
|
||||
// Fetch file individually
|
||||
return Promise.all(
|
||||
executions.map(([spaceId, revisionId, fileId]) =>
|
||||
getRevisionFileById(spaceId, revisionId, fileId),
|
||||
executions.map(([ctx, spaceId, revisionId, fileId]) =>
|
||||
getRevisionFileById(ctx, spaceId, revisionId, fileId),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -597,13 +606,13 @@ export const getRevisionFile = batch<[string, string, string], RevisionFile | nu
|
||||
{
|
||||
delay: 20,
|
||||
groupBy: (spaceId, revisionId) => spaceId + '/' + revisionId,
|
||||
skip: async (spaceId, revisionId, fileId) => {
|
||||
skip: async (ctx, spaceId, revisionId, fileId) => {
|
||||
return (
|
||||
(await getRevision.hasInMemory(spaceId, revisionId, {
|
||||
(await getRevision.hasInMemory(ctx, spaceId, revisionId, {
|
||||
metadata: false,
|
||||
})) ||
|
||||
(await getRevisionAllFiles.hasInMemory(spaceId, revisionId)) ||
|
||||
(await getRevisionFileById.hasInMemory(spaceId, revisionId, fileId))
|
||||
(await getRevisionAllFiles.hasInMemory(ctx, spaceId, revisionId)) ||
|
||||
(await getRevisionFileById.hasInMemory(ctx, spaceId, revisionId, fileId))
|
||||
);
|
||||
},
|
||||
},
|
||||
@@ -613,23 +622,24 @@ export const getRevisionFile = batch<[string, string, string], RevisionFile | nu
|
||||
* Get reusable content in a revision.
|
||||
*/
|
||||
export const getReusableContent = async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
revisionId: string,
|
||||
reusableContentId: string,
|
||||
): Promise<RevisionReusableContent | null> => {
|
||||
const hasRevisionInMemory = await getRevision.hasInMemory(spaceId, revisionId, {
|
||||
const hasRevisionInMemory = await getRevision.hasInMemory(ctx, spaceId, revisionId, {
|
||||
metadata: false,
|
||||
});
|
||||
|
||||
if (hasRevisionInMemory) {
|
||||
const revision = await getRevision(spaceId, revisionId, { metadata: false });
|
||||
const revision = await getRevision(ctx, spaceId, revisionId, { metadata: false });
|
||||
return (
|
||||
revision.reusableContents.find(
|
||||
(reusableContent) => reusableContent.id === reusableContentId,
|
||||
) ?? null
|
||||
);
|
||||
} else {
|
||||
return getRevisionReusableContentById(spaceId, revisionId, reusableContentId);
|
||||
return getRevisionReusableContentById(ctx, spaceId, revisionId, reusableContentId);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -638,12 +648,16 @@ export const getReusableContent = async (
|
||||
*/
|
||||
export const getDocument = cache({
|
||||
name: 'api.getDocument.v2',
|
||||
tag: (spaceId, documentId) =>
|
||||
tag: (_ctx, spaceId, documentId) =>
|
||||
getAPICacheTag({ tag: 'document', space: spaceId, document: documentId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
get: async (spaceId: string, documentId: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getDocumentById(
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
documentId: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const response = await api(ctx).client.spaces.getDocumentById(
|
||||
spaceId,
|
||||
documentId,
|
||||
{
|
||||
@@ -674,9 +688,10 @@ function validateSiteRedirectSource(source: string) {
|
||||
*/
|
||||
export const getSiteRedirectBySource = cache({
|
||||
name: 'api.getSiteRedirectBySource',
|
||||
tag: ({ siteId }) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
tag: (_ctx, { siteId }) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
args: {
|
||||
organizationId: string;
|
||||
siteId: string;
|
||||
@@ -694,8 +709,7 @@ export const getSiteRedirectBySource = cache({
|
||||
};
|
||||
}
|
||||
try {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.orgs.getSiteRedirectBySource(
|
||||
const response = await api(ctx).client.orgs.getSiteRedirectBySource(
|
||||
args.organizationId,
|
||||
args.siteId,
|
||||
{
|
||||
@@ -735,11 +749,15 @@ export const getSiteRedirectBySource = cache({
|
||||
*/
|
||||
export const getSite = cache({
|
||||
name: 'api.getSite',
|
||||
tag: (organizationId, siteId) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
get: async (organizationId: string, siteId: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.orgs.getSiteById(organizationId, siteId, {
|
||||
tag: (_ctx, _organizationId, siteId) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
organizationId: string,
|
||||
siteId: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const response = await api(ctx).client.orgs.getSiteById(organizationId, siteId, {
|
||||
...noCacheFetchOptions,
|
||||
signal: options.signal,
|
||||
});
|
||||
@@ -754,9 +772,10 @@ export const getSite = cache({
|
||||
*/
|
||||
export const getPublishedContentSite = cache({
|
||||
name: 'api.getPublishedContentSite',
|
||||
tag: ({ siteId }) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
tag: (_ctx, { siteId }) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
args: {
|
||||
organizationId: string;
|
||||
siteId: string /** Site share key that can be used as context to resolve site space published urls */;
|
||||
@@ -764,8 +783,7 @@ export const getPublishedContentSite = cache({
|
||||
},
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.orgs.getPublishedContentSite(
|
||||
const response = await api(ctx).client.orgs.getPublishedContentSite(
|
||||
args.organizationId,
|
||||
args.siteId,
|
||||
{
|
||||
@@ -813,6 +831,7 @@ function parseSiteSectionsList(siteSectionId: string, sections: SiteSection[]) {
|
||||
* experience for the site (structure, customizations, scripts etc)
|
||||
*/
|
||||
export async function getSiteData(
|
||||
ctx: GitBookContext,
|
||||
pointer: Pick<
|
||||
SiteContentPointer,
|
||||
'organizationId' | 'siteId' | 'siteSectionId' | 'siteSpaceId' | 'siteShareKey'
|
||||
@@ -823,7 +842,7 @@ export async function getSiteData(
|
||||
structure: siteStructure,
|
||||
customizations,
|
||||
scripts,
|
||||
} = await getPublishedContentSite({
|
||||
} = await getPublishedContentSite(ctx, {
|
||||
organizationId: pointer.organizationId,
|
||||
siteId: pointer.siteId,
|
||||
siteShareKey: pointer.siteShareKey,
|
||||
@@ -850,7 +869,8 @@ export async function getSiteData(
|
||||
const spaces =
|
||||
siteSpaces ?? (sections ? parseSpacesFromSiteSpaces(sections.section.siteSpaces) : []);
|
||||
|
||||
const customization = await getActiveCustomizationSettings(
|
||||
const customization = getActiveCustomizationSettings(
|
||||
ctx,
|
||||
pointer.siteSpaceId ? customizations.siteSpaces[pointer.siteSpaceId] : customizations.site,
|
||||
);
|
||||
|
||||
@@ -867,13 +887,12 @@ export async function getSiteData(
|
||||
/**
|
||||
* Get the customization settings for a space from the API.
|
||||
*/
|
||||
export async function getSpaceCustomization(): Promise<{
|
||||
export function getSpaceCustomization(ctx: GitBookContext): {
|
||||
customization: CustomizationSettings;
|
||||
}> {
|
||||
const headersList = await headers();
|
||||
} {
|
||||
const raw = defaultCustomizationForSpace();
|
||||
|
||||
const extend = headersList.get('x-gitbook-customization');
|
||||
const extend = ctx.customization;
|
||||
if (extend) {
|
||||
try {
|
||||
const parsed = rison.decode_object<Partial<CustomizationSettings>>(extend);
|
||||
@@ -897,10 +916,9 @@ export async function getSpaceCustomization(): Promise<{
|
||||
*/
|
||||
export const getCollection = cache({
|
||||
name: 'api.getCollection',
|
||||
tag: (collectionId) => getAPICacheTag({ tag: 'collection', collection: collectionId }),
|
||||
get: async (collectionId: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.collections.getCollectionById(collectionId, {
|
||||
tag: (_ctx, collectionId) => getAPICacheTag({ tag: 'collection', collection: collectionId }),
|
||||
get: async (ctx: GitBookContext, collectionId: string, options: CacheFunctionOptions) => {
|
||||
const response = await api(ctx).client.collections.getCollectionById(collectionId, {
|
||||
...noCacheFetchOptions,
|
||||
signal: options.signal,
|
||||
});
|
||||
@@ -915,11 +933,10 @@ export const getCollection = cache({
|
||||
*/
|
||||
export const getCollectionSpaces = cache({
|
||||
name: 'api.getCollectionSpaces',
|
||||
tag: (collectionId) => getAPICacheTag({ tag: 'collection', collection: collectionId }),
|
||||
get: async (collectionId: string, options: CacheFunctionOptions) => {
|
||||
tag: (_ctx, collectionId) => getAPICacheTag({ tag: 'collection', collection: collectionId }),
|
||||
get: async (ctx: GitBookContext, collectionId: string, options: CacheFunctionOptions) => {
|
||||
const response = await getAll(async (params) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.collections.listSpacesInCollectionById(
|
||||
const response = await api(ctx).client.collections.listSpacesInCollectionById(
|
||||
collectionId,
|
||||
params,
|
||||
{
|
||||
@@ -945,19 +962,22 @@ export const getCollectionSpaces = cache({
|
||||
* instead of calling the individual functions.
|
||||
*/
|
||||
export async function getSpaceContentData(
|
||||
ctx: GitBookContext,
|
||||
pointer: SpaceContentPointer,
|
||||
shareKey: string | undefined,
|
||||
) {
|
||||
const [space, changeRequest] = await Promise.all([
|
||||
getSpace(pointer.spaceId, shareKey),
|
||||
pointer.changeRequestId ? getChangeRequest(pointer.spaceId, pointer.changeRequestId) : null,
|
||||
getSpace(ctx, pointer.spaceId, shareKey),
|
||||
pointer.changeRequestId
|
||||
? getChangeRequest(ctx, pointer.spaceId, pointer.changeRequestId)
|
||||
: null,
|
||||
]);
|
||||
|
||||
const contentTarget: ContentTarget = {
|
||||
spaceId: pointer.spaceId,
|
||||
revisionId: changeRequest?.revision ?? pointer.revisionId ?? space.revision,
|
||||
};
|
||||
const pages = await getRevisionPages(space.id, contentTarget.revisionId, {
|
||||
const pages = await getRevisionPages(ctx, space.id, contentTarget.revisionId, {
|
||||
// We only care about the Git metadata when the Git sync is enabled
|
||||
// otherwise we can optimize performance by not fetching it
|
||||
metadata: !!space.gitSync,
|
||||
@@ -975,17 +995,17 @@ export async function getSpaceContentData(
|
||||
*/
|
||||
export const searchSpaceContent = cache({
|
||||
name: 'api.searchSpaceContent',
|
||||
tag: (spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
tag: (_ctx, spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
/** The revision ID is used as a cache bust key, to avoid revalidating lot of cache entries by tags */
|
||||
revisionId: string,
|
||||
query: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.searchSpaceContent(
|
||||
const response = await api(ctx).client.spaces.searchSpaceContent(
|
||||
spaceId,
|
||||
{ query },
|
||||
{
|
||||
@@ -1002,11 +1022,15 @@ export const searchSpaceContent = cache({
|
||||
*/
|
||||
export const searchParentContent = cache({
|
||||
name: 'api.searchParentContent',
|
||||
tag: (spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
get: async (parentId: string, query: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.search.searchContent(
|
||||
tag: (_ctx, spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
parentId: string,
|
||||
query: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const response = await api(ctx).client.search.searchContent(
|
||||
{ query },
|
||||
{
|
||||
...noCacheFetchOptions,
|
||||
@@ -1024,9 +1048,10 @@ export const searchParentContent = cache({
|
||||
*/
|
||||
export const searchSiteContent = cache({
|
||||
name: 'api.searchSiteContent',
|
||||
tag: (organizationId, siteId) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: getAPIContextId,
|
||||
tag: (_ctx, _organizationId, siteId) => getAPICacheTag({ tag: 'site', site: siteId }),
|
||||
getKeySuffix: (ctx) => api(ctx).contextId,
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
organizationId: string,
|
||||
siteId: string,
|
||||
query: string,
|
||||
@@ -1038,8 +1063,7 @@ export const searchSiteContent = cache({
|
||||
cacheBust?: string,
|
||||
options?: CacheFunctionOptions,
|
||||
) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.orgs.searchSiteContent(
|
||||
const response = await api(ctx).client.orgs.searchSiteContent(
|
||||
organizationId,
|
||||
siteId,
|
||||
{
|
||||
@@ -1064,10 +1088,9 @@ export const searchSiteContent = cache({
|
||||
*/
|
||||
export const getRecommendedQuestionsInSpace = cache({
|
||||
name: 'api.getRecommendedQuestionsInSpace',
|
||||
tag: (spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
get: async (spaceId: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getRecommendedQuestionsInSpace(spaceId, {
|
||||
tag: (_ctx, spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
get: async (ctx: GitBookContext, spaceId: string, options: CacheFunctionOptions) => {
|
||||
const response = await api(ctx).client.spaces.getRecommendedQuestionsInSpace(spaceId, {
|
||||
...noCacheFetchOptions,
|
||||
signal: options.signal,
|
||||
});
|
||||
@@ -1080,14 +1103,15 @@ export const getRecommendedQuestionsInSpace = cache({
|
||||
*/
|
||||
export const renderIntegrationUi = cache({
|
||||
name: 'api.renderIntegrationUi',
|
||||
tag: (integrationName) => getAPICacheTag({ tag: 'integration', integration: integrationName }),
|
||||
tag: (_ctx, integrationName) =>
|
||||
getAPICacheTag({ tag: 'integration', integration: integrationName }),
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
integrationName: string,
|
||||
request: RequestRenderIntegrationUI,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.integrations.renderIntegrationUiWithPost(
|
||||
const response = await api(ctx).client.integrations.renderIntegrationUiWithPost(
|
||||
integrationName,
|
||||
request,
|
||||
{
|
||||
@@ -1105,9 +1129,8 @@ export const renderIntegrationUi = cache({
|
||||
*/
|
||||
export const getEmbedByUrl = cache({
|
||||
name: 'api.getEmbedByUrl',
|
||||
get: async (url: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.urls.getEmbedByUrl(
|
||||
get: async (ctx: GitBookContext, url: string, options: CacheFunctionOptions) => {
|
||||
const response = await api(ctx).client.urls.getEmbedByUrl(
|
||||
{ url },
|
||||
{
|
||||
...noCacheFetchOptions,
|
||||
@@ -1123,10 +1146,14 @@ export const getEmbedByUrl = cache({
|
||||
*/
|
||||
export const getEmbedByUrlInSpace = cache({
|
||||
name: 'api.getEmbedByUrlInSpace',
|
||||
tag: (spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
get: async (spaceId: string, url: string, options: CacheFunctionOptions) => {
|
||||
const apiCtx = await api();
|
||||
const response = await apiCtx.client.spaces.getEmbedByUrlInSpace(
|
||||
tag: (_ctx, spaceId) => getAPICacheTag({ tag: 'space', space: spaceId }),
|
||||
get: async (
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
url: string,
|
||||
options: CacheFunctionOptions,
|
||||
) => {
|
||||
const response = await api(ctx).client.spaces.getEmbedByUrlInSpace(
|
||||
spaceId,
|
||||
{ url },
|
||||
{
|
||||
@@ -1297,11 +1324,11 @@ async function getAll<T, E>(
|
||||
* Selects the customization settings from the x-gitbook-customization header if present,
|
||||
* otherwise returns the original API-provided settings.
|
||||
*/
|
||||
async function getActiveCustomizationSettings(
|
||||
function getActiveCustomizationSettings(
|
||||
ctx: GitBookContext,
|
||||
settings: SiteCustomizationSettings,
|
||||
): Promise<SiteCustomizationSettings> {
|
||||
const headersList = await headers();
|
||||
const extend = headersList.get('x-gitbook-customization');
|
||||
): SiteCustomizationSettings {
|
||||
const extend = ctx.customization;
|
||||
if (extend) {
|
||||
try {
|
||||
const parsedSettings = rison.decode_object<SiteCustomizationSettings>(extend);
|
||||
|
||||
+6
-6
@@ -74,7 +74,7 @@ describe('cache', () => {
|
||||
describe('cache with suffix key', () => {
|
||||
const impl = mock((arg: string) => 'test-' + arg);
|
||||
const getKeySuffixImpl: Mock<NonNullable<CacheDefinition<[string], string>['getKeySuffix']>> =
|
||||
mock(async () => hash({ test: 1 }));
|
||||
mock(() => hash({ test: 1 }));
|
||||
|
||||
let fn: CacheFunction<[string], string>;
|
||||
let testId = 0;
|
||||
@@ -112,7 +112,7 @@ describe('cache with suffix key', () => {
|
||||
|
||||
expect(impl).toHaveBeenCalledTimes(2);
|
||||
|
||||
getKeySuffixImpl.mockImplementation(async () => hash({ test: 2 }));
|
||||
getKeySuffixImpl.mockImplementation(() => hash({ test: 2 }));
|
||||
expect(await fn('a')).toEqual('test-a');
|
||||
|
||||
expect(impl).toHaveBeenCalledTimes(3);
|
||||
@@ -120,7 +120,7 @@ describe('cache with suffix key', () => {
|
||||
|
||||
it('should preserve behaviour even when the returned key suffix is undefined', async () => {
|
||||
// Start with the returned suffix being undefined
|
||||
getKeySuffixImpl.mockImplementation(async () => undefined);
|
||||
getKeySuffixImpl.mockImplementation(() => undefined);
|
||||
const result = await fn('a');
|
||||
expect(result).toEqual('test-a');
|
||||
|
||||
@@ -128,19 +128,19 @@ describe('cache with suffix key', () => {
|
||||
expect(impl).toHaveBeenCalledTimes(1);
|
||||
|
||||
// The returned suffix changes so we should get the value computed by the function
|
||||
getKeySuffixImpl.mockImplementation(async () => hash({ test: 1 }));
|
||||
getKeySuffixImpl.mockImplementation(() => hash({ test: 1 }));
|
||||
expect(await fn('a')).toEqual('test-a');
|
||||
|
||||
expect(impl).toHaveBeenCalledTimes(2);
|
||||
|
||||
// The returned suffix is undefined again so we should get the value from a previous cache entry
|
||||
getKeySuffixImpl.mockImplementation(async () => undefined);
|
||||
getKeySuffixImpl.mockImplementation(() => undefined);
|
||||
expect(await fn('a')).toEqual('test-a');
|
||||
|
||||
expect(impl).toHaveBeenCalledTimes(2);
|
||||
|
||||
// The returned suffix goes back to a previous hash so we should the value from a previous cache entry
|
||||
getKeySuffixImpl.mockImplementation(async () => hash({ test: 1 }));
|
||||
getKeySuffixImpl.mockImplementation(() => hash({ test: 1 }));
|
||||
expect(await fn('a')).toEqual('test-a');
|
||||
|
||||
expect(impl).toHaveBeenCalledTimes(2);
|
||||
|
||||
+4
-4
@@ -57,7 +57,7 @@ export interface CacheDefinition<Args extends any[], Result> {
|
||||
getKeyArgs?: (args: Args) => any[];
|
||||
|
||||
/** Returns a precomputed hash that is used alongside arguments to generate the cache key */
|
||||
getKeySuffix?: () => Promise<string | undefined>;
|
||||
getKeySuffix?: (...args: Args) => string | undefined;
|
||||
|
||||
/** Default ttl (in seconds) */
|
||||
defaultTtl?: number;
|
||||
@@ -245,7 +245,7 @@ export function cache<Args extends any[], Result>(
|
||||
const [args, { signal }] = extractCacheFunctionOptions<Args>(rawArgs);
|
||||
|
||||
const cacheArgs = cacheDef.getKeyArgs ? cacheDef.getKeyArgs(args) : args;
|
||||
const cacheKeySuffix = cacheDef.getKeySuffix ? await cacheDef.getKeySuffix() : undefined;
|
||||
const cacheKeySuffix = cacheDef.getKeySuffix ? cacheDef.getKeySuffix(...args) : undefined;
|
||||
const key = getCacheKey(cacheDef.name, cacheArgs, cacheKeySuffix);
|
||||
|
||||
return await trace(
|
||||
@@ -263,7 +263,7 @@ export function cache<Args extends any[], Result>(
|
||||
cacheFn.revalidate = async (...rawArgs: Args | [...Args, CacheFunctionOptions]) => {
|
||||
const [args, { signal }] = extractCacheFunctionOptions<Args>(rawArgs);
|
||||
const cacheArgs = cacheDef.getKeyArgs ? cacheDef.getKeyArgs(args) : args;
|
||||
const cacheKeySuffix = cacheDef.getKeySuffix ? await cacheDef.getKeySuffix() : undefined;
|
||||
const cacheKeySuffix = cacheDef.getKeySuffix ? cacheDef.getKeySuffix(...args) : undefined;
|
||||
const key = getCacheKey(cacheDef.name, cacheArgs, cacheKeySuffix);
|
||||
|
||||
const result = await revalidate(key, signal, ...args);
|
||||
@@ -272,7 +272,7 @@ export function cache<Args extends any[], Result>(
|
||||
|
||||
cacheFn.hasInMemory = async (...args: Args) => {
|
||||
const cacheArgs = cacheDef.getKeyArgs ? cacheDef.getKeyArgs(args) : args;
|
||||
const cacheKeySuffix = cacheDef.getKeySuffix ? await cacheDef.getKeySuffix() : undefined;
|
||||
const cacheKeySuffix = cacheDef.getKeySuffix ? cacheDef.getKeySuffix(...args) : undefined;
|
||||
const key = getCacheKey(cacheDef.name, cacheArgs, cacheKeySuffix);
|
||||
const tag = cacheDef.tag?.(...args);
|
||||
|
||||
|
||||
@@ -3,18 +3,17 @@ import { merge } from 'content-security-policy-merger';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { assetsDomain } from './assets';
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
import { filterOutNullable } from './typescript';
|
||||
/**
|
||||
* Get the current nonce for the current request.
|
||||
*/
|
||||
export async function getContentSecurityPolicyNonce(): Promise<string> {
|
||||
const headersList = await headers();
|
||||
const nonce = headersList.get('x-nonce');
|
||||
if (!nonce) {
|
||||
export function getContentSecurityPolicyNonce(ctx: GitBookContext): string {
|
||||
if (!ctx.nonce) {
|
||||
throw new Error('No nonce found in headers');
|
||||
}
|
||||
|
||||
return nonce;
|
||||
return ctx.nonce;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { DEFAULT_API_ENDPOINT } from './api';
|
||||
import { formatBasePath } from './links';
|
||||
|
||||
export type GitBookContext = {
|
||||
theme: string | null;
|
||||
nonce: string | null;
|
||||
visitorToken: string | null;
|
||||
trackPageViews: boolean;
|
||||
apiEndpoint: string;
|
||||
apiToken: string | null;
|
||||
apiTokenContextId: string | null;
|
||||
customization: string | null;
|
||||
host: string;
|
||||
basePath: string;
|
||||
protocol: string;
|
||||
originBasePath: string;
|
||||
|
||||
// Content pointers
|
||||
spaceId: string | null;
|
||||
siteId: string | null;
|
||||
organizationId: string | null;
|
||||
siteSpaceId: string | null;
|
||||
siteSectionId: string | null;
|
||||
siteShareKey: string | null;
|
||||
contentRevisionId: string | null;
|
||||
changeRequestId: string | null;
|
||||
|
||||
// Indexation
|
||||
searchIndexation: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Extract the gitbook context from the headers.
|
||||
*/
|
||||
export function getGitBookContextFromHeaders(headers: Headers): GitBookContext {
|
||||
return {
|
||||
theme: headers.get('x-gitbook-theme'),
|
||||
nonce: headers.get('x-nonce'),
|
||||
visitorToken: headers.get('x-gitbook-visitor-token'),
|
||||
trackPageViews: headers.has('x-gitbook-track-page-views'),
|
||||
apiEndpoint: headers.get('x-gitbook-api') ?? DEFAULT_API_ENDPOINT,
|
||||
apiToken: headers.get('x-gitbook-token'),
|
||||
apiTokenContextId: headers.get('x-gitbook-token-context'),
|
||||
customization: headers.get('x-gitbook-customization'),
|
||||
basePath: formatBasePath(headers.get('x-gitbook-basepath')),
|
||||
host: headers.get('x-gitbook-host') ?? headers.get('host') ?? '',
|
||||
protocol: headers.get('x-forwarded-proto') ?? 'https',
|
||||
originBasePath: headers.get('x-gitbook-origin-basepath') ?? '/',
|
||||
spaceId: headers.get('x-gitbook-content-space'),
|
||||
siteId: headers.get('x-gitbook-content-site'),
|
||||
organizationId: headers.get('x-gitbook-content-organization'),
|
||||
siteSpaceId: headers.get('x-gitbook-content-site-space'),
|
||||
siteSectionId: headers.get('x-gitbook-content-site-section'),
|
||||
siteShareKey: headers.get('x-gitbook-content-site-share-key'),
|
||||
contentRevisionId: headers.get('x-gitbook-content-revision'),
|
||||
changeRequestId: headers.get('x-gitbook-content-changerequest'),
|
||||
searchIndexation: headers.has('x-gitbook-search-indexation'),
|
||||
};
|
||||
}
|
||||
|
||||
export type IpAndUserAgent = {
|
||||
ip: string;
|
||||
userAgent: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read the IP and User-Agent from the headers.
|
||||
* This function can only be called at the top level of a component or route.
|
||||
*/
|
||||
export function getIpAndUserAgentFromHeaders(headers: Headers): IpAndUserAgent {
|
||||
const ip =
|
||||
headers.get('x-gitbook-ipv4') ??
|
||||
headers.get('x-gitbook-ip') ??
|
||||
headers.get('cf-pseudo-ipv4') ??
|
||||
headers.get('cf-connecting-ip') ??
|
||||
headers.get('x-forwarded-for') ??
|
||||
'';
|
||||
const userAgent = headers.get('user-agent') ?? '';
|
||||
|
||||
return { ip, userAgent };
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import 'server-only';
|
||||
import fnv1a from '@sindresorhus/fnv1a';
|
||||
import type { MaybePromise } from 'p-map';
|
||||
|
||||
import { getHost } from './links';
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
|
||||
/**
|
||||
* GitBook has supported different version of image signing in the past. To maintain backwards
|
||||
@@ -19,12 +19,14 @@ export const CURRENT_SIGNATURE_VERSION: SignatureVersion = '2';
|
||||
/**
|
||||
* A mapping of signature versions to signature functions.
|
||||
*/
|
||||
const IMAGE_SIGNATURE_FUNCTIONS: Record<SignatureVersion, (input: string) => MaybePromise<string>> =
|
||||
{
|
||||
'0': generateSignatureV0,
|
||||
'1': generateSignatureV1,
|
||||
'2': generateSignatureV2,
|
||||
};
|
||||
const IMAGE_SIGNATURE_FUNCTIONS: Record<
|
||||
SignatureVersion,
|
||||
(ctx: GitBookContext, input: string) => MaybePromise<string>
|
||||
> = {
|
||||
'0': generateSignatureV0,
|
||||
'1': generateSignatureV1,
|
||||
'2': generateSignatureV2,
|
||||
};
|
||||
|
||||
export function isSignatureVersion(input: string): input is SignatureVersion {
|
||||
return Object.keys(IMAGE_SIGNATURE_FUNCTIONS).includes(input);
|
||||
@@ -34,11 +36,12 @@ export function isSignatureVersion(input: string): input is SignatureVersion {
|
||||
* Verify a signature of an image URL
|
||||
*/
|
||||
export async function verifyImageSignature(
|
||||
ctx: GitBookContext,
|
||||
input: string,
|
||||
{ signature, version }: { signature: string; version: SignatureVersion },
|
||||
): Promise<boolean> {
|
||||
const generator = IMAGE_SIGNATURE_FUNCTIONS[version];
|
||||
const generated = await generator(input);
|
||||
const generated = await generator(ctx, input);
|
||||
return generated === signature;
|
||||
}
|
||||
|
||||
@@ -48,11 +51,14 @@ export async function verifyImageSignature(
|
||||
* This function is sync. If you need to implement an async version of image signing, you'll need to change
|
||||
* ths signature of this fn and where it's used.
|
||||
*/
|
||||
export async function generateImageSignature(input: string): Promise<{
|
||||
export function generateImageSignature(
|
||||
ctx: GitBookContext,
|
||||
input: string,
|
||||
): {
|
||||
signature: string;
|
||||
version: SignatureVersion;
|
||||
}> {
|
||||
const result = await generateSignatureV2(input);
|
||||
} {
|
||||
const result = generateSignatureV2(ctx, input);
|
||||
return { signature: result, version: CURRENT_SIGNATURE_VERSION };
|
||||
}
|
||||
|
||||
@@ -63,8 +69,8 @@ const fnv1aUtf8Buffer = new Uint8Array(512);
|
||||
* Generate a signature for an image.
|
||||
* The signature is relative to the current site being rendered to avoid serving images from other sites on the same domain.
|
||||
*/
|
||||
async function generateSignatureV2(input: string): Promise<string> {
|
||||
const hostName = await getHost();
|
||||
function generateSignatureV2(ctx: GitBookContext, input: string): string {
|
||||
const hostName = ctx.host;
|
||||
const all = [
|
||||
input,
|
||||
hostName, // The hostname is used to avoid serving images from other sites on the same domain
|
||||
@@ -83,7 +89,7 @@ const fnv1aUtf8BufferV1 = new Uint8Array(512);
|
||||
* When setting it in a URL, we use version '1' for the 'sv' querystring parameneter
|
||||
* to know that it was the algorithm that was used.
|
||||
*/
|
||||
function generateSignatureV1(input: string): string {
|
||||
function generateSignatureV1(ctx: GitBookContext, input: string): string {
|
||||
const all = [input, process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
|
||||
return fnv1a(all, { utf8Buffer: fnv1aUtf8BufferV1 }).toString(16);
|
||||
}
|
||||
@@ -93,7 +99,7 @@ function generateSignatureV1(input: string): string {
|
||||
* We still need it to validate older signatures that were generated without versioning
|
||||
* but still exist in previously generated and cached content.
|
||||
*/
|
||||
async function generateSignatureV0(input: string): Promise<string> {
|
||||
async function generateSignatureV0(ctx: GitBookContext, input: string): Promise<string> {
|
||||
const all = [input, process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
|
||||
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(all));
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'server-only';
|
||||
|
||||
import { noCacheFetchOptions } from '@/lib/cache/http';
|
||||
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
import { generateImageSignature } from './image-signatures';
|
||||
import { getRootUrl } from './links';
|
||||
import { getImageAPIUrl } from './urls';
|
||||
@@ -84,17 +85,16 @@ interface ResizeImageOptions {
|
||||
/**
|
||||
* Create a function to get resized image URLs for a given image URL.
|
||||
*/
|
||||
export async function getResizedImageURLFactory(
|
||||
export function getResizedImageURLFactory(
|
||||
ctx: GitBookContext,
|
||||
input: string,
|
||||
): Promise<((options: ResizeImageOptions) => string) | null> {
|
||||
): ((options: ResizeImageOptions) => string) | null {
|
||||
if (!checkIsSizableImageURL(input)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [{ signature, version }, rootUrl] = await Promise.all([
|
||||
generateImageSignature(input),
|
||||
getRootUrl(),
|
||||
]);
|
||||
const { signature, version } = generateImageSignature(ctx, input);
|
||||
const rootUrl = getRootUrl(ctx);
|
||||
|
||||
return (options) => {
|
||||
const url = new URL('/~gitbook/image', rootUrl);
|
||||
@@ -124,11 +124,12 @@ export async function getResizedImageURLFactory(
|
||||
* Create a new URL for an image with resized parameters.
|
||||
* The URL is signed and verified by the server.
|
||||
*/
|
||||
export async function getResizedImageURL(
|
||||
export function getResizedImageURL(
|
||||
ctx: GitBookContext,
|
||||
input: string,
|
||||
options: ResizeImageOptions,
|
||||
): Promise<string> {
|
||||
const factory = await getResizedImageURLFactory(input);
|
||||
): string {
|
||||
const factory = getResizedImageURLFactory(ctx, input);
|
||||
return factory?.(options) ?? input;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
import { getPagePath } from './pages';
|
||||
|
||||
export interface PageHrefContext {
|
||||
@@ -22,9 +23,8 @@ export interface PageHrefContext {
|
||||
* Return the base path for the current request.
|
||||
* The value will start and finish with /
|
||||
*/
|
||||
export async function getBasePath(): Promise<string> {
|
||||
const headersList = await headers();
|
||||
let path = headersList.get('x-gitbook-basepath') ?? '/';
|
||||
export function formatBasePath(headerBasePath: string | null): string {
|
||||
let path = headerBasePath ?? '/';
|
||||
|
||||
if (!path.startsWith('/')) {
|
||||
path = '/' + path;
|
||||
@@ -37,24 +37,15 @@ export async function getBasePath(): Promise<string> {
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current host for the current request.
|
||||
*/
|
||||
export async function getHost(): Promise<string> {
|
||||
const headersList = await headers();
|
||||
return headersList.get('x-gitbook-host') ?? headersList.get('host') ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the root URL for the GitBook Open instance (not the content).
|
||||
* Use `baseUrl` to get the base URL for the current content.
|
||||
*
|
||||
* The URL will end with "/".
|
||||
*/
|
||||
export async function getRootUrl(): Promise<string> {
|
||||
const [headersList, host] = await Promise.all([headers(), getHost()]);
|
||||
const protocol = headersList.get('x-forwarded-proto') ?? 'https';
|
||||
let path = headersList.get('x-gitbook-origin-basepath') ?? '/';
|
||||
export function getRootUrl(ctx: GitBookContext): string {
|
||||
const protocol = ctx.protocol;
|
||||
let path = ctx.originBasePath;
|
||||
|
||||
if (!path.startsWith('/')) {
|
||||
path = '/' + path;
|
||||
@@ -64,24 +55,26 @@ export async function getRootUrl(): Promise<string> {
|
||||
path = path + '/';
|
||||
}
|
||||
|
||||
return `${protocol}://${host}${path}`;
|
||||
return `${protocol}://${ctx.host}${path}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base URL for the current content.
|
||||
* The URL will end with "/".
|
||||
*/
|
||||
export async function getBaseUrl(): Promise<string> {
|
||||
const [headersList, host, basePath] = await Promise.all([headers(), getHost(), getBasePath()]);
|
||||
const protocol = headersList.get('x-forwarded-proto') ?? 'https';
|
||||
return `${protocol}://${host}${basePath}`;
|
||||
export function getBaseUrl(ctx: GitBookContext): string {
|
||||
return `${ctx.protocol}://${ctx.host}${ctx.basePath}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an absolute href in the current content.
|
||||
*/
|
||||
export async function getAbsoluteHref(href: string, withHost: boolean = false): Promise<string> {
|
||||
const base = withHost ? await getBaseUrl() : await getBasePath();
|
||||
export function getAbsoluteHref(
|
||||
ctx: GitBookContext,
|
||||
href: string,
|
||||
withHost: boolean = false,
|
||||
): string {
|
||||
const base = withHost ? getBaseUrl(ctx) : ctx.basePath;
|
||||
return `${base}${href.startsWith('/') ? href.slice(1) : href}`;
|
||||
}
|
||||
|
||||
@@ -98,13 +91,14 @@ export function getGitbookAppHref(pathname: string): string {
|
||||
/**
|
||||
* Create a link to a page path in the current space.
|
||||
*/
|
||||
export async function getPageHref(
|
||||
export function getPageHref(
|
||||
ctx: GitBookContext,
|
||||
rootPages: RevisionPage[],
|
||||
page: RevisionPageDocument | RevisionPageGroup,
|
||||
context: PageHrefContext = {},
|
||||
/** Anchor to link to in the page. */
|
||||
anchor?: string,
|
||||
): Promise<string> {
|
||||
): string {
|
||||
const { pdf } = context;
|
||||
|
||||
if (pdf) {
|
||||
@@ -120,9 +114,7 @@ export async function getPageHref(
|
||||
}
|
||||
}
|
||||
|
||||
const href =
|
||||
(await getAbsoluteHref(getPagePath(rootPages, page))) + (anchor ? '#' + anchor : '');
|
||||
return href;
|
||||
return getAbsoluteHref(ctx, getPagePath(rootPages, page)) + (anchor ? '#' + anchor : '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { SiteContentPointer, SpaceContentPointer } from './api';
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
|
||||
/**
|
||||
* Get the current site content pointer from the headers
|
||||
*/
|
||||
export async function getSiteContentPointer(): Promise<SiteContentPointer> {
|
||||
const headersList = await headers();
|
||||
const spaceId = headersList.get('x-gitbook-content-space');
|
||||
const siteId = headersList.get('x-gitbook-content-site');
|
||||
const organizationId = headersList.get('x-gitbook-content-organization');
|
||||
const siteSpaceId = headersList.get('x-gitbook-content-site-space');
|
||||
const siteSectionId = headersList.get('x-gitbook-content-site-section');
|
||||
const siteShareKey = headersList.get('x-gitbook-content-site-share-key');
|
||||
export function getSiteContentPointer(ctx: GitBookContext): SiteContentPointer {
|
||||
const { siteId, spaceId, organizationId, siteSectionId, siteSpaceId, siteShareKey } = ctx;
|
||||
|
||||
if (!spaceId || !siteId || !organizationId) {
|
||||
throw new Error(
|
||||
@@ -27,8 +20,8 @@ export async function getSiteContentPointer(): Promise<SiteContentPointer> {
|
||||
siteSpaceId: siteSpaceId ?? undefined,
|
||||
siteShareKey: siteShareKey ?? undefined,
|
||||
organizationId,
|
||||
revisionId: headersList.get('x-gitbook-content-revision') ?? undefined,
|
||||
changeRequestId: headersList.get('x-gitbook-content-changerequest') ?? undefined,
|
||||
revisionId: ctx.contentRevisionId ?? undefined,
|
||||
changeRequestId: ctx.changeRequestId ?? undefined,
|
||||
};
|
||||
|
||||
return pointer;
|
||||
@@ -38,9 +31,8 @@ export async function getSiteContentPointer(): Promise<SiteContentPointer> {
|
||||
* Get the current space pointer from the headers. This should be used when rendering
|
||||
* the space in an isolated context (e.g. PDF generation).
|
||||
*/
|
||||
export async function getSpacePointer(): Promise<SpaceContentPointer> {
|
||||
const headersList = await headers();
|
||||
const spaceId = headersList.get('x-gitbook-content-space');
|
||||
export function getSpacePointer(ctx: GitBookContext): SpaceContentPointer {
|
||||
const spaceId = ctx.spaceId;
|
||||
if (!spaceId) {
|
||||
throw new Error(
|
||||
'getSpacePointer is called outside the scope of a request processed by the middleware',
|
||||
@@ -49,8 +41,8 @@ export async function getSpacePointer(): Promise<SpaceContentPointer> {
|
||||
|
||||
const pointer: SpaceContentPointer = {
|
||||
spaceId,
|
||||
revisionId: headersList.get('x-gitbook-content-revision') ?? undefined,
|
||||
changeRequestId: headersList.get('x-gitbook-content-changerequest') ?? undefined,
|
||||
revisionId: ctx.contentRevisionId ?? undefined,
|
||||
changeRequestId: ctx.changeRequestId ?? undefined,
|
||||
};
|
||||
|
||||
return pointer;
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
parseSpacesFromSiteSpaces,
|
||||
} from './api';
|
||||
import { getBlockById, getBlockTitle } from './document';
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
import { getGitbookAppHref, getPageHref, PageHrefContext } from './links';
|
||||
import { getPagePath, resolvePageId } from './pages';
|
||||
import { ClassValue } from './tailwind';
|
||||
@@ -100,6 +101,7 @@ export interface ResolveContentRefOptions {
|
||||
* Resolve a content reference to be rendered.
|
||||
*/
|
||||
export async function resolveContentRef(
|
||||
ctx: GitBookContext,
|
||||
contentRef: ContentRef,
|
||||
context: ContentRefContext,
|
||||
options: ResolveContentRefOptions = {},
|
||||
@@ -117,7 +119,7 @@ export async function resolveContentRef(
|
||||
}
|
||||
|
||||
case 'file': {
|
||||
const file = await getRevisionFile(space.id, revisionId, contentRef.file);
|
||||
const file = await getRevisionFile(ctx, space.id, revisionId, contentRef.file);
|
||||
if (file) {
|
||||
return {
|
||||
href: file.downloadURL,
|
||||
@@ -133,7 +135,7 @@ export async function resolveContentRef(
|
||||
case 'anchor':
|
||||
case 'page': {
|
||||
if (contentRef.space && contentRef.space !== space.id) {
|
||||
return resolveContentRefInSpace(contentRef.space, siteContext, contentRef);
|
||||
return resolveContentRefInSpace(ctx, contentRef.space, siteContext, contentRef);
|
||||
}
|
||||
|
||||
const resolvePageResult =
|
||||
@@ -162,7 +164,7 @@ export async function resolveContentRef(
|
||||
|
||||
if (resolveAnchorText) {
|
||||
const document = page.documentId
|
||||
? await getDocument(space.id, page.documentId)
|
||||
? await getDocument(ctx, space.id, page.documentId)
|
||||
: null;
|
||||
if (document) {
|
||||
const block = getBlockById(document, anchor);
|
||||
@@ -193,7 +195,7 @@ export async function resolveContentRef(
|
||||
}
|
||||
} else {
|
||||
// Page in the current content
|
||||
href = await getPageHref(pages, page, linksContext, anchor);
|
||||
href = await getPageHref(ctx, pages, page, linksContext, anchor);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -209,7 +211,7 @@ export async function resolveContentRef(
|
||||
const targetSpace =
|
||||
contentRef.space === space.id
|
||||
? space
|
||||
: await getBestTargetSpace(contentRef.space, siteContext);
|
||||
: await getBestTargetSpace(ctx, contentRef.space, siteContext);
|
||||
|
||||
if (!targetSpace) {
|
||||
return {
|
||||
@@ -227,7 +229,7 @@ export async function resolveContentRef(
|
||||
}
|
||||
|
||||
case 'user': {
|
||||
const user = await getUserById(contentRef.user);
|
||||
const user = await getUserById(ctx, contentRef.user);
|
||||
if (user) {
|
||||
return {
|
||||
href: `mailto:${user.email}`,
|
||||
@@ -250,7 +252,7 @@ export async function resolveContentRef(
|
||||
}
|
||||
|
||||
case 'collection': {
|
||||
const collection = await ignoreAPIError(getCollection(contentRef.collection));
|
||||
const collection = await ignoreAPIError(getCollection(ctx, contentRef.collection));
|
||||
if (!collection) {
|
||||
return {
|
||||
href: getGitbookAppHref(`/s/${contentRef.collection}`),
|
||||
@@ -268,6 +270,7 @@ export async function resolveContentRef(
|
||||
|
||||
case 'reusable-content': {
|
||||
const reusableContent = await getReusableContent(
|
||||
ctx,
|
||||
space.id,
|
||||
revisionId,
|
||||
contentRef.reusableContent,
|
||||
@@ -293,16 +296,21 @@ export async function resolveContentRef(
|
||||
* It will try to return the space in the site context if it exists to avoid cross-site links.
|
||||
*/
|
||||
async function getBestTargetSpace(
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
siteContext: SiteContentPointer | null,
|
||||
): Promise<Space | undefined> {
|
||||
const [fetchedSpace, publishedContentSite] = await Promise.all([
|
||||
ignoreAPIError(
|
||||
getSpace(spaceId, siteContext?.siteShareKey ? siteContext.siteShareKey : undefined),
|
||||
getSpace(
|
||||
ctx,
|
||||
spaceId,
|
||||
siteContext?.siteShareKey ? siteContext.siteShareKey : undefined,
|
||||
),
|
||||
),
|
||||
siteContext
|
||||
? ignoreAPIError(
|
||||
getPublishedContentSite({
|
||||
getPublishedContentSite(ctx, {
|
||||
organizationId: siteContext.organizationId,
|
||||
siteId: siteContext.siteId,
|
||||
siteShareKey: siteContext.siteShareKey,
|
||||
@@ -333,6 +341,7 @@ async function getBestTargetSpace(
|
||||
}
|
||||
|
||||
async function resolveContentRefInSpace(
|
||||
ctx: GitBookContext,
|
||||
spaceId: string,
|
||||
siteContext: SiteContentPointer | null,
|
||||
contentRef: ContentRef,
|
||||
@@ -342,8 +351,8 @@ async function resolveContentRefInSpace(
|
||||
};
|
||||
|
||||
const [result, bestTargetSpace] = await Promise.all([
|
||||
ignoreAPIError(getSpaceContentData(pointer, siteContext?.siteShareKey)),
|
||||
getBestTargetSpace(spaceId, siteContext),
|
||||
ignoreAPIError(getSpaceContentData(ctx, pointer, siteContext?.siteShareKey)),
|
||||
getBestTargetSpace(ctx, spaceId, siteContext),
|
||||
]);
|
||||
if (!result) {
|
||||
return null;
|
||||
@@ -358,7 +367,7 @@ async function resolveContentRefInSpace(
|
||||
baseUrl += '/';
|
||||
}
|
||||
|
||||
const resolved = await resolveContentRef(contentRef, {
|
||||
const resolved = await resolveContentRef(ctx, contentRef, {
|
||||
siteContext,
|
||||
space,
|
||||
revisionId: space.revision,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
Collection,
|
||||
ContentVisibility,
|
||||
RevisionPageDocument,
|
||||
RevisionPageGroup,
|
||||
@@ -7,7 +6,8 @@ import {
|
||||
SiteVisibility,
|
||||
Space,
|
||||
} from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
|
||||
/**
|
||||
* Return true if a page is indexable in search.
|
||||
@@ -31,21 +31,16 @@ export function isPageIndexable(
|
||||
/**
|
||||
* Return true if a space should be indexed by search engines.
|
||||
*/
|
||||
export async function isSpaceIndexable({ space, site }: { space: Space; site: Site | null }) {
|
||||
const headersList = await headers();
|
||||
|
||||
if (
|
||||
process.env.GITBOOK_BLOCK_SEARCH_INDEXATION &&
|
||||
!headersList.has('x-gitbook-search-indexation')
|
||||
) {
|
||||
export function isSpaceIndexable(
|
||||
ctx: GitBookContext,
|
||||
{ space, site }: { space: Space; site: Site | null },
|
||||
) {
|
||||
if (process.env.GITBOOK_BLOCK_SEARCH_INDEXATION && !ctx.searchIndexation) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prevent indexation of preview of revisions / change-requests
|
||||
if (
|
||||
headersList.get('x-gitbook-content-revision') ||
|
||||
headersList.get('x-gitbook-content-changerequest')
|
||||
) {
|
||||
if (ctx.contentRevisionId || ctx.changeRequestId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import { headers } from 'next/headers';
|
||||
import { GitBookContext } from './gitbook-context';
|
||||
|
||||
/**
|
||||
* Return true if events should be tracked on the site.
|
||||
*/
|
||||
export async function shouldTrackEvents(): Promise<boolean> {
|
||||
const headersList = await headers();
|
||||
|
||||
export function shouldTrackEvents(ctx: GitBookContext): boolean {
|
||||
if (
|
||||
process.env.NODE_ENV === 'development' ||
|
||||
(process.env.GITBOOK_BLOCK_PAGE_VIEWS_TRACKING &&
|
||||
!headersList.has('x-gitbook-track-page-views'))
|
||||
(process.env.GITBOOK_BLOCK_PAGE_VIEWS_TRACKING && !ctx.trackPageViews)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -90,15 +90,6 @@ export function normalizeVisitorAuthURL(url: URL): URL {
|
||||
return withoutVAParam;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the visitor token from the request context.
|
||||
*/
|
||||
export async function getCurrentVisitorToken(): Promise<string | null> {
|
||||
const headersList = await headers();
|
||||
const visitorToken = headersList.get('x-gitbook-visitor-token');
|
||||
return visitorToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all possible basePaths for a given URL. This is used to find the visitor
|
||||
* authentication cookie token.
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
normalizeVisitorAuthURL,
|
||||
} from '@/lib/visitor-token';
|
||||
|
||||
import { getGitBookContextFromHeaders, GitBookContext } from './lib/gitbook-context';
|
||||
import { waitUntil } from './lib/waitUntil';
|
||||
|
||||
export const config = {
|
||||
@@ -96,6 +97,7 @@ export type LookupResult = PublishedContentWithCache & {
|
||||
* The middleware also takes care of persisting the visitor authentication state.
|
||||
*/
|
||||
export async function middleware(request: NextRequest) {
|
||||
const ctx = getGitBookContextFromHeaders(request.headers);
|
||||
const { url, mode } = getInputURL(request);
|
||||
|
||||
setTag('url', url.toString());
|
||||
@@ -128,7 +130,7 @@ export async function middleware(request: NextRequest) {
|
||||
}),
|
||||
contextId: undefined,
|
||||
},
|
||||
() => lookupSiteForURL(mode, request, inputURL),
|
||||
() => lookupSiteForURL(ctx, mode, request, inputURL),
|
||||
);
|
||||
if ('error' in resolved) {
|
||||
return new NextResponse(resolved.error.message, {
|
||||
@@ -182,7 +184,7 @@ export async function middleware(request: NextRequest) {
|
||||
async () => {
|
||||
const [siteData] = await Promise.all([
|
||||
'site' in resolved
|
||||
? getSiteData({
|
||||
? getSiteData(ctx, {
|
||||
organizationId: resolved.organization,
|
||||
siteId: resolved.site,
|
||||
siteSectionId: resolved.siteSection,
|
||||
@@ -194,6 +196,7 @@ export async function middleware(request: NextRequest) {
|
||||
// the cache will handle concurrent calls
|
||||
waitUntil(
|
||||
getSpaceContentData(
|
||||
ctx,
|
||||
{
|
||||
spaceId: resolved.space,
|
||||
changeRequestId: resolved.changeRequest,
|
||||
@@ -367,25 +370,26 @@ function getInputURL(request: NextRequest): {
|
||||
}
|
||||
|
||||
async function lookupSiteForURL(
|
||||
ctx: GitBookContext,
|
||||
mode: URLLookupMode,
|
||||
request: NextRequest,
|
||||
url: URL,
|
||||
): Promise<LookupResult> {
|
||||
switch (mode) {
|
||||
case 'single': {
|
||||
return await lookupSiteInSingleMode(url);
|
||||
return lookupSiteInSingleMode(ctx, url);
|
||||
}
|
||||
case 'multi': {
|
||||
return await lookupSiteInMultiMode(request, url);
|
||||
return await lookupSiteInMultiMode(ctx, request, url);
|
||||
}
|
||||
case 'multi-path': {
|
||||
return await lookupSiteInMultiPathMode(request, url);
|
||||
return await lookupSiteInMultiPathMode(ctx, request, url);
|
||||
}
|
||||
case 'multi-id': {
|
||||
return await lookupSiteOrSpaceInMultiIdMode(request, url);
|
||||
return await lookupSiteOrSpaceInMultiIdMode(ctx, request, url);
|
||||
}
|
||||
case 'proxy':
|
||||
return await lookupSiteInProxy(request, url);
|
||||
return await lookupSiteInProxy(ctx, request, url);
|
||||
default:
|
||||
assertNever(mode);
|
||||
}
|
||||
@@ -395,7 +399,7 @@ async function lookupSiteForURL(
|
||||
* GITBOOK_MODE=single
|
||||
* When serving a single space, configured using GITBOOK_SPACE_ID and GITBOOK_TOKEN.
|
||||
*/
|
||||
async function lookupSiteInSingleMode(url: URL): Promise<LookupResult> {
|
||||
function lookupSiteInSingleMode(ctx: GitBookContext, url: URL): LookupResult {
|
||||
const spaceId = process.env.GITBOOK_SPACE_ID;
|
||||
if (!spaceId) {
|
||||
throw new Error(
|
||||
@@ -403,8 +407,7 @@ async function lookupSiteInSingleMode(url: URL): Promise<LookupResult> {
|
||||
);
|
||||
}
|
||||
|
||||
const apiCtx = await api();
|
||||
const apiToken = getDefaultAPIToken(apiCtx.client.endpoint);
|
||||
const apiToken = getDefaultAPIToken(api(ctx).client.endpoint);
|
||||
if (!apiToken) {
|
||||
throw new Error(
|
||||
`Missing GITBOOK_TOKEN environment variable. It should be passed when using GITBOOK_MODE=single.`,
|
||||
@@ -425,7 +428,11 @@ async function lookupSiteInSingleMode(url: URL): Promise<LookupResult> {
|
||||
* GITBOOK_MODE=proxy
|
||||
* When proxying a site on a different base URL.
|
||||
*/
|
||||
async function lookupSiteInProxy(request: NextRequest, url: URL): Promise<LookupResult> {
|
||||
async function lookupSiteInProxy(
|
||||
ctx: GitBookContext,
|
||||
request: NextRequest,
|
||||
url: URL,
|
||||
): Promise<LookupResult> {
|
||||
const rawSiteUrl = request.headers.get('x-gitbook-site-url');
|
||||
if (!rawSiteUrl) {
|
||||
throw new Error(
|
||||
@@ -436,16 +443,20 @@ async function lookupSiteInProxy(request: NextRequest, url: URL): Promise<Lookup
|
||||
const siteUrl = new URL(rawSiteUrl);
|
||||
siteUrl.pathname = joinPath(siteUrl.pathname, url.pathname);
|
||||
|
||||
return await lookupSiteInMultiMode(request, siteUrl);
|
||||
return await lookupSiteInMultiMode(ctx, request, siteUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* GITBOOK_MODE=multi
|
||||
* When serving multi spaces based on the current URL.
|
||||
*/
|
||||
async function lookupSiteInMultiMode(request: NextRequest, url: URL): Promise<LookupResult> {
|
||||
async function lookupSiteInMultiMode(
|
||||
ctx: GitBookContext,
|
||||
request: NextRequest,
|
||||
url: URL,
|
||||
): Promise<LookupResult> {
|
||||
const visitorAuthToken = getVisitorToken(request, url);
|
||||
const lookup = await lookupSiteByAPI(url, visitorAuthToken);
|
||||
const lookup = await lookupSiteByAPI(ctx, url, visitorAuthToken);
|
||||
return {
|
||||
...lookup,
|
||||
...('basePath' in lookup && visitorAuthToken
|
||||
@@ -465,6 +476,7 @@ async function lookupSiteInMultiMode(request: NextRequest, url: URL): Promise<Lo
|
||||
* - /~space|~site/:id/~revisions/:revisionId/:path
|
||||
*/
|
||||
async function lookupSiteOrSpaceInMultiIdMode(
|
||||
ctx: GitBookContext,
|
||||
request: NextRequest,
|
||||
url: URL,
|
||||
): Promise<LookupResult> {
|
||||
@@ -543,9 +555,8 @@ async function lookupSiteOrSpaceInMultiIdMode(
|
||||
// invalidated when trying to preview the site with different visitor
|
||||
// attributes.
|
||||
const contextId = decoded.claims ? hash(decoded.claims) : undefined;
|
||||
const apiCtx = await api();
|
||||
const gitbookAPI = new GitBookAPI({
|
||||
endpoint: apiEndpoint ?? apiCtx.client.endpoint,
|
||||
endpoint: apiEndpoint ?? api(ctx).client.endpoint,
|
||||
authToken: apiToken,
|
||||
userAgent: userAgent(),
|
||||
});
|
||||
@@ -554,7 +565,7 @@ async function lookupSiteOrSpaceInMultiIdMode(
|
||||
// (the cache is not dependend on the auth token, so it could leak data)
|
||||
if (source.kind === 'space') {
|
||||
await withAPI({ client: gitbookAPI, contextId }, () =>
|
||||
getSpace.revalidate(source.id, undefined),
|
||||
getSpace.revalidate(ctx, source.id, undefined),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -562,7 +573,7 @@ async function lookupSiteOrSpaceInMultiIdMode(
|
||||
// (the cache is not dependend on the auth token, so it could leak data)
|
||||
if (source.kind === 'site') {
|
||||
await withAPI({ client: gitbookAPI, contextId }, () =>
|
||||
getPublishedContentSite.revalidate({
|
||||
getPublishedContentSite.revalidate(ctx, {
|
||||
organizationId: decoded.organization,
|
||||
siteId: source.id,
|
||||
siteShareKey: undefined,
|
||||
@@ -612,7 +623,11 @@ async function lookupSiteOrSpaceInMultiIdMode(
|
||||
* GITBOOK_MODE=multi-path
|
||||
* When serving multi spaces with the url passed in the path.
|
||||
*/
|
||||
async function lookupSiteInMultiPathMode(request: NextRequest, url: URL): Promise<LookupResult> {
|
||||
async function lookupSiteInMultiPathMode(
|
||||
ctx: GitBookContext,
|
||||
request: NextRequest,
|
||||
url: URL,
|
||||
): Promise<LookupResult> {
|
||||
// Skip useless requests
|
||||
if (
|
||||
url.pathname === '/favicon.ico' ||
|
||||
@@ -651,7 +666,7 @@ async function lookupSiteInMultiPathMode(request: NextRequest, url: URL): Promis
|
||||
|
||||
const visitorAuthToken = getVisitorToken(request, target);
|
||||
|
||||
const lookup = await lookupSiteByAPI(target, visitorAuthToken);
|
||||
const lookup = await lookupSiteByAPI(ctx, target, visitorAuthToken);
|
||||
if ('error' in lookup) {
|
||||
return lookup;
|
||||
}
|
||||
@@ -689,6 +704,7 @@ async function lookupSiteInMultiPathMode(request: NextRequest, url: URL): Promis
|
||||
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
|
||||
*/
|
||||
async function lookupSiteByAPI(
|
||||
ctx: GitBookContext,
|
||||
lookupURL: URL,
|
||||
visitorTokenLookup: VisitorTokenLookup,
|
||||
): Promise<LookupResult> {
|
||||
@@ -706,6 +722,7 @@ async function lookupSiteByAPI(
|
||||
|
||||
const result = await race(lookup.urls, async (alternative, { signal }) => {
|
||||
const data = await getPublishedContentByUrl(
|
||||
ctx,
|
||||
alternative.url,
|
||||
visitorTokenLookup?.token,
|
||||
redirectOnError || undefined,
|
||||
|
||||
Reference in New Issue
Block a user