Browse Source

Fix missing assets during local development

pull/1253/head
Thomas 3 years ago
parent
commit
ecfd475256
  1. 38
      apps/api/src/app/frontend.middleware.ts
  2. 2
      package.json

38
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 { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config'; import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config';
import { Injectable, NestMiddleware } from '@nestjs/common'; import { Injectable, NestMiddleware } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NextFunction, Request, Response } from 'express'; import { NextFunction, Request, Response } from 'express';
@Injectable() @Injectable()
export class FrontendMiddleware implements NestMiddleware { export class FrontendMiddleware implements NestMiddleware {
public indexHtmlDe = fs.readFileSync( public indexHtmlDe = '';
this.getPathOfIndexHtmlFile('de'), public indexHtmlEn = '';
'utf8' public isProduction: boolean;
);
public indexHtmlEn = fs.readFileSync(
this.getPathOfIndexHtmlFile(DEFAULT_LANGUAGE_CODE),
'utf8'
);
public constructor( public constructor(
private readonly configService: ConfigService,
private readonly configurationService: ConfigurationService 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) { public use(req: Request, res: Response, next: NextFunction) {
let featureGraphicPath = 'assets/cover.png'; let featureGraphicPath = 'assets/cover.png';
@ -31,7 +45,11 @@ export class FrontendMiddleware implements NestMiddleware {
featureGraphicPath = 'assets/images/blog/500-stars-on-github.jpg'; 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 // Skip
next(); next();
} else if (req.path === '/de' || req.path.startsWith('/de/')) { } else if (req.path === '/de' || req.path.startsWith('/de/')) {

2
package.json

@ -37,7 +37,7 @@
"lint": "nx workspace-lint && ng lint", "lint": "nx workspace-lint && ng lint",
"ng": "nx", "ng": "nx",
"nx": "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", "replace-placeholders-in-build": "node ./replace.build.js",
"start": "node dist/apps/api/main", "start": "node dist/apps/api/main",
"start:client": "ng serve client --configuration=development-en --hmr -o", "start:client": "ng serve client --configuration=development-en --hmr -o",

Loading…
Cancel
Save