1- import { Injectable } from '@angular/core' ;
2- import * as Klaro from 'klaro' ;
1+ import { Inject , Injectable , InjectionToken } from '@angular/core' ;
32import { combineLatest as observableCombineLatest , Observable , of as observableOf } from 'rxjs' ;
43import { AuthService } from '../../core/auth/auth.service' ;
54import { TranslateService } from '@ngx-translate/core' ;
@@ -42,6 +41,17 @@ const cookiePurposeMessagePrefix = 'cookies.consent.purpose.';
4241 */
4342const updateDebounce = 300 ;
4443
44+ /**
45+ * By using this injection token instead of importing directly we can keep Klaro out of the main bundle
46+ */
47+ const LAZY_KLARO = new InjectionToken < Promise < any > > (
48+ 'Lazily loaded Klaro' ,
49+ {
50+ providedIn : 'root' ,
51+ factory : async ( ) => ( await import ( 'klaro' ) ) ,
52+ }
53+ ) ;
54+
4555/**
4656 * Browser implementation for the KlaroService, representing a service for handling Klaro consent preferences and UI
4757 */
@@ -64,7 +74,9 @@ export class BrowserKlaroService extends KlaroService {
6474 private authService : AuthService ,
6575 private ePersonService : EPersonDataService ,
6676 private configService : ConfigurationDataService ,
67- private cookieService : CookieService ) {
77+ private cookieService : CookieService ,
78+ @Inject ( LAZY_KLARO ) private lazyKlaro : Promise < any > ,
79+ ) {
6880 super ( ) ;
6981 }
7082
@@ -134,8 +146,7 @@ export class BrowserKlaroService extends KlaroService {
134146 this . translateConfiguration ( ) ;
135147
136148 this . klaroConfig . services = this . filterConfigServices ( servicesToHide ) ;
137-
138- Klaro . setup ( this . klaroConfig ) ;
149+ this . lazyKlaro . then ( ( { setup } ) => setup ( this . klaroConfig ) ) ;
139150 } ) ;
140151 }
141152
@@ -219,7 +230,7 @@ export class BrowserKlaroService extends KlaroService {
219230 * Show the cookie consent form
220231 */
221232 showSettings ( ) {
222- Klaro . show ( this . klaroConfig ) ;
233+ this . lazyKlaro . then ( ( { show} ) => show ( this . klaroConfig ) ) ;
223234 }
224235
225236 /**
0 commit comments