Skip to content

Fix computed revision cache in v1 #3142

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
type CacheFunctionOptions,
cache,
cacheResponse,
getResponseCacheTags,
noCacheFetchOptions,
parseCacheResponse,
} from './cache';
Expand Down Expand Up @@ -370,6 +371,17 @@ interface GetRevisionOptions {
* These options don't impact the cache key and it means revisions can be shared between different fetches with different metadata options.
*/
metadata: boolean;

/**
* Whether to fetch the revision as a computed revision.
* @default true
*/
computed?: boolean;

/**
* Additional tags to add to the cache entry.
*/
tags?: string[];
}

const getAPIContextId = async () => {
Expand All @@ -382,8 +394,14 @@ const getAPIContextId = async () => {
*/
export const getRevision = cache({
name: 'api.getRevision.v2',
tag: (spaceId, revisionId) =>
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
tag: (spaceId, revisionId, fetchOptions) =>
// Temporary hack to make it work with OpenAPI on v1
fetchOptions.tags?.[0] ??
getCacheTag({
tag: 'revision',
space: spaceId,
revision: revisionId,
}),
tagImmutable: true,
getKeySuffix: getAPIContextId,
get: async (
Expand All @@ -405,18 +423,35 @@ export const getRevision = cache({
}
);

return cacheResponse(response, fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day);
return cacheResponse(response, {
...(fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day),
data: {
...response.data,
tags: getResponseCacheTags(response),
},
});
},
getKeyArgs: ([spaceId, revisionId, fetchOptions]) => {
if (fetchOptions.computed === true) {
return [spaceId, revisionId, { computed: true }];
}
return [spaceId, revisionId];
},
getKeyArgs: (args) => [args[0], args[1]],
});

/**
* Get all the pages in a revision of a space.
*/
export const getRevisionPages = cache({
name: 'api.getRevisionPages.v4',
tag: (spaceId, revisionId) =>
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
tag: (spaceId, revisionId, fetchOptions) =>
// Temporary hack to make it work with OpenAPI on v1
fetchOptions.tags?.[0] ??
getCacheTag({
tag: 'revision',
space: spaceId,
revision: revisionId,
}),
tagImmutable: true,
getKeySuffix: getAPIContextId,
get: async (
Expand All @@ -440,10 +475,15 @@ export const getRevisionPages = cache({

return cacheResponse(response, {
...(fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day),
data: response.data.pages,
data: { ...response.data, tags: getResponseCacheTags(response) },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it an array usually ?

});
},
getKeyArgs: (args) => [args[0], args[1]],
getKeyArgs: ([spaceId, revisionId, fetchOptions]) => {
if (fetchOptions.computed === true) {
return [spaceId, revisionId, { computed: true }];
}
return [spaceId, revisionId];
},
});

/**
Expand Down
13 changes: 10 additions & 3 deletions packages/gitbook/src/lib/cache/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export const noCacheFetchOptions: Partial<RequestInit> = {
},
};

/**
* Return the cache tags from the response.
*/
export function getResponseCacheTags(response: Response): string[] {
const cacheTagHeader = response.headers.get('x-gitbook-cache-tag');
return !cacheTagHeader ? [] : cacheTagHeader.split(',');
}

/**
* Parse an HTTP response into a cache entry.
*/
Expand All @@ -26,8 +34,7 @@ export function parseCacheResponse(response: Response): {
const cacheControlHeader = response.headers.get('cache-control');
const cacheControl = cacheControlHeader ? parseCacheControl(cacheControlHeader) : null;

const cacheTagHeader = response.headers.get('x-gitbook-cache-tag');
const tags = !cacheTagHeader ? [] : cacheTagHeader.split(',');
const tags = getResponseCacheTags(response);

const entry = {
ttl: 60 * 60 * 24,
Expand All @@ -47,7 +54,7 @@ export function parseCacheResponse(response: Response): {
export function cacheResponse<Result, DefaultData = Result>(
response: Response & { data: Result },
defaultEntry: Partial<CacheResult<DefaultData>> = {}
): CacheResult<DefaultData extends Result ? Result : DefaultData> {
): CacheResult<DefaultData extends undefined ? Result : DefaultData> {
const parsed = parseCacheResponse(response);

return {
Expand Down
38 changes: 36 additions & 2 deletions packages/gitbook/src/lib/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { GitBookDataFetcher } from '@v2/lib/data/types';
import { createImageResizer } from '@v2/lib/images';
import { createLinker } from '@v2/lib/links';

import { RevisionPageType } from '@gitbook/api';
import { DataFetcherError, wrapDataFetcherError } from '@v2/lib/data';
import { headers } from 'next/headers';
import {
Expand Down Expand Up @@ -134,9 +135,29 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {

getRevision(params) {
return wrapDataFetcherError(async () => {
return getRevision(params.spaceId, params.revisionId, {
const { tags, ...revision } = await getRevision(params.spaceId, params.revisionId, {
metadata: params.metadata,
computed: false,
});

if (
Object.values(revision.pages).some(
(page) => page.type === RevisionPageType.Computed
)
) {
const { tags: _tags, ...revision } = await getRevision(
params.spaceId,
params.revisionId,
{
metadata: params.metadata,
computed: true,
tags,
}
);
return revision;
}

return revision;
});
},

Expand Down Expand Up @@ -183,9 +204,22 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {

getRevisionPages(params) {
return wrapDataFetcherError(async () => {
return getRevisionPages(params.spaceId, params.revisionId, {
const { pages, tags } = await getRevisionPages(params.spaceId, params.revisionId, {
metadata: params.metadata,
computed: false,
});

if (pages.some((page) => page.type === RevisionPageType.Computed)) {
const { pages } = await getRevisionPages(params.spaceId, params.revisionId, {
metadata: params.metadata,
computed: true,
tags,
});

return pages;
}

return pages;
});
},

Expand Down
Loading