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

Skip to content

Commit c5db6e4

Browse files
committed
fixup! feat(isr): added background revalidation and non-blocking render
1 parent f41d38f commit c5db6e4

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

apps/ssr-isr/server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function app(): express.Express {
3333
commonEngine,
3434
backgroundRevalidation: true, // will revalidate in background and serve the cache page first
3535
nonBlockingRender: true, // will serve page first and store in cache in background
36-
modifyGeneratedHtml: defaultModifyGeneratedHtml,
36+
modifyGeneratedHtml: customModifyGeneratedHtml,
3737
// cache: fsCacheHandler,
3838
});
3939

@@ -67,12 +67,11 @@ export function app(): express.Express {
6767
return server;
6868
}
6969

70-
const defaultModifyGeneratedHtml: ModifyHtmlCallbackFn = (
70+
const customModifyGeneratedHtml: ModifyHtmlCallbackFn = (
7171
req: Request,
7272
html: string,
7373
): string => {
7474
const time = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
75-
7675
let msg = '<!-- ';
7776
msg += `\n🚀 ISR: Served from cache! \n⌛ Last updated: ${time}. `;
7877
msg += ' \n-->';

libs/isr/server/src/cache-generation.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,37 @@ export class CacheGeneration {
103103
// don't do !revalidate because it will also catch "0"
104104
return { html: finalHtml };
105105
}
106+
106107
// add the regenerated page to cache
107-
if (this.isrConfig.nonBlockingRender) {
108-
this.cache.add(cacheKey, finalHtml, {
109-
revalidate,
110-
buildId: this.isrConfig.buildId,
111-
});
112-
} else {
113-
await this.cache.add(cacheKey, finalHtml, {
108+
const addToCache = () => {
109+
return this.cache.add(cacheKey, finalHtml, {
114110
revalidate,
115111
buildId: this.isrConfig.buildId,
116112
});
113+
};
114+
115+
try {
116+
if (this.isrConfig.nonBlockingRender) {
117+
// If enabled, add to cache without waiting (fire-and-forget)
118+
addToCache();
119+
} else {
120+
// If not enabled, wait for cache addition to complete before proceeding
121+
await addToCache();
122+
}
123+
} catch (error) {
124+
console.error('Error adding to cache:', error);
117125
}
126+
118127
if (mode === 'regenerate') {
119128
// remove from urlsOnHold because we are done
120129
this.urlsOnHold = this.urlsOnHold.filter((x) => x !== cacheKey);
121130
this.logger.log(`Url: ${cacheKey} was regenerated!`);
122131
}
132+
123133
return { html: finalHtml };
124134
} catch (error) {
125135
this.logger.log(`Error regenerating url: ${cacheKey}`, error);
136+
126137
if (mode === 'regenerate') {
127138
// Ensure removal from urlsOnHold in case of error
128139
this.urlsOnHold = this.urlsOnHold.filter((x) => x !== cacheKey);

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,26 +197,29 @@ export class ISRHandler {
197197
const lastCacheDateDiff = (Date.now() - createdAt) / 1000; // in seconds
198198

199199
if (lastCacheDateDiff > cacheConfig.revalidate) {
200-
// regenerate the page without awaiting, so the user gets the cached page immediately
201-
if (this.isrConfig.backgroundRevalidation) {
202-
this.cacheGeneration.generateWithCacheKey(
200+
const generate = () => {
201+
return this.cacheGeneration.generateWithCacheKey(
203202
req,
204203
res,
205204
cacheKey,
206205
config?.providers,
207206
'regenerate',
208207
);
209-
} else {
210-
const result = await this.cacheGeneration.generateWithCacheKey(
211-
req,
212-
res,
213-
cacheKey,
214-
config?.providers,
215-
'regenerate',
216-
);
217-
if (result?.html) {
218-
finalHtml = result.html;
208+
};
209+
210+
try {
211+
// regenerate the page without awaiting, so the user gets the cached page immediately
212+
if (this.isrConfig.backgroundRevalidation) {
213+
generate();
214+
} else {
215+
const result = await generate();
216+
if (result?.html) {
217+
finalHtml = result.html;
218+
}
219219
}
220+
} catch (error) {
221+
console.error('Error generating html', error);
222+
next();
220223
}
221224
}
222225
}

0 commit comments

Comments
 (0)