From ecfd475256c33e1bb459802055870bc52aee3654 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Tue, 13 Sep 2022 21:34:04 +0200 Subject: [PATCH] Fix missing assets during local development --- apps/api/src/app/frontend.middleware.ts | 38 ++++++++++++++++++------- package.json | 2 +- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/apps/api/src/app/frontend.middleware.ts b/apps/api/src/app/frontend.middleware.ts index c45a35749..b650555fd 100644 --- a/apps/api/src/app/frontend.middleware.ts +++ b/apps/api/src/app/frontend.middleware.ts @@ -4,22 +4,36 @@ import * as path from 'path'; import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config'; import { Injectable, NestMiddleware } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { NextFunction, Request, Response } from 'express'; @Injectable() export class FrontendMiddleware implements NestMiddleware { - public indexHtmlDe = fs.readFileSync( - this.getPathOfIndexHtmlFile('de'), - 'utf8' - ); - public indexHtmlEn = fs.readFileSync( - this.getPathOfIndexHtmlFile(DEFAULT_LANGUAGE_CODE), - 'utf8' - ); + public indexHtmlDe = ''; + public indexHtmlEn = ''; + public isProduction: boolean; public constructor( + private readonly configService: ConfigService, private readonly configurationService: ConfigurationService - ) {} + ) { + const NODE_ENV = + this.configService.get<'development' | 'production'>('NODE_ENV') ?? + 'development'; + + this.isProduction = NODE_ENV === 'production'; + + try { + this.indexHtmlDe = fs.readFileSync( + this.getPathOfIndexHtmlFile('de'), + 'utf8' + ); + this.indexHtmlEn = fs.readFileSync( + this.getPathOfIndexHtmlFile(DEFAULT_LANGUAGE_CODE), + 'utf8' + ); + } catch {} + } public use(req: Request, res: Response, next: NextFunction) { let featureGraphicPath = 'assets/cover.png'; @@ -31,7 +45,11 @@ export class FrontendMiddleware implements NestMiddleware { featureGraphicPath = 'assets/images/blog/500-stars-on-github.jpg'; } - if (req.path.startsWith('/api/') || this.isFileRequest(req.url)) { + if ( + req.path.startsWith('/api/') || + this.isFileRequest(req.url) || + !this.isProduction + ) { // Skip next(); } else if (req.path === '/de' || req.path.startsWith('/de/')) { diff --git a/package.json b/package.json index 5bd4074b3..cca00ae1d 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "lint": "nx workspace-lint && ng lint", "ng": "nx", "nx": "nx", - "postinstall": "prisma generate && ngcc --properties es2020 browser module main", + "postinstall": "prisma generate && ngcc --properties es2020 browser module main && nx run client:build:production --localize", "replace-placeholders-in-build": "node ./replace.build.js", "start": "node dist/apps/api/main", "start:client": "ng serve client --configuration=development-en --hmr -o",