mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 15:33:04 +00:00
Merge pull request #863 from atmire/SSR-preboot-and-error-handling
Fixes for preboot and SSR error handling
This commit is contained in:
50
server.ts
50
server.ts
@@ -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,37 +130,7 @@ 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
|
|
||||||
// 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,
|
req,
|
||||||
res,
|
res,
|
||||||
@@ -171,16 +140,17 @@ function ngApp(req, res) {
|
|||||||
baseUrl: environment.ui.nameSpace,
|
baseUrl: environment.ui.nameSpace,
|
||||||
originUrl: environment.ui.baseUrl,
|
originUrl: environment.ui.baseUrl,
|
||||||
requestUrl: req.originalUrl
|
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 {
|
} 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>`
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user