From 3f4a0d1fd9738fce8e0a49acbfffdd051b87d990 Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 12 Aug 2020 14:41:30 +0200 Subject: [PATCH 1/3] removing proboot option from config --- server.ts | 1 - src/config/universal-config.interface.ts | 1 - src/environments/environment.common.ts | 1 - src/environments/mock-environment.ts | 1 - src/main.browser.ts | 2 +- 5 files changed, 1 insertion(+), 5 deletions(-) diff --git a/server.ts b/server.ts index 478dd063f6..a3d7ad3dd0 100644 --- a/server.ts +++ b/server.ts @@ -165,7 +165,6 @@ function ngApp(req, res) { res.render(DIST_FOLDER + '/index.html', { req, res, - preboot: environment.universal.preboot, async: environment.universal.async, time: environment.universal.time, baseUrl: environment.ui.nameSpace, diff --git a/src/config/universal-config.interface.ts b/src/config/universal-config.interface.ts index c088dcd657..d3bbf9d17c 100644 --- a/src/config/universal-config.interface.ts +++ b/src/config/universal-config.interface.ts @@ -1,7 +1,6 @@ import { Config } from './config.interface'; export interface UniversalConfig extends Config { - preboot: boolean; async: boolean; time: boolean; } diff --git a/src/environments/environment.common.ts b/src/environments/environment.common.ts index 32ae2f54b0..c665e6d3db 100644 --- a/src/environments/environment.common.ts +++ b/src/environments/environment.common.ts @@ -121,7 +121,6 @@ export const environment: GlobalConfig = { }, // Angular Universal settings universal: { - preboot: true, async: true, time: false }, diff --git a/src/environments/mock-environment.ts b/src/environments/mock-environment.ts index 6e4d60e268..f2253e307f 100644 --- a/src/environments/mock-environment.ts +++ b/src/environments/mock-environment.ts @@ -105,7 +105,6 @@ export const environment: Partial = { }, // Angular Universal settings universal: { - preboot: true, async: true, time: false }, diff --git a/src/main.browser.ts b/src/main.browser.ts index 5149014d88..70f13be6cc 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -45,7 +45,7 @@ function addGoogleAnalytics() { } // support async tag or hmr -if (hasValue(environment.universal) && environment.universal.preboot === false) { +if (environment.production === false) { bootloader(main); } else { document.addEventListener('DOMContentLoaded', () => bootloader(main)); From cced7a291462b1adec3dd6c25003e62f99fee4b5 Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 9 Sep 2020 11:41:24 +0200 Subject: [PATCH 2/3] fixed preboot issues and error handling --- server.ts | 44 +++++------------------- src/config/universal-config.interface.ts | 1 + src/environments/environment.common.ts | 1 + src/environments/mock-environment.ts | 1 + src/main.browser.ts | 2 +- 5 files changed, 12 insertions(+), 37 deletions(-) diff --git a/server.ts b/server.ts index a3d7ad3dd0..ca79215206 100644 --- a/server.ts +++ b/server.ts @@ -99,7 +99,6 @@ app.engine('html', (_, options, callback) => /* * Register the view engines for html and ejs */ -app.set('view engine', 'ejs'); app.set('view engine', 'html'); /* @@ -131,55 +130,28 @@ app.get('*.*', cacheControl, express.static(DIST_FOLDER, { index: false })); * The callback function to serve server side angular */ function ngApp(req, res) { - // Object to be set to window.dspace when CSR is used - // this allows us to pass the info in the original request - // to the dspace7-angular instance running in the client's browser - const dspace = { - originalRequest: { - headers: req.headers, - body: req.body, - method: req.method, - params: req.params, - reportProgress: req.reportProgress, - withCredentials: req.withCredentials, - responseType: req.responseType, - urlWithParams: req.urlWithParams - } - }; - - // callback function for the case when SSR throws an error. - function onHandleError(parentZoneDelegate, currentZone, targetZone, error) { - if (!res._headerSent) { - console.warn('Error in SSR, serving for direct CSR. Error details : ', error); - res.sendFile('index.csr.ejs', { - root: DIST_FOLDER, - scripts: `` - }); - } - } - if (environment.universal.preboot) { // If preboot is enabled, create a new zone for SSR, and // register the error handler for when it throws an error - Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => { - res.render(DIST_FOLDER + '/index.html', { + res.render(DIST_FOLDER + '/index.html', { req, res, + preboot: environment.universal.preboot, async: environment.universal.async, time: environment.universal.time, baseUrl: environment.ui.nameSpace, originUrl: environment.ui.baseUrl, requestUrl: req.originalUrl - }); - }); + }, (err) => { + console.warn('Error in SSR, serving for direct CSR. Error details : ', err); + res.sendFile(DIST_FOLDER + '/index.html'); + } + ); } else { // If preboot is disabled, just serve the client side ejs template and pass it the required // variables console.log('Universal off, serving for direct CSR'); - res.render('index-csr.ejs', { - root: DIST_FOLDER, - scripts: `` - }); + res.sendFile(DIST_FOLDER + '/index.html'); } } diff --git a/src/config/universal-config.interface.ts b/src/config/universal-config.interface.ts index d3bbf9d17c..c088dcd657 100644 --- a/src/config/universal-config.interface.ts +++ b/src/config/universal-config.interface.ts @@ -1,6 +1,7 @@ import { Config } from './config.interface'; export interface UniversalConfig extends Config { + preboot: boolean; async: boolean; time: boolean; } diff --git a/src/environments/environment.common.ts b/src/environments/environment.common.ts index c665e6d3db..32ae2f54b0 100644 --- a/src/environments/environment.common.ts +++ b/src/environments/environment.common.ts @@ -121,6 +121,7 @@ export const environment: GlobalConfig = { }, // Angular Universal settings universal: { + preboot: true, async: true, time: false }, diff --git a/src/environments/mock-environment.ts b/src/environments/mock-environment.ts index f2253e307f..6e4d60e268 100644 --- a/src/environments/mock-environment.ts +++ b/src/environments/mock-environment.ts @@ -105,6 +105,7 @@ export const environment: Partial = { }, // Angular Universal settings universal: { + preboot: true, async: true, time: false }, diff --git a/src/main.browser.ts b/src/main.browser.ts index 70f13be6cc..5149014d88 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -45,7 +45,7 @@ function addGoogleAnalytics() { } // support async tag or hmr -if (environment.production === false) { +if (hasValue(environment.universal) && environment.universal.preboot === false) { bootloader(main); } else { document.addEventListener('DOMContentLoaded', () => bootloader(main)); From a6dd87f78c84289fe269ef3f99b7064410a3c210 Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 9 Sep 2020 16:04:42 +0200 Subject: [PATCH 3/3] updated server.ts to latest fixes --- server.ts | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/server.ts b/server.ts index ca79215206..99e2b25e1e 100644 --- a/server.ts +++ b/server.ts @@ -15,7 +15,6 @@ * import for `ngExpressEngine`. */ -import 'zone.js/dist/zone-node'; import 'reflect-metadata'; import 'rxjs'; @@ -34,6 +33,7 @@ import { enableProdMode, NgModuleFactory, Type } from '@angular/core'; import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens'; import { environment } from './src/environments/environment'; import { createProxyMiddleware } from 'http-proxy-middleware'; +import { hasValue } from './src/app/shared/empty.util'; /* * Set path for the browser application's dist folder @@ -131,25 +131,24 @@ app.get('*.*', cacheControl, express.static(DIST_FOLDER, { index: false })); */ function ngApp(req, res) { if (environment.universal.preboot) { - // If preboot is enabled, create a new zone for SSR, and - // register the error handler for when it throws an error res.render(DIST_FOLDER + '/index.html', { - req, - res, - preboot: environment.universal.preboot, - async: environment.universal.async, - time: environment.universal.time, - baseUrl: environment.ui.nameSpace, - originUrl: environment.ui.baseUrl, - requestUrl: req.originalUrl - }, (err) => { - console.warn('Error in SSR, serving for direct CSR. Error details : ', err); - res.sendFile(DIST_FOLDER + '/index.html'); + req, + res, + preboot: environment.universal.preboot, + async: environment.universal.async, + time: environment.universal.time, + baseUrl: environment.ui.nameSpace, + originUrl: environment.ui.baseUrl, + requestUrl: req.originalUrl + }, (err) => { + console.warn('Error in SSR, serving for direct CSR.'); + if (hasValue(err)) { + console.warn('Error details : ', err); } - ); + res.sendFile(DIST_FOLDER + '/index.html'); + }) } else { - // If preboot is disabled, just serve the client side ejs template and pass it the required - // variables + // If preboot is disabled, just serve the client console.log('Universal off, serving for direct CSR'); res.sendFile(DIST_FOLDER + '/index.html'); }