Skip to content

Commit ad9cdbf

Browse files
correct wrong logic
1 parent 2229136 commit ad9cdbf

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

packages/next-on-pages/templates/_worker.js/utils/cache.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const CACHE_TAGS_HEADER = 'x-vercel-cache-tags';
1010
// https://github.com/vercel/next.js/blob/ba23d986/packages/next/src/lib/constants.ts#L18
1111
const NEXT_CACHE_SOFT_TAGS_HEADER = 'x-next-cache-soft-tags';
1212

13+
// https://github.com/vercel/next.js/blob/fc25fcef/packages/next/src/server/lib/incremental-cache/fetch-cache.ts#L21
14+
const CACHE_STATE_HEADER = 'x-vercel-cache-state';
15+
1316
/**
1417
* Handles an internal request to the suspense cache.
1518
*
@@ -52,20 +55,15 @@ export async function handleSuspenseCacheRequest(
5255
NEXT_CACHE_SOFT_TAGS_HEADER,
5356
);
5457

55-
// Retrieve the value from the cache.
5658
const data = await incrementalCache.get(cacheKey, { softTags });
5759
if (!data) return new Response(null, { status: 404 });
5860

59-
const lastModified =
60-
typeof data.curRevalidate === 'number'
61-
? data.curRevalidate
62-
: Date.now();
6361
return new Response(JSON.stringify(data.value), {
6462
status: 200,
6563
headers: {
6664
'Content-Type': 'application/json',
67-
'x-vercel-cache-state': 'fresh',
68-
age: `${(Date.now() - lastModified) / 1000}`,
65+
[CACHE_STATE_HEADER]: 'fresh',
66+
age: `${data.age}`,
6967
},
7068
});
7169
}

packages/next-on-pages/templates/cache/incrementalCache.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class CacheHandler {
3939
/* eslint-enable */
4040

4141
/**
42-
* Simplified version of the IncrementalCache from Next.js
42+
* Simplified (and tweaked) version of the IncrementalCache from Next.js
4343
* source: https://github.com/vercel/next.js/blob/c4adae89b/packages/next/src/server/lib/incremental-cache/index.ts#L65
4444
*/
4545
export class IncrementalCache {
@@ -89,12 +89,16 @@ export class IncrementalCache {
8989
tags?: string[];
9090
softTags?: string[];
9191
} = {},
92-
): Promise<IncrementalCacheEntry | null> {
93-
let entry: IncrementalCacheEntry | null = null;
92+
): Promise<(IncrementalCacheEntry & { age?: number }) | null> {
93+
let entry: (IncrementalCacheEntry & { age?: number }) | null = null;
9494
let revalidate = ctx.revalidate;
9595

9696
const cacheData = await this.cacheHandler?.get(cacheKey, ctx);
9797

98+
const age = cacheData
99+
? (Date.now() - (cacheData.lastModified || 0)) / 1000
100+
: undefined;
101+
98102
if (cacheData?.value?.kind === 'FETCH') {
99103
const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])];
100104
// if a tag was revalidated we don't return stale data
@@ -107,9 +111,10 @@ export class IncrementalCache {
107111
}
108112

109113
revalidate = revalidate || cacheData.value.revalidate;
110-
const age = (Date.now() - (cacheData.lastModified || 0)) / 1000;
111-
112-
const isStale = typeof revalidate === 'number' && age > revalidate;
114+
const isStale =
115+
typeof revalidate === 'number' &&
116+
typeof age === 'number' &&
117+
age > revalidate;
113118
const data = cacheData.value.data;
114119

115120
return {
@@ -119,6 +124,7 @@ export class IncrementalCache {
119124
data,
120125
revalidate: revalidate,
121126
},
127+
age,
122128
revalidateAfter:
123129
typeof revalidate === 'number' && Date.now() + revalidate * 1000,
124130
};
@@ -128,31 +134,30 @@ export class IncrementalCache {
128134
let revalidateAfter: false | number;
129135

130136
if (cacheData?.lastModified === -1) {
131-
isStale = -1;
132137
revalidateAfter = -1 * CACHE_ONE_YEAR;
138+
isStale = -1;
133139
} else {
134140
revalidateAfter = 1 * 1000 + (cacheData?.lastModified || Date.now());
135-
136141
isStale = revalidateAfter < Date.now() ? true : undefined;
137142
}
138143

139-
if (cacheData) {
140-
entry = {
141-
isStale,
142-
revalidateAfter,
143-
value: cacheData.value,
144-
};
145-
}
144+
entry = {
145+
isStale,
146+
revalidateAfter,
147+
value: null,
148+
};
146149

147-
if (!cacheData) {
148-
entry = {
149-
isStale,
150-
value: null,
151-
revalidateAfter,
152-
};
150+
if (cacheData) {
151+
entry.value = cacheData.value;
152+
entry.age = age;
153+
} else {
153154
await this.set(cacheKey, entry.value, ctx);
154155
}
155-
return entry;
156+
157+
return {
158+
...entry,
159+
age,
160+
};
156161
}
157162

158163
async set(

0 commit comments

Comments
 (0)