diff --git a/packages/gitbook/src/app/sites/ppr/[mode]/[siteURL]/[siteData]/[tocAPIToken]/[pageAPIToken]/(content)/[pagePath]/page.tsx b/packages/gitbook/src/app/sites/ppr/[mode]/[siteURL]/[siteData]/[tocAPIToken]/[pageAPIToken]/(content)/[pagePath]/page.tsx index 8a783c688..23373944a 100644 --- a/packages/gitbook/src/app/sites/ppr/[mode]/[siteURL]/[siteData]/[tocAPIToken]/[pageAPIToken]/(content)/[pagePath]/page.tsx +++ b/packages/gitbook/src/app/sites/ppr/[mode]/[siteURL]/[siteData]/[tocAPIToken]/[pageAPIToken]/(content)/[pagePath]/page.tsx @@ -8,7 +8,7 @@ import { PPRPageBody, cachedGenerateSitePageMetadata, cachedGenerateSitePageViewport, -} from '@/components/SitePage'; +} from '@/components/SitePage/PPRSitePage'; import type { Metadata, Viewport } from 'next'; diff --git a/packages/gitbook/src/components/SitePage/PPRSitePage.tsx b/packages/gitbook/src/components/SitePage/PPRSitePage.tsx index b804fe279..3747d2814 100644 --- a/packages/gitbook/src/components/SitePage/PPRSitePage.tsx +++ b/packages/gitbook/src/components/SitePage/PPRSitePage.tsx @@ -3,9 +3,16 @@ import { SpaceHeader, SpaceTableOfContents } from '@/components/SpaceLayout'; import { getCacheTag } from '@gitbook/cache-tags'; -import { cacheLife, cacheTag } from 'next/cache'; +import { cacheLife, cacheTag, unstable_cache } from 'next/cache'; -import { SitePage } from './SitePage'; +import type { GitBookSiteContext } from '@/lib/context'; +import type { Metadata, Viewport } from 'next'; +import { + SitePage, + type SitePageProps, + generateSitePageMetadata, + generateSitePageViewport, +} from './SitePage'; /** * Render the header from cache without carrying a request-scoped data fetcher into the cache key. @@ -92,3 +99,47 @@ export async function PPRPageBody(props: { params: RouteLayoutParams; pathname: return ; } + +// It looks like we cannot use use cache for this one. +export const cachedGenerateSitePageMetadata = (props: SitePageProps): (() => Promise) => { + return unstable_cache( + async () => generateSitePageMetadata(props), + ['cachedGenerateSitePageMetadata'], + { + revalidate: 60 * 60 * 24, // Revalidate every 24 hours + tags: [ + getCacheTag({ + tag: 'site', + site: props.context.site.id, + }), + getCacheTag({ + tag: 'space', + space: props.context.space.id, + }), + ], + } + ); +}; + +export async function cachedGenerateSitePageViewport( + context: GitBookSiteContext +): Promise { + 'use cache: remote'; + cacheLife('days'); // Cache for 1 day + + cacheTag( + getCacheTag({ + tag: 'site', + site: context.site.id, + }) + ); // Tag the cache entry for the metadata so it can be invalidated when the site changes + + cacheTag( + getCacheTag({ + tag: 'space', + space: context.space.id, + }) + ); // Tag the cache entry for the metadata so it can be invalidated when the space changes + + return generateSitePageViewport(context); +} diff --git a/packages/gitbook/src/components/SitePage/SitePage.tsx b/packages/gitbook/src/components/SitePage/SitePage.tsx index e61ae4946..18cca768a 100644 --- a/packages/gitbook/src/components/SitePage/SitePage.tsx +++ b/packages/gitbook/src/components/SitePage/SitePage.tsx @@ -1,5 +1,4 @@ import type { Metadata, Viewport } from 'next'; -import { cacheLife, cacheTag } from 'next/cache'; import { notFound, redirect } from 'next/navigation'; import { @@ -9,7 +8,6 @@ import { SiteInsightsDisplayContext, type TranslationLanguage, } from '@gitbook/api'; -import { getCacheTag } from '@gitbook/cache-tags'; import { IconsProvider } from '@gitbook/icons'; import { PageContextProvider } from '../PageContext'; @@ -148,29 +146,6 @@ export async function SitePage(props: SitePageProps & { staticRoute: boolean }) ); } -export async function cachedGenerateSitePageViewport( - context: GitBookSiteContext -): Promise { - 'use cache: remote'; - cacheLife('days'); // Cache for 1 day - - cacheTag( - getCacheTag({ - tag: 'site', - site: context.site.id, - }) - ); // Tag the cache entry for the metadata so it can be invalidated when the site changes - - cacheTag( - getCacheTag({ - tag: 'space', - space: context.space.id, - }) - ); // Tag the cache entry for the metadata so it can be invalidated when the space changes - - return generateSitePageViewport(context); -} - export async function generateSitePageViewport(context: GitBookSiteContext): Promise { const { customization } = context; @@ -187,27 +162,6 @@ export async function generateSitePageViewport(context: GitBookSiteContext): Pro }; } -export async function cachedGenerateSitePageMetadata(props: SitePageProps): Promise { - 'use cache: remote'; - cacheLife('days'); // Cache for 1 day - - cacheTag( - getCacheTag({ - tag: 'site', - site: props.context.site.id, - }) - ); // Tag the cache entry for the metadata so it can be invalidated when the site changes - - cacheTag( - getCacheTag({ - tag: 'space', - space: props.context.space.id, - }) - ); // Tag the cache entry for the metadata so it can be invalidated when the space changes - - return generateSitePageMetadata(props); -} - export async function generateSitePageMetadata(props: SitePageProps): Promise { const { context, pageTarget, pageMetaLinks } = await getPageDataWithFallback({ context: props.context,