Skip to content

Pass revalidate value to the incremental cache for ISR/SSG route #833

New issue

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

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

Already on GitHub? # to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/lucky-ghosts-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

pass revalidate for ISR/SSG cache
27 changes: 26 additions & 1 deletion packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export default class Cache {
if (data === null || data === undefined) {
await globalThis.incrementalCache.delete(key);
} else {
const revalidate = this.extractRevalidateForSet(ctx);
switch (data.kind) {
case "ROUTE":
case "APP_ROUTE": {
Expand All @@ -224,6 +225,7 @@ export default class Cache {
status,
headers,
},
revalidate,
},
false,
);
Expand All @@ -244,6 +246,7 @@ export default class Cache {
status,
headers,
},
revalidate,
},
false,
);
Expand All @@ -254,6 +257,7 @@ export default class Cache {
type: "page",
html,
json: pageData,
revalidate,
},
false,
);
Expand All @@ -272,6 +276,7 @@ export default class Cache {
status,
headers,
},
revalidate,
},
false,
);
Expand All @@ -286,6 +291,7 @@ export default class Cache {
{
type: "redirect",
props: data.props,
revalidate,
},
false,
);
Expand Down Expand Up @@ -424,7 +430,8 @@ export default class Cache {
// If we use an in house version of getDerivedTags in build we should use it here instead of next's one
const derivedTags: string[] =
data?.kind === "FETCH"
? (ctx?.tags ?? data?.data?.tags ?? []) // before version 14 next.js used data?.data?.tags so we keep it for backward compatibility
? //@ts-expect-error - On older versions of next, ctx was a number, but for these cases we use data?.data?.tags
(ctx?.tags ?? data?.data?.tags ?? []) // before version 14 next.js used data?.data?.tags so we keep it for backward compatibility
: data?.kind === "PAGE"
? (data.headers?.["x-next-cache-tags"]?.split(",") ?? [])
: [];
Expand All @@ -446,4 +453,22 @@ export default class Cache {
);
}
}

private extractRevalidateForSet(
ctx?: IncrementalCacheContext,
): number | false | undefined {
if (ctx === undefined) {
return undefined;
}
if (typeof ctx === "number" || ctx === false) {
return ctx;
}
if ("revalidate" in ctx) {
return ctx.revalidate;
}
if ("cacheControl" in ctx) {
return ctx.cacheControl?.revalidate;
}
return undefined;
}
}
48 changes: 41 additions & 7 deletions packages/open-next/src/types/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,44 @@ export type TagCacheMetaFile = {
revalidatedAt: { N: string };
};

export type IncrementalCacheContext = {
revalidate?: number | false | undefined;
fetchCache?: boolean | undefined;
fetchUrl?: string | undefined;
fetchIdx?: number | undefined;
tags?: string[] | undefined;
};
// Cache context since vercel/next.js#76207
interface SetIncrementalFetchCacheContext {
fetchCache: true;
fetchUrl?: string;
fetchIdx?: number;
tags?: string[];
}

interface SetIncrementalResponseCacheContext {
fetchCache?: false;
cacheControl?: {
revalidate: number | false;
expire?: number;
};

/**
* True if the route is enabled for PPR.
*/
isRoutePPREnabled?: boolean;

/**
* True if this is a fallback request.
*/
isFallback?: boolean;
}

// Before vercel/next.js#76207 revalidate was passed this way
interface SetIncrementalCacheContext {
revalidate?: number | false;
isRoutePPREnabled?: boolean;
isFallback?: boolean;
}

// Before vercel/next.js#53321 context on set was just the revalidate
type OldSetIncrementalCacheContext = number | false | undefined;

export type IncrementalCacheContext =
| OldSetIncrementalCacheContext
| SetIncrementalCacheContext
| SetIncrementalFetchCacheContext
| SetIncrementalResponseCacheContext;
7 changes: 6 additions & 1 deletion packages/open-next/src/types/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ export type WithLastModified<T> = {

export type CacheValue<IsFetch extends boolean> = (IsFetch extends true
? CachedFetchValue
: CachedFile) & { revalidate?: number | false };
: CachedFile) & {
/**
* This is available for page cache entry, but only at runtime.
*/
revalidate?: number | false;
};

export type IncrementalCache = {
get<IsFetch extends boolean = false>(
Expand Down