Refactor caching functions for site page metadata and viewport in PPR components

This commit is contained in:
Nicolas Dorseuil
2026-07-30 11:02:37 +02:00
parent 40d14b02d8
commit 23b438404c
3 changed files with 54 additions and 49 deletions
@@ -8,7 +8,7 @@ import {
PPRPageBody,
cachedGenerateSitePageMetadata,
cachedGenerateSitePageViewport,
} from '@/components/SitePage';
} from '@/components/SitePage/PPRSitePage';
import type { Metadata, Viewport } from 'next';
@@ -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 <SitePage context={context} pageParams={{ pathname: props.pathname }} staticRoute />;
}
// It looks like we cannot use use cache for this one.
export const cachedGenerateSitePageMetadata = (props: SitePageProps): (() => Promise<Metadata>) => {
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<Viewport> {
'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);
}
@@ -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<Viewport> {
'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<Viewport> {
const { customization } = context;
@@ -187,27 +162,6 @@ export async function generateSitePageViewport(context: GitBookSiteContext): Pro
};
}
export async function cachedGenerateSitePageMetadata(props: SitePageProps): Promise<Metadata> {
'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<Metadata> {
const { context, pageTarget, pageMetaLinks } = await getPageDataWithFallback({
context: props.context,