1717
1818import 'zone.js/node' ;
1919import 'reflect-metadata' ;
20- import 'rxjs' ;
2120
2221/* eslint-disable import/no-namespace */
2322import * as morgan from 'morgan' ;
@@ -40,26 +39,25 @@ import { join } from 'path';
4039import { enableProdMode } from '@angular/core' ;
4140
4241
43-
4442import { environment } from './src/environments/environment' ;
4543import { createProxyMiddleware } from 'http-proxy-middleware' ;
46- import { hasNoValue , hasValue } from './src/app/shared/empty.util' ;
47-
44+ import { hasValue } from './src/app/shared/empty.util' ;
4845import { UIServerConfig } from './src/config/ui-server-config.interface' ;
49-
5046import bootstrap from './src/main.server' ;
51-
5247import { buildAppConfig } from './src/config/config.server' ;
53- import { APP_CONFIG , AppConfig } from './src/config/app-config.interface' ;
48+ import {
49+ APP_CONFIG ,
50+ AppConfig ,
51+ } from './src/config/app-config.interface' ;
5452import { extendEnvironmentWithAppConfig } from './src/config/config.util' ;
5553import { logStartupMessage } from './startup-message' ;
5654import { TOKENITEM } from './src/app/core/auth/models/auth-token-info.model' ;
5755import { CommonEngine } from '@angular/ssr' ;
5856import { APP_BASE_HREF } from '@angular/common' ;
59- import { REQUEST , RESPONSE } from './src/express.tokens' ;
60- import { dirname , resolve } from 'node:path' ;
61- import { fileURLToPath } from 'node:url' ;
62-
57+ import {
58+ REQUEST ,
59+ RESPONSE ,
60+ } from './src/express.tokens' ;
6361
6462/*
6563 * Set path for the browser application's dist folder
@@ -92,9 +90,6 @@ export function app() {
9290 * Create a new express application
9391 */
9492 const server = express ( ) ;
95- const serverDistFolder = dirname ( fileURLToPath ( import . meta. url ) ) ;
96- const browserDistFolder = resolve ( serverDistFolder , '../browser' ) ;
97- const commonEngine = new CommonEngine ( ) ;
9893
9994 // Tell Express to trust X-FORWARDED-* headers from proxies
10095 // See https://expressjs.com/en/guide/behind-proxies.html
@@ -134,27 +129,6 @@ export function app() {
134129 */
135130 server . use ( json ( ) ) ;
136131
137- // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
138- /* server.engine('html', (_, options, callback) =>
139- ngExpressEngine({
140- bootstrap,
141- providers: [
142- {
143- provide: REQUEST,
144- useValue: (options as any).req,
145- },
146- {
147- provide: RESPONSE,
148- useValue: (options as any).req.res,
149- },
150- {
151- provide: APP_CONFIG,
152- useValue: environment,
153- },
154- ],
155- })(_, (options as any), callback),
156- );*/
157-
158132 server . engine ( 'ejs' , ejs . renderFile ) ;
159133
160134 /*
@@ -234,12 +208,7 @@ export function app() {
234208 * copy of the page (see cacheCheck())
235209 */
236210 router . get ( '*' , cacheCheck , ngApp ) ;
237- // All regular routes use the Angular engine
238- // server.get('*', (req, res, next) => {
239- // const { protocol, originalUrl, baseUrl, headers } = req;
240- //
241- //
242- // });
211+
243212 server . use ( environment . ui . nameSpace , router ) ;
244213
245214 return server ;
@@ -249,7 +218,7 @@ export function app() {
249218 * The callback function to serve server side angular
250219 */
251220function ngApp ( req , res , next ) {
252- if ( environment . universal . preboot ) {
221+ if ( environment . ssr . enabled ) {
253222 // Render the page to user via SSR (server side rendering)
254223 serverSideRender ( req , res , next ) ;
255224 } else {
@@ -264,17 +233,19 @@ function ngApp(req, res, next) {
264233 * returned to the user.
265234 * @param req current request
266235 * @param res current response
236+ * @param next the next function
267237 * @param sendToUser if true (default), send the rendered content to the user.
268238 * If false, then only save this rendered content to the in-memory cache (to refresh cache).
269239 */
270240function serverSideRender ( req , res , next , sendToUser : boolean = true ) {
271241 const { protocol, originalUrl, baseUrl, headers } = req ;
272- const commonEngine = new CommonEngine ( ) ;
242+ const commonEngine = new CommonEngine ( { enablePerformanceProfiler : environment . ssr . enablePerformanceProfiler } ) ;
273243 // Render the page via SSR (server side rendering)
274244 commonEngine
275245 . render ( {
276246 bootstrap,
277247 documentFilePath : indexHtml ,
248+ inlineCriticalCss : environment . ssr . inlineCriticalCss ,
278249 url : `${ protocol } ://${ headers . host } ${ originalUrl } ` ,
279250 publicPath : DIST_FOLDER ,
280251 providers : [
@@ -293,44 +264,35 @@ function serverSideRender(req, res, next, sendToUser: boolean = true) {
293264 } ,
294265 ] ,
295266 } )
296- . then ( ( html ) => res . send ( html ) )
297- . catch ( ( err ) => next ( err ) ) ;
298-
299-
300- /* res.render(indexHtml, {
301- req,
302- res,
303- preboot: environment.universal.preboot,
304- async: environment.universal.async,
305- time: environment.universal.time,
306- baseUrl: environment.ui.nameSpace,
307- originUrl: environment.ui.baseUrl,
308- requestUrl: req.originalUrl,
309- }, (err, data) => {
310- if (hasNoValue(err) && hasValue(data)) {
311- // save server side rendered page to cache (if any are enabled)
312- saveToCache(req, data);
313- if (sendToUser) {
314- res.locals.ssr = true; // mark response as SSR (enables text compression)
315- // send rendered page to user
316- res.send(data);
317- }
318- } else if (hasValue(err) && err.code === 'ERR_HTTP_HEADERS_SENT') {
319- // When this error occurs we can't fall back to CSR because the response has already been
320- // sent. These errors occur for various reasons in universal, not all of which are in our
321- // control to solve.
322- console.warn('Warning [ERR_HTTP_HEADERS_SENT]: Tried to set headers after they were sent to the client');
323- } else {
324- console.warn('Error in server-side rendering (SSR)');
325- if (hasValue(err)) {
326- console.warn('Error details : ', err);
267+ . then ( ( html ) => {
268+ if ( hasValue ( html ) ) {
269+ // save server side rendered page to cache (if any are enabled)
270+ saveToCache ( req , html ) ;
271+ if ( sendToUser ) {
272+ res . locals . ssr = true ; // mark response as SSR (enables text compression)
273+ // send rendered page to user
274+ res . send ( html ) ;
275+ }
327276 }
328- if (sendToUser) {
329- console.warn('Falling back to serving direct client-side rendering (CSR).');
330- clientSideRender(req, res);
277+ } )
278+ . catch ( ( err ) => {
279+ if ( hasValue ( err ) && err . code === 'ERR_HTTP_HEADERS_SENT' ) {
280+ // When this error occurs we can't fall back to CSR because the response has already been
281+ // sent. These errors occur for various reasons in universal, not all of which are in our
282+ // control to solve.
283+ console . warn ( 'Warning [ERR_HTTP_HEADERS_SENT]: Tried to set headers after they were sent to the client' ) ;
284+ } else {
285+ console . warn ( 'Error in server-side rendering (SSR)' ) ;
286+ if ( hasValue ( err ) ) {
287+ console . warn ( 'Error details : ' , err ) ;
288+ }
289+ if ( sendToUser ) {
290+ console . warn ( 'Falling back to serving direct client-side rendering (CSR).' ) ;
291+ clientSideRender ( req , res ) ;
292+ }
331293 }
332- }
333- });*/
294+ next ( err ) ;
295+ } ) ;
334296}
335297
336298/**
@@ -388,7 +350,7 @@ function initCache() {
388350function botCacheEnabled ( ) : boolean {
389351 // Caching is only enabled if SSR is enabled AND
390352 // "max" pages to cache is greater than zero
391- return environment . universal . preboot && environment . cache . serverSide . botCache . max && ( environment . cache . serverSide . botCache . max > 0 ) ;
353+ return environment . ssr . enabled && environment . cache . serverSide . botCache . max && ( environment . cache . serverSide . botCache . max > 0 ) ;
392354}
393355
394356/**
@@ -397,7 +359,7 @@ function botCacheEnabled(): boolean {
397359function anonymousCacheEnabled ( ) : boolean {
398360 // Caching is only enabled if SSR is enabled AND
399361 // "max" pages to cache is greater than zero
400- return environment . universal . preboot && environment . cache . serverSide . anonymousCache . max && ( environment . cache . serverSide . anonymousCache . max > 0 ) ;
362+ return environment . ssr . enabled && environment . cache . serverSide . anonymousCache . max && ( environment . cache . serverSide . anonymousCache . max > 0 ) ;
401363}
402364
403365/**
@@ -410,9 +372,9 @@ function cacheCheck(req, res, next) {
410372
411373 // If the bot cache is enabled and this request looks like a bot, check the bot cache for a cached page.
412374 if ( botCacheEnabled ( ) && isbot ( req . get ( 'user-agent' ) ) ) {
413- cachedCopy = checkCacheForRequest ( 'bot' , botCache , req , res ) ;
375+ cachedCopy = checkCacheForRequest ( 'bot' , botCache , req , res , next ) ;
414376 } else if ( anonymousCacheEnabled ( ) && ! isUserAuthenticated ( req ) ) {
415- cachedCopy = checkCacheForRequest ( 'anonymous' , anonymousCache , req , res ) ;
377+ cachedCopy = checkCacheForRequest ( 'anonymous' , anonymousCache , req , res , next ) ;
416378 }
417379
418380 // If cached copy exists, return it to the user.
@@ -448,9 +410,10 @@ function cacheCheck(req, res, next) {
448410 * @param cache LRU cache to check
449411 * @param req current request to look for in the cache
450412 * @param res current response
413+ * @param next the next function
451414 * @returns cached copy (if found) or undefined (if not found)
452415 */
453- function checkCacheForRequest ( cacheName : string , cache : LRU < string , any > , req , res ) : any {
416+ function checkCacheForRequest ( cacheName : string , cache : LRU < string , any > , req , res , next ) : any {
454417 // Get the cache key for this request
455418 const key = getCacheKey ( req ) ;
456419
@@ -466,7 +429,7 @@ function checkCacheForRequest(cacheName: string, cache: LRU<string, any>, req, r
466429 // Update cached copy by rerendering server-side
467430 // NOTE: In this scenario the currently cached copy will be returned to the current user.
468431 // This re-render is peformed behind the scenes to update cached copy for next user.
469- serverSideRender ( req , res , null , false ) ;
432+ serverSideRender ( req , res , next , false ) ;
470433 }
471434 } else {
472435 if ( environment . cache . serverSide . debug ) { console . log ( `CACHE MISS FOR ${ key } in ${ cacheName } cache.` ) ; }
@@ -570,7 +533,7 @@ function createHttpsServer(keys) {
570533 const listener = createServer ( {
571534 key : keys . serviceKey ,
572535 cert : keys . certificate ,
573- } , app ) . listen ( environment . ui . port , environment . ui . host , ( ) => {
536+ } , app ( ) ) . listen ( environment . ui . port , environment . ui . host , ( ) => {
574537 serverStarted ( ) ;
575538 } ) ;
576539
0 commit comments