Skip to content

Commit 617e385

Browse files
committed
Fix computed revision cache in v1
1 parent 3363a18 commit 617e385

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

packages/gitbook/src/lib/api.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
type CacheFunctionOptions,
2727
cache,
2828
cacheResponse,
29+
getResponseCacheTags,
2930
noCacheFetchOptions,
3031
parseCacheResponse,
3132
} from './cache';
@@ -370,6 +371,17 @@ interface GetRevisionOptions {
370371
* These options don't impact the cache key and it means revisions can be shared between different fetches with different metadata options.
371372
*/
372373
metadata: boolean;
374+
375+
/**
376+
* Whether to fetch the revision as a computed revision.
377+
* @default true
378+
*/
379+
computed?: boolean;
380+
381+
/**
382+
* Additional tags to add to the cache entry.
383+
*/
384+
tags?: string[];
373385
}
374386

375387
const getAPIContextId = async () => {
@@ -382,8 +394,14 @@ const getAPIContextId = async () => {
382394
*/
383395
export const getRevision = cache({
384396
name: 'api.getRevision.v2',
385-
tag: (spaceId, revisionId) =>
386-
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
397+
tag: (spaceId, revisionId, fetchOptions) =>
398+
// Temporary hack to make it work with OpenAPI on v1
399+
fetchOptions.tags?.[0] ??
400+
getCacheTag({
401+
tag: 'revision',
402+
space: spaceId,
403+
revision: revisionId,
404+
}),
387405
tagImmutable: true,
388406
getKeySuffix: getAPIContextId,
389407
get: async (
@@ -405,18 +423,35 @@ export const getRevision = cache({
405423
}
406424
);
407425

408-
return cacheResponse(response, fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day);
426+
return cacheResponse(response, {
427+
...(fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day),
428+
data: {
429+
...response.data,
430+
tags: getResponseCacheTags(response),
431+
},
432+
});
433+
},
434+
getKeyArgs: ([spaceId, revisionId, fetchOptions]) => {
435+
if (fetchOptions.computed === true) {
436+
return [spaceId, revisionId, { computed: true }];
437+
}
438+
return [spaceId, revisionId];
409439
},
410-
getKeyArgs: (args) => [args[0], args[1]],
411440
});
412441

413442
/**
414443
* Get all the pages in a revision of a space.
415444
*/
416445
export const getRevisionPages = cache({
417446
name: 'api.getRevisionPages.v4',
418-
tag: (spaceId, revisionId) =>
419-
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
447+
tag: (spaceId, revisionId, fetchOptions) =>
448+
// Temporary hack to make it work with OpenAPI on v1
449+
fetchOptions.tags?.[0] ??
450+
getCacheTag({
451+
tag: 'revision',
452+
space: spaceId,
453+
revision: revisionId,
454+
}),
420455
tagImmutable: true,
421456
getKeySuffix: getAPIContextId,
422457
get: async (
@@ -440,10 +475,15 @@ export const getRevisionPages = cache({
440475

441476
return cacheResponse(response, {
442477
...(fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day),
443-
data: response.data.pages,
478+
data: { ...response.data, tags: getResponseCacheTags(response) },
444479
});
445480
},
446-
getKeyArgs: (args) => [args[0], args[1]],
481+
getKeyArgs: ([spaceId, revisionId, fetchOptions]) => {
482+
if (fetchOptions.computed === true) {
483+
return [spaceId, revisionId, { computed: true }];
484+
}
485+
return [spaceId, revisionId];
486+
},
447487
});
448488

449489
/**

packages/gitbook/src/lib/cache/http.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export const noCacheFetchOptions: Partial<RequestInit> = {
1313
},
1414
};
1515

16+
/**
17+
* Return the cache tags from the response.
18+
*/
19+
export function getResponseCacheTags(response: Response): string[] {
20+
const cacheTagHeader = response.headers.get('x-gitbook-cache-tag');
21+
return !cacheTagHeader ? [] : cacheTagHeader.split(',');
22+
}
23+
1624
/**
1725
* Parse an HTTP response into a cache entry.
1826
*/
@@ -26,8 +34,7 @@ export function parseCacheResponse(response: Response): {
2634
const cacheControlHeader = response.headers.get('cache-control');
2735
const cacheControl = cacheControlHeader ? parseCacheControl(cacheControlHeader) : null;
2836

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

3239
const entry = {
3340
ttl: 60 * 60 * 24,
@@ -47,7 +54,7 @@ export function parseCacheResponse(response: Response): {
4754
export function cacheResponse<Result, DefaultData = Result>(
4855
response: Response & { data: Result },
4956
defaultEntry: Partial<CacheResult<DefaultData>> = {}
50-
): CacheResult<DefaultData extends Result ? Result : DefaultData> {
57+
): CacheResult<DefaultData extends undefined ? Result : DefaultData> {
5158
const parsed = parseCacheResponse(response);
5259

5360
return {

packages/gitbook/src/lib/v1.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { GitBookDataFetcher } from '@v2/lib/data/types';
88
import { createImageResizer } from '@v2/lib/images';
99
import { createLinker } from '@v2/lib/links';
1010

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

135136
getRevision(params) {
136137
return wrapDataFetcherError(async () => {
137-
return getRevision(params.spaceId, params.revisionId, {
138+
const { tags, ...revision } = await getRevision(params.spaceId, params.revisionId, {
138139
metadata: params.metadata,
140+
computed: false,
139141
});
142+
143+
if (
144+
Object.values(revision.pages).some(
145+
(page) => page.type === RevisionPageType.Computed
146+
)
147+
) {
148+
const { tags: _tags, ...revision } = await getRevision(
149+
params.spaceId,
150+
params.revisionId,
151+
{
152+
metadata: params.metadata,
153+
computed: true,
154+
tags,
155+
}
156+
);
157+
return revision;
158+
}
159+
160+
return revision;
140161
});
141162
},
142163

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

184205
getRevisionPages(params) {
185206
return wrapDataFetcherError(async () => {
186-
return getRevisionPages(params.spaceId, params.revisionId, {
207+
const { pages, tags } = await getRevisionPages(params.spaceId, params.revisionId, {
187208
metadata: params.metadata,
209+
computed: false,
188210
});
211+
212+
if (pages.some((page) => page.type === RevisionPageType.Computed)) {
213+
const { pages } = await getRevisionPages(params.spaceId, params.revisionId, {
214+
metadata: params.metadata,
215+
computed: true,
216+
tags,
217+
});
218+
219+
return pages;
220+
}
221+
222+
return pages;
189223
});
190224
},
191225

0 commit comments

Comments
 (0)