@@ -39,7 +39,7 @@ export class CacheHandler {
39
39
/* eslint-enable */
40
40
41
41
/**
42
- * Simplified version of the IncrementalCache from Next.js
42
+ * Simplified (and tweaked) version of the IncrementalCache from Next.js
43
43
* source: https://github.com/vercel/next.js/blob/c4adae89b/packages/next/src/server/lib/incremental-cache/index.ts#L65
44
44
*/
45
45
export class IncrementalCache {
@@ -89,12 +89,16 @@ export class IncrementalCache {
89
89
tags ?: string [ ] ;
90
90
softTags ?: string [ ] ;
91
91
} = { } ,
92
- ) : Promise < IncrementalCacheEntry | null > {
93
- let entry : IncrementalCacheEntry | null = null ;
92
+ ) : Promise < ( IncrementalCacheEntry & { age ?: number } ) | null > {
93
+ let entry : ( IncrementalCacheEntry & { age ?: number } ) | null = null ;
94
94
let revalidate = ctx . revalidate ;
95
95
96
96
const cacheData = await this . cacheHandler ?. get ( cacheKey , ctx ) ;
97
97
98
+ const age = cacheData
99
+ ? ( Date . now ( ) - ( cacheData . lastModified || 0 ) ) / 1000
100
+ : undefined ;
101
+
98
102
if ( cacheData ?. value ?. kind === 'FETCH' ) {
99
103
const combinedTags = [ ...( ctx . tags || [ ] ) , ...( ctx . softTags || [ ] ) ] ;
100
104
// if a tag was revalidated we don't return stale data
@@ -107,9 +111,10 @@ export class IncrementalCache {
107
111
}
108
112
109
113
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 ;
113
118
const data = cacheData . value . data ;
114
119
115
120
return {
@@ -119,6 +124,7 @@ export class IncrementalCache {
119
124
data,
120
125
revalidate : revalidate ,
121
126
} ,
127
+ age,
122
128
revalidateAfter :
123
129
typeof revalidate === 'number' && Date . now ( ) + revalidate * 1000 ,
124
130
} ;
@@ -128,31 +134,30 @@ export class IncrementalCache {
128
134
let revalidateAfter : false | number ;
129
135
130
136
if ( cacheData ?. lastModified === - 1 ) {
131
- isStale = - 1 ;
132
137
revalidateAfter = - 1 * CACHE_ONE_YEAR ;
138
+ isStale = - 1 ;
133
139
} else {
134
140
revalidateAfter = 1 * 1000 + ( cacheData ?. lastModified || Date . now ( ) ) ;
135
-
136
141
isStale = revalidateAfter < Date . now ( ) ? true : undefined ;
137
142
}
138
143
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
+ } ;
146
149
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 {
153
154
await this . set ( cacheKey , entry . value , ctx ) ;
154
155
}
155
- return entry ;
156
+
157
+ return {
158
+ ...entry ,
159
+ age,
160
+ } ;
156
161
}
157
162
158
163
async set (
0 commit comments