From cced7a291462b1adec3dd6c25003e62f99fee4b5 Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 9 Sep 2020 11:41:24 +0200 Subject: [PATCH] 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));