Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit c0b0418

Browse files
committed
fix(@nguniversal/common): check for server context when doing hybrid rendering
Prior to this change, we did not check for the server context which is some cases caused the app-shell to be served instead of the SSR version. (cherry picked from commit 98ea74f)
1 parent aa8ca19 commit c0b0418

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

modules/common/engine/src/engine.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import * as fs from 'fs';
1818
import { dirname, resolve } from 'path';
1919
import { URL } from 'url';
2020

21+
const SSG_MARKER_REGEXP = /ng-server-context=["']\w*\|?ssg\|?\w*["']/;
22+
2123
export interface RenderOptions {
2224
bootstrap?: Type<{}> | (() => Promise<ApplicationRef>);
2325
providers?: StaticProvider[];
@@ -44,7 +46,7 @@ export interface RenderOptions {
4446
export class CommonEngine {
4547
private readonly templateCache = new Map<string, string>();
4648
private readonly inlineCriticalCssProcessor: InlineCriticalCssProcessor;
47-
private readonly pageExists = new Map<string, boolean>();
49+
private readonly pageIsSSG = new Map<string, boolean>();
4850

4951
constructor(
5052
private bootstrap?: Type<{}> | (() => Promise<ApplicationRef>),
@@ -70,13 +72,20 @@ export class CommonEngine {
7072

7173
if (pagePath !== resolve(opts.documentFilePath)) {
7274
// View path doesn't match with prerender path.
73-
let pageExists = this.pageExists.get(pagePath);
74-
if (pageExists === undefined) {
75-
pageExists = await exists(pagePath);
76-
this.pageExists.set(pagePath, pageExists);
77-
}
78-
79-
if (pageExists) {
75+
const pageIsSSG = this.pageIsSSG.get(pagePath);
76+
if (pageIsSSG === undefined) {
77+
if (await exists(pagePath)) {
78+
const content = await fs.promises.readFile(pagePath, 'utf-8');
79+
const isSSG = SSG_MARKER_REGEXP.test(content);
80+
this.pageIsSSG.set(pagePath, isSSG);
81+
82+
if (isSSG) {
83+
return content;
84+
}
85+
} else {
86+
this.pageIsSSG.set(pagePath, false);
87+
}
88+
} else if (pageIsSSG) {
8089
// Serve pre-rendered page.
8190
return fs.promises.readFile(pagePath, 'utf-8');
8291
}

0 commit comments

Comments
 (0)