@@ -18,6 +18,8 @@ import * as fs from 'fs';
18
18
import { dirname , resolve } from 'path' ;
19
19
import { URL } from 'url' ;
20
20
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
+
21
23
export interface RenderOptions {
22
24
bootstrap ?: Type < { } > | ( ( ) => Promise < ApplicationRef > ) ;
23
25
providers ?: StaticProvider [ ] ;
@@ -44,7 +46,7 @@ export interface RenderOptions {
44
46
export class CommonEngine {
45
47
private readonly templateCache = new Map < string , string > ( ) ;
46
48
private readonly inlineCriticalCssProcessor : InlineCriticalCssProcessor ;
47
- private readonly pageExists = new Map < string , boolean > ( ) ;
49
+ private readonly pageIsSSG = new Map < string , boolean > ( ) ;
48
50
49
51
constructor (
50
52
private bootstrap ?: Type < { } > | ( ( ) => Promise < ApplicationRef > ) ,
@@ -70,13 +72,20 @@ export class CommonEngine {
70
72
71
73
if ( pagePath !== resolve ( opts . documentFilePath ) ) {
72
74
// 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 ) {
80
89
// Serve pre-rendered page.
81
90
return fs . promises . readFile ( pagePath , 'utf-8' ) ;
82
91
}
0 commit comments