Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(isr): use modifyGeneratedHtml in all cache generation process #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 31, 2024
Prev Previous commit
Next Next commit
feat(isr): update the example to show modifyGeneratedHtml usage
  • Loading branch information
maxisam authored and eneajaho committed Aug 31, 2024
commit 378159da813ca3d6af20feb87af65d2b95816e3c
20 changes: 19 additions & 1 deletion 20 apps/ssr-isr/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommonEngine } from '@angular/ssr';
import { modifyHtmlCallbackFn } from '@rx-angular/isr/models';
import { ISRHandler } from '@rx-angular/isr/server';
import express from 'express';
import express, { Request } from 'express';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { RESPONSE } from './src/app/redirect.component';
Expand Down Expand Up @@ -30,6 +31,7 @@ export function app(): express.Express {
browserDistFolder,
bootstrap,
commonEngine,
modifyGeneratedHtml: customModifyGeneratedHtml,
// cache: fsCacheHandler,
});

Expand Down Expand Up @@ -72,6 +74,22 @@ export function app(): express.Express {
return server;
}

const customModifyGeneratedHtml: modifyHtmlCallbackFn = (
req: Request,
html: string,
revalidateTime?: number | null,
): string => {
const time = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');

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

function run(): void {
const port = process.env['PORT'] || 4000;

Expand Down
21 changes: 14 additions & 7 deletions 21 apps/ssr-isr/src/app/dynamic.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import { map, switchMap } from 'rxjs';
selector: 'app-dynamic-page',
template: `
@if (post$ | async; as post) {
<div>
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
</div>
}
<div>
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
<h2>
Dynamically Modification (controlled by modifyGeneratedHtml in
ISRHandlerConfig)
</h2>
<p>Original content</p>
</div>
}
`,
imports: [AsyncPipe],
standalone: true,
Expand All @@ -22,14 +29,14 @@ export class DynamicPageComponent {
private http = inject(HttpClient);

private postId$ = inject(ActivatedRoute).params.pipe(
map((p) => p['id'] as string)
map((p) => p['id'] as string),
);

post$ = this.postId$.pipe(
switchMap((id) =>
this.http.get<{ title: string; body: string }>(
`https://jsonplaceholder.typicode.com/posts/${id}`
)
)
`https://jsonplaceholder.typicode.com/posts/${id}`,
),
),
);
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.