From e032aec9b92be25a996923361f83a96a89505254 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 18 Jul 2018 09:52:46 +0200 Subject: Render CSS/title/description tags on server side --- client/src/app/app.component.ts | 42 ++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'client/src/app') diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index fc4d6c6a2..2149768a2 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -4,6 +4,7 @@ import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router' import { AuthService, RedirectService, ServerService } from '@app/core' import { is18nPath } from '../../../shared/models/i18n' import { ScreenService } from '@app/shared/misc/screen.service' +import { skip } from 'rxjs/operators' @Component({ selector: 'my-app', @@ -89,25 +90,36 @@ export class AppComponent implements OnInit { } ) + // Inject JS this.serverService.configLoaded - .subscribe(() => { - const config = this.serverService.getConfig() + .subscribe(() => { + const config = this.serverService.getConfig() + + if (config.instance.customizations.javascript) { + try { + // tslint:disable:no-eval + eval(config.instance.customizations.javascript) + } catch (err) { + console.error('Cannot eval custom JavaScript.', err) + } + } + }) - // We test customCSS if the admin removed the css - if (this.customCSS || config.instance.customizations.css) { - const styleTag = '' - this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) - } + // Inject CSS if modified (admin config settings) + this.serverService.configLoaded + .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server + .subscribe(() => { + const headStyle = document.querySelector('style.custom-css-style') + if (headStyle) headStyle.parentNode.removeChild(headStyle) - if (config.instance.customizations.javascript) { - try { - // tslint:disable:no-eval - eval(config.instance.customizations.javascript) - } catch (err) { - console.error('Cannot eval custom JavaScript.', err) + const config = this.serverService.getConfig() + + // We test customCSS if the admin removed the css + if (this.customCSS || config.instance.customizations.css) { + const styleTag = '' + this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) } - } - }) + }) } isUserLoggedIn () { -- cgit v1.2.3