@@ -18,6 +18,8 @@ import * as fs from 'fs';
1818import { dirname , resolve } from 'path' ;
1919import { URL } from 'url' ;
2020
21+ const SSG_MARKER_REGEXP = / n g - s e r v e r - c o n t e x t = [ " ' ] \w * \| ? s s g \| ? \w * [ " ' ] / ;
22+
2123export interface RenderOptions {
2224 bootstrap ?: Type < { } > | ( ( ) => Promise < ApplicationRef > ) ;
2325 providers ?: StaticProvider [ ] ;
@@ -44,7 +46,7 @@ export interface RenderOptions {
4446export 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