Skip to content

Commit 3b979a2

Browse files
authored
Pass revalidate value to the incremental cache for ISR/SSG route (#833)
* pass revalidate for isr incremental cache * changeset * review fix
1 parent 7a8d2e7 commit 3b979a2

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

.changeset/lucky-ghosts-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
pass revalidate for ISR/SSG cache

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export default class Cache {
207207
if (data === null || data === undefined) {
208208
await globalThis.incrementalCache.delete(key);
209209
} else {
210+
const revalidate = this.extractRevalidateForSet(ctx);
210211
switch (data.kind) {
211212
case "ROUTE":
212213
case "APP_ROUTE": {
@@ -224,6 +225,7 @@ export default class Cache {
224225
status,
225226
headers,
226227
},
228+
revalidate,
227229
},
228230
false,
229231
);
@@ -244,6 +246,7 @@ export default class Cache {
244246
status,
245247
headers,
246248
},
249+
revalidate,
247250
},
248251
false,
249252
);
@@ -254,6 +257,7 @@ export default class Cache {
254257
type: "page",
255258
html,
256259
json: pageData,
260+
revalidate,
257261
},
258262
false,
259263
);
@@ -272,6 +276,7 @@ export default class Cache {
272276
status,
273277
headers,
274278
},
279+
revalidate,
275280
},
276281
false,
277282
);
@@ -286,6 +291,7 @@ export default class Cache {
286291
{
287292
type: "redirect",
288293
props: data.props,
294+
revalidate,
289295
},
290296
false,
291297
);
@@ -424,7 +430,8 @@ export default class Cache {
424430
// If we use an in house version of getDerivedTags in build we should use it here instead of next's one
425431
const derivedTags: string[] =
426432
data?.kind === "FETCH"
427-
? (ctx?.tags ?? data?.data?.tags ?? []) // before version 14 next.js used data?.data?.tags so we keep it for backward compatibility
433+
? //@ts-expect-error - On older versions of next, ctx was a number, but for these cases we use data?.data?.tags
434+
(ctx?.tags ?? data?.data?.tags ?? []) // before version 14 next.js used data?.data?.tags so we keep it for backward compatibility
428435
: data?.kind === "PAGE"
429436
? (data.headers?.["x-next-cache-tags"]?.split(",") ?? [])
430437
: [];
@@ -446,4 +453,22 @@ export default class Cache {
446453
);
447454
}
448455
}
456+
457+
private extractRevalidateForSet(
458+
ctx?: IncrementalCacheContext,
459+
): number | false | undefined {
460+
if (ctx === undefined) {
461+
return undefined;
462+
}
463+
if (typeof ctx === "number" || ctx === false) {
464+
return ctx;
465+
}
466+
if ("revalidate" in ctx) {
467+
return ctx.revalidate;
468+
}
469+
if ("cacheControl" in ctx) {
470+
return ctx.cacheControl?.revalidate;
471+
}
472+
return undefined;
473+
}
449474
}

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,44 @@ export type TagCacheMetaFile = {
9898
revalidatedAt: { N: string };
9999
};
100100

101-
export type IncrementalCacheContext = {
102-
revalidate?: number | false | undefined;
103-
fetchCache?: boolean | undefined;
104-
fetchUrl?: string | undefined;
105-
fetchIdx?: number | undefined;
106-
tags?: string[] | undefined;
107-
};
101+
// Cache context since vercel/next.js#76207
102+
interface SetIncrementalFetchCacheContext {
103+
fetchCache: true;
104+
fetchUrl?: string;
105+
fetchIdx?: number;
106+
tags?: string[];
107+
}
108+
109+
interface SetIncrementalResponseCacheContext {
110+
fetchCache?: false;
111+
cacheControl?: {
112+
revalidate: number | false;
113+
expire?: number;
114+
};
115+
116+
/**
117+
* True if the route is enabled for PPR.
118+
*/
119+
isRoutePPREnabled?: boolean;
120+
121+
/**
122+
* True if this is a fallback request.
123+
*/
124+
isFallback?: boolean;
125+
}
126+
127+
// Before vercel/next.js#76207 revalidate was passed this way
128+
interface SetIncrementalCacheContext {
129+
revalidate?: number | false;
130+
isRoutePPREnabled?: boolean;
131+
isFallback?: boolean;
132+
}
133+
134+
// Before vercel/next.js#53321 context on set was just the revalidate
135+
type OldSetIncrementalCacheContext = number | false | undefined;
136+
137+
export type IncrementalCacheContext =
138+
| OldSetIncrementalCacheContext
139+
| SetIncrementalCacheContext
140+
| SetIncrementalFetchCacheContext
141+
| SetIncrementalResponseCacheContext;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ export type WithLastModified<T> = {
7979

8080
export type CacheValue<IsFetch extends boolean> = (IsFetch extends true
8181
? CachedFetchValue
82-
: CachedFile) & { revalidate?: number | false };
82+
: CachedFile) & {
83+
/**
84+
* This is available for page cache entry, but only at runtime.
85+
*/
86+
revalidate?: number | false;
87+
};
8388

8489
export type IncrementalCache = {
8590
get<IsFetch extends boolean = false>(

0 commit comments

Comments
 (0)