Fix direct CSR

By moving from environment.ts to config.yml we made it so the environment is _not_ up to date with the server configuration when main.js is first loaded.
Because of this the app behaved as if CSR always happened _after_ SSR, effectively breaking direct CSR.

Here the "criterion" for SSR/non-SSR HTML is changed from the related configuration property to the presence of Angular Universal transfer state.

This means we can correctly determine when to bootstrap the app for direct CSR, and it's' now "safe" to just send index.html by itself.
This commit is contained in:
Yury Bondarenko
2023-02-07 15:01:01 +01:00
parent ca864379c8
commit 88cb397dc9
3 changed files with 18 additions and 41 deletions

View File

@@ -31,7 +31,6 @@ import * as expressStaticGzip from 'express-static-gzip';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { APP_BASE_HREF } from '@angular/common';
import { enableProdMode } from '@angular/core';
import { ngExpressEngine } from '@nguniversal/express-engine';
@@ -57,7 +56,7 @@ const DIST_FOLDER = join(process.cwd(), 'dist/browser');
// Set path fir IIIF viewer.
const IIIF_VIEWER = join(process.cwd(), 'dist/iiif');
const indexHtml = existsSync(join(DIST_FOLDER, 'index.html')) ? 'index.html' : 'index';
const indexHtml = join(DIST_FOLDER, 'index.html');
const cookieParser = require('cookie-parser');
@@ -207,7 +206,6 @@ function ngApp(req, res) {
baseUrl: environment.ui.nameSpace,
originUrl: environment.ui.baseUrl,
requestUrl: req.originalUrl,
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }]
}, (err, data) => {
if (hasNoValue(err) && hasValue(data)) {
res.locals.ssr = true; // mark response as SSR
@@ -222,25 +220,15 @@ function ngApp(req, res) {
if (hasValue(err)) {
console.warn('Error details : ', err);
}
res.render(indexHtml, {
req,
providers: [{
provide: APP_BASE_HREF,
useValue: req.baseUrl
}]
});
res.sendFile(indexHtml);
}
});
} else {
// If preboot is disabled, just serve the client
console.log('Universal off, serving for direct CSR');
res.render(indexHtml, {
req,
providers: [{
provide: APP_BASE_HREF,
useValue: req.baseUrl
}]
});
res.sendFile(indexHtml);
}
}