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

Skip to content

Commit 0bb7443

Browse files
authored
fix(isr): fix breaking changes with the new modifyGeneratedHtml config (#1766)
1 parent e59ffb5 commit 0bb7443

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

apps/ssr-isr/server.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function app(): express.Express {
3232
bootstrap,
3333
commonEngine,
3434
modifyGeneratedHtml: defaultModifyGeneratedHtml,
35+
3536
// cache: fsCacheHandler,
3637
});
3738

@@ -68,14 +69,11 @@ export function app(): express.Express {
6869
const defaultModifyGeneratedHtml: ModifyHtmlCallbackFn = (
6970
req: Request,
7071
html: string,
71-
revalidateTime?: number | null,
7272
): string => {
7373
const time = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
7474

7575
let msg = '<!-- ';
7676
msg += `\n🚀 ISR: Served from cache! \n⌛ Last updated: ${time}. `;
77-
if (revalidateTime)
78-
msg += `\n⏭️ Next refresh is after ${revalidateTime} seconds. `;
7977
msg += ' \n-->';
8078
html = html.replace('Original content', 'Modified content');
8179
return html + msg;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ export type ModifyHtmlCallbackFn = (
141141

142142
export interface RenderConfig {
143143
providers?: Provider[];
144+
145+
// TODO: remove this in a major as a BREAKING CHANGE (we can provide some schematics to fix the breaking change maybe)
146+
/**
147+
* This callback lets you hook into the generated html and provide any modifications
148+
* necessary on-the-fly.
149+
* Use with caution as this may lead to a performance loss on serving the html.
150+
* @deprecated
151+
*/
152+
modifyGeneratedHtml?: (req: Request, html: string) => string;
144153
}
145154

146155
/**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class CacheGeneration {
2222
public cache: CacheHandler,
2323
public logger: ISRLogger,
2424
) {}
25+
2526
async generate(
2627
req: Request,
2728
res: Response,
@@ -38,6 +39,7 @@ export class CacheGeneration {
3839

3940
return this.generateWithCacheKey(req, res, cacheKey, providers, mode);
4041
}
42+
4143
async generateWithCacheKey(
4244
req: Request,
4345
res: Response,
@@ -58,6 +60,7 @@ export class CacheGeneration {
5860

5961
this.urlsOnHold.push(cacheKey);
6062
}
63+
6164
const renderUrlConfig: RenderUrlConfig = {
6265
req,
6366
res,
@@ -69,6 +72,7 @@ export class CacheGeneration {
6972
browserDistFolder: this.isrConfig.browserDistFolder,
7073
inlineCriticalCss: this.isrConfig.inlineCriticalCss,
7174
};
75+
7276
try {
7377
const html = await renderUrl(renderUrlConfig);
7478
const { revalidate, errors } = getRouteISRDataFromHTML(html);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CacheHandler,
33
InvalidateConfig,
44
ISRHandlerConfig,
5+
ModifyHtmlCallbackFn,
56
RenderConfig,
67
ServeFromCacheConfig,
78
VariantRebuildItem,
@@ -228,6 +229,23 @@ export class ISRHandler {
228229
next: NextFunction,
229230
config?: RenderConfig,
230231
): Promise<Response | void> {
232+
// TODO: remove this in a major as a BREAKING CHANGE
233+
if (config?.modifyGeneratedHtml) {
234+
if (this.isrConfig.modifyGeneratedHtml !== undefined) {
235+
console.warn(
236+
'You can only specify `modifyGeneratedHtml` once. The one in render function will be removed in the next version.',
237+
);
238+
}
239+
const patchedModifyFn: ModifyHtmlCallbackFn = (
240+
req: Request,
241+
html: string,
242+
validate?: number | null,
243+
) => {
244+
return config!.modifyGeneratedHtml!(req, html);
245+
};
246+
this.isrConfig['modifyGeneratedHtml'] = patchedModifyFn;
247+
}
248+
231249
try {
232250
const result = await this.cacheGeneration.generate(
233251
req,

libs/isr/server/src/modify-generated-html.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { ModifyHtmlCallbackFn } from '@rx-angular/isr/models';
12
import { Request } from 'express';
2-
import { ModifyHtmlCallbackFn } from '../../models/src';
33

44
export const defaultModifyGeneratedHtml: ModifyHtmlCallbackFn = (
55
req: Request,

0 commit comments

Comments
 (0)