Skip to content

Commit 337176b

Browse files
committed
Skip immutable data structures
1 parent d32ca86 commit 337176b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/gitbook/src/lib/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export const getUserById = cache({
178178
tag: 'user',
179179
user: userId,
180180
}),
181+
tagImmutable: true,
181182
get: async (userId: string, options: CacheFunctionOptions) => {
182183
try {
183184
const apiCtx = await api();
@@ -379,6 +380,7 @@ export const getRevision = cache({
379380
name: 'api.getRevision.v2',
380381
tag: (spaceId, revisionId) =>
381382
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
383+
tagImmutable: true,
382384
getKeySuffix: getAPIContextId,
383385
get: async (
384386
spaceId: string,
@@ -411,6 +413,7 @@ export const getRevisionPages = cache({
411413
name: 'api.getRevisionPages.v4',
412414
tag: (spaceId, revisionId) =>
413415
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
416+
tagImmutable: true,
414417
getKeySuffix: getAPIContextId,
415418
get: async (
416419
spaceId: string,
@@ -446,6 +449,7 @@ export const getRevisionPageByPath = cache({
446449
name: 'api.getRevisionPageByPath.v3',
447450
tag: (spaceId, revisionId) =>
448451
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
452+
tagImmutable: true,
449453
getKeySuffix: getAPIContextId,
450454
get: async (
451455
spaceId: string,
@@ -492,6 +496,7 @@ const getRevisionFileById = cache({
492496
name: 'api.getRevisionFile.v3',
493497
tag: (spaceId, revisionId) =>
494498
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
499+
tagImmutable: true,
495500
get: async (
496501
spaceId: string,
497502
revisionId: string,
@@ -528,6 +533,7 @@ const getRevisionReusableContentById = cache({
528533
name: 'api.getRevisionReusableContentById.v1',
529534
tag: (spaceId, revisionId) =>
530535
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
536+
tagImmutable: true,
531537
getKeySuffix: getAPIContextId,
532538
get: async (
533539
spaceId: string,
@@ -569,6 +575,7 @@ const getRevisionAllFiles = cache({
569575
name: 'api.getRevisionAllFiles.v2',
570576
tag: (spaceId, revisionId) =>
571577
getCacheTag({ tag: 'revision', space: spaceId, revision: revisionId }),
578+
tagImmutable: true,
572579
get: async (spaceId: string, revisionId: string, options: CacheFunctionOptions) => {
573580
const response = await getAll(
574581
async (params) => {
@@ -684,6 +691,7 @@ export const getDocument = cache({
684691
name: 'api.getDocument.v2',
685692
tag: (spaceId, documentId) =>
686693
getCacheTag({ tag: 'document', space: spaceId, document: documentId }),
694+
tagImmutable: true,
687695
getKeySuffix: getAPIContextId,
688696
get: async (spaceId: string, documentId: string, options: CacheFunctionOptions) => {
689697
const apiCtx = await api();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export interface CacheDefinition<Args extends any[], Result> {
5555
/** Tag to associate to the entry */
5656
tag?: (...args: Args) => string;
5757

58+
/** If true, the tag will not be sent to the HTTP response, as we consider it immutable */
59+
tagImmutable?: boolean;
60+
5861
/** Filter the arguments that should be taken into consideration for the cache key */
5962
getKeyArgs?: (args: Args) => any[];
6063

@@ -136,7 +139,7 @@ export function cache<Args extends any[], Result>(
136139
const tag = cacheDef.tag?.(...args);
137140

138141
// Add the cache tag to the HTTP response
139-
if (tag) {
142+
if (tag && !cacheDef.tagImmutable) {
140143
addResponseCacheTag(tag);
141144
}
142145

0 commit comments

Comments
 (0)