From db33619f6b879e32db37d4eeaf990f36fd5a6edf Mon Sep 17 00:00:00 2001 From: Rebolon Date: Thu, 27 Jul 2023 14:22:02 +0200 Subject: [PATCH] fir prerender --- server.ts | 31 +++++++++++++------ .../list-cites/list-cites.component.ts | 3 +- src/app/tools/Device.ts | 5 ++- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/server.ts b/server.ts index 004e20d..ac78f21 100644 --- a/server.ts +++ b/server.ts @@ -11,12 +11,17 @@ import bootstrap from './src/main.server'; export function app(): express.Express { const server = express(); const distFolder = join(process.cwd(), 'dist/citations/browser'); - const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index'; + const indexHtml = existsSync(join(distFolder, 'index.original.html')) + ? 'index.original.html' + : 'index'; // Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine) - server.engine('html', ngExpressEngine({ - bootstrap - })); + server.engine( + 'html', + ngExpressEngine({ + bootstrap, + }) + ); server.set('view engine', 'html'); server.set('views', distFolder); @@ -24,13 +29,19 @@ export function app(): express.Express { // Example Express Rest API endpoints // server.get('/api/**', (req, res) => { }); // Serve static files from /browser - server.get('*.*', express.static(distFolder, { - maxAge: '1y' - })); + server.get( + '*.*', + express.static(distFolder, { + maxAge: '1y', + }) + ); // All regular routes use the Universal engine server.get('*', (req, res) => { - res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] }); + res.render(indexHtml, { + req, + providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }], + }); }); return server; @@ -51,9 +62,9 @@ function run(): void { // The below code is to ensure that the server is run only when not requiring the bundle. declare const __non_webpack_require__: NodeRequire; const mainModule = __non_webpack_require__.main; -const moduleFilename = mainModule && mainModule.filename || ''; +const moduleFilename = (mainModule && mainModule.filename) || ''; if (moduleFilename === __filename || moduleFilename.includes('iisnode')) { run(); } -export default bootstrap; \ No newline at end of file +export default bootstrap; diff --git a/src/app/components/list-cites/list-cites.component.ts b/src/app/components/list-cites/list-cites.component.ts index 2f75c6e..b188790 100644 --- a/src/app/components/list-cites/list-cites.component.ts +++ b/src/app/components/list-cites/list-cites.component.ts @@ -116,7 +116,8 @@ export class ListCitesComponent } protected findCitesBySearch() { - if (this.q.trim() === '') { + // prerender crash without the first check + if (this.q && this.q.trim() === '') { this.citeService.reset().subscribe(); return; diff --git a/src/app/tools/Device.ts b/src/app/tools/Device.ts index 012a5e9..d1a488c 100644 --- a/src/app/tools/Device.ts +++ b/src/app/tools/Device.ts @@ -3,7 +3,10 @@ import { Injectable } from '@angular/core'; @Injectable() export class Device { isMobile(): boolean { - return navigator.userAgent.includes('Mobile'); + // This check is for prerender prevent errors + return Object.keys(navigator).length + ? navigator.userAgent.includes('Mobile') + : false; } isDesktop(): boolean {