Skip to content

Commit

Permalink
fix(@nguniversal/common): check for server context when doing hybrid …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
alan-agius4 committed Aug 9, 2023
1 parent aa8ca19 commit c0b0418
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions modules/common/engine/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import * as fs from 'fs';
import { dirname, resolve } from 'path';
import { URL } from 'url';

const SSG_MARKER_REGEXP = /ng-server-context=["']\w*\|?ssg\|?\w*["']/;

export interface RenderOptions {
bootstrap?: Type<{}> | (() => Promise<ApplicationRef>);
providers?: StaticProvider[];
Expand All @@ -44,7 +46,7 @@ export interface RenderOptions {
export class CommonEngine {
private readonly templateCache = new Map<string, string>();
private readonly inlineCriticalCssProcessor: InlineCriticalCssProcessor;
private readonly pageExists = new Map<string, boolean>();
private readonly pageIsSSG = new Map<string, boolean>();

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

if (pagePath !== resolve(opts.documentFilePath)) {
// View path doesn't match with prerender path.
let pageExists = this.pageExists.get(pagePath);
if (pageExists === undefined) {
pageExists = await exists(pagePath);
this.pageExists.set(pagePath, pageExists);
}

if (pageExists) {
const pageIsSSG = this.pageIsSSG.get(pagePath);
if (pageIsSSG === undefined) {
if (await exists(pagePath)) {
const content = await fs.promises.readFile(pagePath, 'utf-8');
const isSSG = SSG_MARKER_REGEXP.test(content);
this.pageIsSSG.set(pagePath, isSSG);

if (isSSG) {
return content;
}
} else {
this.pageIsSSG.set(pagePath, false);
}
} else if (pageIsSSG) {
// Serve pre-rendered page.
return fs.promises.readFile(pagePath, 'utf-8');
}
Expand Down

0 comments on commit c0b0418

Please sign in to comment.