fixed preboot issues and error handling

This commit is contained in:
lotte
2020-09-09 11:41:24 +02:00
parent 3f4a0d1fd9
commit cced7a2914
5 changed files with 12 additions and 37 deletions

View File

@@ -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,55 +130,28 @@ 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 // If preboot is enabled, create a new zone for SSR, and
// register the error handler for when it throws an error // 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,
preboot: environment.universal.preboot,
async: environment.universal.async, async: environment.universal.async,
time: environment.universal.time, time: environment.universal.time,
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. 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 side ejs template and pass it the required
// variables // 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>`
});
} }
} }

View File

@@ -1,6 +1,7 @@
import { Config } from './config.interface'; import { Config } from './config.interface';
export interface UniversalConfig extends Config { export interface UniversalConfig extends Config {
preboot: boolean;
async: boolean; async: boolean;
time: boolean; time: boolean;
} }

View File

@@ -121,6 +121,7 @@ export const environment: GlobalConfig = {
}, },
// Angular Universal settings // Angular Universal settings
universal: { universal: {
preboot: true,
async: true, async: true,
time: false time: false
}, },

View File

@@ -105,6 +105,7 @@ export const environment: Partial<GlobalConfig> = {
}, },
// Angular Universal settings // Angular Universal settings
universal: { universal: {
preboot: true,
async: true, async: true,
time: false time: false
}, },

View File

@@ -45,7 +45,7 @@ function addGoogleAnalytics() {
} }
// support async tag or hmr // support async tag or hmr
if (environment.production === false) { if (hasValue(environment.universal) && environment.universal.preboot === false) {
bootloader(main); bootloader(main);
} else { } else {
document.addEventListener('DOMContentLoaded', () => bootloader(main)); document.addEventListener('DOMContentLoaded', () => bootloader(main));