Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcc61f9

Browse files
committed
feat(isr): add background revalidation option
1 parent 2c88c4f commit dcc61f9

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

libs/isr/models/src/isr-handler-config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ export interface ISRHandlerConfig {
119119
* This can avoid client-side waiting times if the remote cache storage is down.
120120
*/
121121
nonBlockingRender?: boolean;
122+
123+
/**
124+
* If set to true, the server will provide the cached HTML as soon as possible and will revalidate the cache in the background.
125+
*/
126+
backgroundRevalidation?: boolean;
122127
}
123128

124129
export interface ServeFromCacheConfig {

libs/isr/server/src/isr-handler.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,34 +187,46 @@ export class ISRHandler {
187187
return;
188188
}
189189

190-
// Apply the callback if given
191-
let finalHtml = html;
192-
if (config?.modifyCachedHtml) {
193-
const timeStart = performance.now();
194-
finalHtml = config.modifyCachedHtml(req, html);
195-
const totalTime = (performance.now() - timeStart).toFixed(2);
196-
finalHtml += `<!--\nℹ️ ISR: This cachedHtml has been modified with modifyCachedHtml()\n❗️
197-
This resulted into more ${totalTime}ms of processing time.\n-->`;
198-
}
199-
200190
// Cache exists. Send it.
201191
this.logger.log(`Page was retrieved from cache: `, cacheKey);
192+
let finalHtml = html;
202193

203194
// if the cache is expired, we will regenerate it
204195
if (cacheConfig.revalidate && cacheConfig.revalidate > 0) {
205196
const lastCacheDateDiff = (Date.now() - createdAt) / 1000; // in seconds
206197

207198
if (lastCacheDateDiff > cacheConfig.revalidate) {
208199
// regenerate the page without awaiting, so the user gets the cached page immediately
209-
this.cacheGeneration.generateWithCacheKey(
210-
req,
211-
res,
212-
cacheKey,
213-
config?.providers,
214-
'regenerate',
215-
);
200+
if (this.isrConfig.backgroundRevalidation) {
201+
this.cacheGeneration.generateWithCacheKey(
202+
req,
203+
res,
204+
cacheKey,
205+
config?.providers,
206+
'regenerate',
207+
);
208+
} else {
209+
const result = await this.cacheGeneration.generateWithCacheKey(
210+
req,
211+
res,
212+
cacheKey,
213+
config?.providers,
214+
'regenerate',
215+
);
216+
if (result?.html) {
217+
finalHtml = result.html;
218+
}
219+
}
216220
}
217221
}
222+
// Apply the callback if given
223+
if (config?.modifyCachedHtml) {
224+
const timeStart = performance.now();
225+
finalHtml = config.modifyCachedHtml(req, finalHtml);
226+
const totalTime = (performance.now() - timeStart).toFixed(2);
227+
finalHtml += `<!--\nℹ️ ISR: This cachedHtml has been modified with modifyCachedHtml()\n❗️
228+
This resulted into more ${totalTime}ms of processing time.\n-->`;
229+
}
218230
return res.send(finalHtml);
219231
} catch (error) {
220232
// Cache does not exist. Serve user using SSR

0 commit comments

Comments
 (0)