fix client side env variable refresh

This commit is contained in:
Philipinho 2025-02-03 21:34:38 +00:00
parent 0f74f03264
commit 7ee7ba9ba3

View File

@ -4,6 +4,7 @@ import { join } from 'path';
import * as fs from 'node:fs';
import fastifyStatic from '@fastify/static';
import { EnvironmentService } from '../environment/environment.service';
import { fileExistsSync } from 'tsconfig-paths/lib/filesystem';
@Module({})
export class StaticModule implements OnModuleInit {
@ -25,20 +26,28 @@ export class StaticModule implements OnModuleInit {
'client/dist',
);
if (fs.existsSync(clientDistPath)) {
const indexFilePath = join(clientDistPath, 'index.html');
const indexFilePath = join(clientDistPath, 'index.html');
if (fs.existsSync(clientDistPath) && fileExistsSync(indexFilePath)) {
const indexTemplateFilePath = join(clientDistPath, 'index-template.html');
const windowVar = '<!--window-config-->';
const configString = {
ENV: this.environmentService.getNodeEnv(),
APP_URL: this.environmentService.getAppUrl(),
IS_CLOUD: this.environmentService.isCloud(),
FILE_UPLOAD_SIZE_LIMIT: this.environmentService.getFileUploadSizeLimit(),
DRAWIO_URL: this.environmentService.getDrawioUrl()
CLOUD: this.environmentService.isCloud(),
FILE_UPLOAD_SIZE_LIMIT:
this.environmentService.getFileUploadSizeLimit(),
DRAWIO_URL: this.environmentService.getDrawioUrl(),
};
const windowScriptContent = `<script>window.CONFIG=${JSON.stringify(configString)};</script>`;
const html = fs.readFileSync(indexFilePath, 'utf8');
if (!fs.existsSync(indexTemplateFilePath)) {
fs.copyFileSync(indexFilePath, indexTemplateFilePath);
}
const html = fs.readFileSync(indexTemplateFilePath, 'utf8');
const transformedHtml = html.replace(windowVar, windowScriptContent);
fs.writeFileSync(indexFilePath, transformedHtml);