Skip to content

Commit 4e25474

Browse files
committed
add automatic invalidation for nextMode tag cache
1 parent c7a8fe0 commit 4e25474

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,27 @@ export default class Cache {
318318
try {
319319
const _tags = Array.isArray(tags) ? tags : [tags];
320320
if (globalThis.tagCache.mode === "nextMode") {
321-
return globalThis.tagCache.writeTags(_tags);
321+
const paths = (await globalThis.tagCache.getPathsByTags?.(_tags)) ?? [];
322+
323+
await globalThis.tagCache.writeTags(_tags);
324+
if (paths.length > 0) {
325+
// TODO: we should introduce a new method in cdnInvalidationHandler to invalidate paths by tags for cdn that supports it
326+
// It also means that we'll need to provide the tags used in every request to the wrapper or converter.
327+
await globalThis.cdnInvalidationHandler.invalidatePaths(
328+
paths.map((path) => ({
329+
initialPath: path,
330+
rawPath: path,
331+
resolvedRoutes: [
332+
{
333+
route: path,
334+
// TODO: ideally here we should check if it's an app router page or route
335+
type: "app",
336+
},
337+
],
338+
})),
339+
);
340+
}
341+
return;
322342
}
323343
for (const tag of _tags) {
324344
debug("revalidateTag", tag);

packages/open-next/src/overrides/tagCache/ddb-nextMode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function buildDynamoObject(tag: string, revalidatedAt?: number) {
6767
};
6868
}
6969

70+
// This implementation does not support automatic invalidation of paths by the cdn
7071
export default {
7172
name: "ddb-nextMode",
7273
mode: "nextMode",

packages/open-next/src/types/overrides.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ export type NextModeTagCache = BaseTagCache & {
103103
mode: "nextMode";
104104
hasBeenRevalidated(tags: string[], lastModified?: number): Promise<boolean>;
105105
writeTags(tags: string[]): Promise<void>;
106+
// Optional method to get paths by tags
107+
// It is used to automatically invalidate paths in the CDN
108+
getPathsByTags?: (tags: string[]) => Promise<string[]>;
106109
};
107110

108111
export type OriginalTagCache = BaseTagCache & {

0 commit comments

Comments
 (0)