Merge pull request #863 from atmire/SSR-preboot-and-error-handling

Fixes for preboot and SSR error handling
This commit is contained in:
Tim Donohue
2020-09-16 16:06:58 -05:00
committed by GitHub

View File

@@ -15,7 +15,6 @@
* import for `ngExpressEngine`. * import for `ngExpressEngine`.
*/ */
import 'zone.js/dist/zone-node';
import 'reflect-metadata'; import 'reflect-metadata';
import 'rxjs'; import 'rxjs';
@@ -34,6 +33,7 @@ import { enableProdMode, NgModuleFactory, Type } from '@angular/core';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens'; import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import { environment } from './src/environments/environment'; import { environment } from './src/environments/environment';
import { createProxyMiddleware } from 'http-proxy-middleware'; import { createProxyMiddleware } from 'http-proxy-middleware';
import { hasValue } from './src/app/shared/empty.util';
/* /*
* Set path for the browser application's dist folder * Set path for the browser application's dist folder
@@ -99,7 +99,6 @@ app.engine('html', (_, options, callback) =>
/* /*
* Register the view engines for html and ejs * Register the view engines for html and ejs
*/ */
app.set('view engine', 'ejs');
app.set('view engine', 'html'); app.set('view engine', 'html');
/* /*
@@ -131,56 +130,27 @@ app.get('*.*', cacheControl, express.static(DIST_FOLDER, { index: false }));
* The callback function to serve server side angular * The callback function to serve server side angular
*/ */
function ngApp(req, res) { 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: `<script>window.dspace = ${JSON.stringify(dspace)}</script>`
});
}
}
if (environment.universal.preboot) { if (environment.universal.preboot) {
// If preboot is enabled, create a new zone for SSR, and res.render(DIST_FOLDER + '/index.html', {
// register the error handler for when it throws an error req,
Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => { res,
res.render(DIST_FOLDER + '/index.html', { preboot: environment.universal.preboot,
req, async: environment.universal.async,
res, time: environment.universal.time,
preboot: environment.universal.preboot, baseUrl: environment.ui.nameSpace,
async: environment.universal.async, originUrl: environment.ui.baseUrl,
time: environment.universal.time, requestUrl: req.originalUrl
baseUrl: environment.ui.nameSpace, }, (err) => {
originUrl: environment.ui.baseUrl, console.warn('Error in SSR, serving for direct CSR.');
requestUrl: req.originalUrl if (hasValue(err)) {
}); console.warn('Error details : ', err);
}); }
res.sendFile(DIST_FOLDER + '/index.html');
})
} else { } else {
// If preboot is disabled, just serve the client side ejs template and pass it the required // If preboot is disabled, just serve the client
// variables
console.log('Universal off, serving for direct CSR'); console.log('Universal off, serving for direct CSR');
res.render('index-csr.ejs', { res.sendFile(DIST_FOLDER + '/index.html');
root: DIST_FOLDER,
scripts: `<script>window.dspace = ${JSON.stringify(dspace)}</script>`
});
} }
} }