From 8e7760aacd13d34473ca35da42259516fb939a1e Mon Sep 17 00:00:00 2001 From: nsemets Date: Thu, 19 Feb 2026 15:25:10 +0200 Subject: [PATCH] fix(ssr): updated logic for config in ssr --- src/app/core/services/osf-config.service.ts | 18 +++++++++++------- src/server.ts | 13 +++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/app/core/services/osf-config.service.ts b/src/app/core/services/osf-config.service.ts index 8a237ddd9..7034d30b0 100644 --- a/src/app/core/services/osf-config.service.ts +++ b/src/app/core/services/osf-config.service.ts @@ -43,20 +43,24 @@ export class OSFConfigService { * On the server, this is skipped as config is only needed in the browser. */ async load(): Promise { - if (!this.config && isPlatformBrowser(this.platformId)) { + if (this.config) return; + + if (isPlatformBrowser(this.platformId)) { this.config = await lastValueFrom( this.http.get('/assets/config/config.json').pipe( shareReplay(1), catchError(() => of({} as ConfigModel)) ) ); + } else { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + this.config = ((globalThis as any).__SSR_CONFIG__ ?? {}) as ConfigModel; + } - // Apply every key from config to environment - for (const [key, value] of Object.entries(this.config)) { - // eslint-disable-next-line - // @ts-ignore - this.environment[key] = value; - } + for (const [key, value] of Object.entries(this.config)) { + // eslint-disable-next-line + // @ts-ignore + this.environment[key] = value; } } } diff --git a/src/server.ts b/src/server.ts index 1a7ffc292..272569de9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,12 +6,25 @@ import { } from '@angular/ssr/node'; import express from 'express'; +import { existsSync, readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const serverDistFolder = dirname(fileURLToPath(import.meta.url)); const browserDistFolder = resolve(serverDistFolder, '../browser'); +const configPath = resolve(browserDistFolder, 'assets/config/config.json'); + +if (existsSync(configPath)) { + try { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (globalThis as any).__SSR_CONFIG__ = JSON.parse(readFileSync(configPath, 'utf-8')); + } catch { + // eslint-disable-next-line no-console + console.warn('Failed to parse SSR config at', configPath); + } +} + const app = express(); const angularApp = new AngularNodeAppEngine();