Skip to content

Commit a08a093

Browse files
committed
refactor updateOnTagsSet
1 parent 6ee3121 commit a08a093

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

packages/open-next/src/adapters/cache.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,8 @@ export default class Cache {
295295
break;
296296
}
297297
}
298-
if (
299-
globalThis.openNextConfig.dangerous?.disableTagCache ||
300-
globalThis.tagCache.mode === "nextMode"
301-
) {
302-
return;
303-
}
304-
// Write derivedTags to dynamodb
305-
// If we use an in house version of getDerivedTags in build we should use it here instead of next's one
306-
const derivedTags: string[] =
307-
data?.kind === "FETCH"
308-
? (ctx?.tags ?? data?.data?.tags ?? []) // before version 14 next.js used data?.data?.tags so we keep it for backward compatibility
309-
: data?.kind === "PAGE"
310-
? (data.headers?.["x-next-cache-tags"]?.split(",") ?? [])
311-
: [];
312-
debug("derivedTags", derivedTags);
313-
await this.updateTagsOnSet(key, derivedTags);
298+
299+
await this.updateTagsOnSet(key, data, ctx);
314300
debug("Finished setting cache");
315301
} catch (e) {
316302
error("Failed to set cache", e);
@@ -414,13 +400,28 @@ export default class Cache {
414400
}
415401
}
416402

417-
private async updateTagsOnSet(key: string, derivedTags: string[]) {
403+
private async updateTagsOnSet(
404+
key: string,
405+
data?: IncrementalCacheValue,
406+
ctx?: IncrementalCacheContext,
407+
) {
418408
if (
419409
globalThis.openNextConfig.dangerous?.disableTagCache ||
420410
globalThis.tagCache.mode === "nextMode"
421411
) {
422412
return;
423413
}
414+
415+
// Write derivedTags to the tag cache
416+
// If we use an in house version of getDerivedTags in build we should use it here instead of next's one
417+
const derivedTags: string[] =
418+
data?.kind === "FETCH"
419+
? (ctx?.tags ?? data?.data?.tags ?? []) // before version 14 next.js used data?.data?.tags so we keep it for backward compatibility
420+
: data?.kind === "PAGE"
421+
? (data.headers?.["x-next-cache-tags"]?.split(",") ?? [])
422+
: [];
423+
debug("derivedTags", derivedTags);
424+
424425
// Get all tags stored in dynamodb for the given key
425426
// If any of the derived tags are not stored in dynamodb for the given key, write them
426427
const storedTags = await globalThis.tagCache.getByPath(key);

0 commit comments

Comments
 (0)