Skip to content

Commit 127d960

Browse files
atarix83Andrea Barbasso
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-1570-fix-rum (pull request DSpace#1748)
Fix several issue with SSR Approved-by: Andrea Barbasso
2 parents 8d16e8b + 484e75e commit 127d960

File tree

11 files changed

+120
-77
lines changed

11 files changed

+120
-77
lines changed

src/app/app.component.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { of } from 'rxjs';
3838
import { APP_CONFIG } from '../config/app-config.interface';
3939
import { environment } from '../environments/environment';
4040
import { KlaroService } from './shared/cookies/klaro.service';
41+
import { DatadogRumService } from './shared/datadog-rum/datadog-rum.service';
4142

4243
let comp: AppComponent;
4344
let fixture: ComponentFixture<AppComponent>;
@@ -57,6 +58,7 @@ describe('App component', () => {
5758
let breadcrumbsServiceSpy;
5859
let routeServiceMock;
5960
let klaroServiceSpy: jasmine.SpyObj<KlaroService>;
61+
let datadogRumServiceSpy: jasmine.SpyObj<DatadogRumService>;
6062

6163
const getDefaultTestBedConf = () => {
6264
breadcrumbsServiceSpy = jasmine.createSpyObj(['listenForRouteChanges']);
@@ -71,6 +73,11 @@ describe('App component', () => {
7173
consentsUpdates$: of({})
7274
});
7375

76+
datadogRumServiceSpy = jasmine.createSpyObj('DatadogRumService', {
77+
initDatadogRum: jasmine.createSpy('initDatadogRum'),
78+
getDatadogRumState: jasmine.createSpy('getDatadogRumState')
79+
});
80+
7481
return {
7582
imports: [
7683
CommonModule,
@@ -99,6 +106,7 @@ describe('App component', () => {
99106
{ provide: RouteService, useValue: routeServiceMock },
100107
{ provide: APP_CONFIG, useValue: environment },
101108
{ provide: KlaroService, useValue: klaroServiceSpy },
109+
{ provide: DatadogRumService, useValue: datadogRumServiceSpy },
102110
provideMockStore({ initialState }),
103111
AppComponent,
104112
// RouteService

src/app/app.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ export class AppComponent implements OnInit, AfterViewInit {
111111

112112
this.dispatchWindowSize(this._window.nativeWindow.innerWidth, this._window.nativeWindow.innerHeight);
113113

114-
if (isPlatformBrowser(this.platformId)) {
115-
this.datadogRumService.initDatadogRum();
116-
}
114+
this.datadogRumService.initDatadogRum();
117115
}
118116

119117
private storeCSSVariables() {

src/app/core/metadata/metadata.service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ xdescribe('MetadataService', () => {
123123
appConfig,
124124
authorizationService,
125125
schemaJsonLDService,
126-
'browser'
126+
'browser',
127+
null
127128
);
128129
});
129130

src/app/core/metadata/metadata.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { getDownloadableBitstream } from '../shared/bitstream.operators';
4242
import { APP_CONFIG, AppConfig } from '../../../config/app-config.interface';
4343
import { SchemaJsonLDService } from './schema-json-ld/schema-json-ld.service';
4444
import { ITEM } from '../shared/item.resource-type';
45-
import { isPlatformServer } from '@angular/common';
45+
import { DOCUMENT, isPlatformServer } from '@angular/common';
4646
import { Root } from '../data/root.model';
4747
import { environment } from '../../../environments/environment';
4848

@@ -102,6 +102,7 @@ export class MetadataService {
102102
private authorizationService: AuthorizationDataService,
103103
private schemaJsonLDService: SchemaJsonLDService,
104104
@Inject(PLATFORM_ID) private platformId: any,
105+
@Inject(DOCUMENT) private _document: Document,
105106
) {
106107
}
107108

@@ -656,11 +657,11 @@ export class MetadataService {
656657
}
657658

658659
private getMetaTagValue(key: string): string {
659-
return this.currentObject.value.firstMetadataValue(key);
660+
return this.currentObject?.value?.firstMetadataValue(key);
660661
}
661662

662663
private getFirstMetaTagValue(keys: string[]): string {
663-
return this.currentObject.value.firstMetadataValue(keys);
664+
return this.currentObject?.value?.firstMetadataValue(keys);
664665
}
665666

666667
private getMetaTagValuesAndCombine(key: string): string {
@@ -741,7 +742,7 @@ export class MetadataService {
741742

742743

743744
private setGenericPageMetaTags() {
744-
const pageDocumentTitle = document.getElementsByTagName('title')[0].innerText;
745+
const pageDocumentTitle = this._document.getElementsByTagName('title')[0].innerText;
745746
const pageUrl = new URLCombiner(this.hardRedirectService.getCurrentOrigin(), this.router.url).toString();
746747
const genericPageOpenGraphType = 'website';
747748

src/app/core/services/internal-link.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NativeWindowRef, NativeWindowService } from './window.service';
77
*/
88
@Injectable()
99
export class InternalLinkService {
10-
currentURL = this._window.nativeWindow.location.origin;
10+
currentURL = this._window.nativeWindow?.location?.origin;
1111

1212
constructor(
1313
@Inject(NativeWindowService) protected _window: NativeWindowRef,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Injectable } from '@angular/core';
2+
import { environment } from '../../../environments/environment';
3+
import { datadogRum } from '@datadog/browser-rum';
4+
import { CookieConsents, KlaroService } from '../cookies/klaro.service';
5+
import { BehaviorSubject, Observable } from 'rxjs';
6+
import { createSelector, Store } from '@ngrx/store';
7+
import { setDatadogRumStatusAction } from './datadog-rum.actions';
8+
import { DatadogRumState } from './datadog-rum.reducer';
9+
import { distinctUntilChanged, take } from 'rxjs/operators';
10+
import { coreSelector } from '../../core/core.selectors';
11+
import { CoreState } from '../../core/core-state.model';
12+
import { DatadogRumService } from './datadog-rum.service';
13+
14+
@Injectable()
15+
export class BrowserDatadogRumService extends DatadogRumService {
16+
17+
consentsUpdates$: BehaviorSubject<CookieConsents>;
18+
datadogRumStateSelector = createSelector(coreSelector, (state: CoreState) => state.datadogRum);
19+
20+
constructor(
21+
private klaroService: KlaroService,
22+
private store: Store
23+
) {
24+
super();
25+
}
26+
27+
initDatadogRum() {
28+
this.klaroService.watchConsentUpdates();
29+
this.consentsUpdates$ = this.klaroService.consentsUpdates$;
30+
this.consentsUpdates$.subscribe(savedPreferences => {
31+
this.getDatadogRumState().subscribe((state) => {
32+
if (savedPreferences?.datadog &&
33+
environment.datadogRum?.clientToken && environment.datadogRum?.applicationId &&
34+
environment.datadogRum?.service && environment.datadogRum?.env) {
35+
if (!state.isInitialized) {
36+
this.store.dispatch(new setDatadogRumStatusAction({
37+
isInitialized: true,
38+
isRunning: true
39+
}));
40+
datadogRum.init(environment.datadogRum);
41+
} else if (!state.isRunning) {
42+
this.store.dispatch(new setDatadogRumStatusAction({
43+
isRunning: true
44+
}));
45+
datadogRum.startSessionReplayRecording();
46+
}
47+
} else {
48+
datadogRum.stopSessionReplayRecording();
49+
this.store.dispatch(new setDatadogRumStatusAction({
50+
isRunning: false
51+
}));
52+
}
53+
});
54+
});
55+
}
56+
57+
58+
getDatadogRumState(): Observable<DatadogRumState> {
59+
return this.store
60+
.select(this.datadogRumStateSelector)
61+
.pipe(
62+
distinctUntilChanged(),
63+
take(1),
64+
);
65+
}
66+
}
67+

src/app/shared/datadog-rum/datadog-rum.service.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { CookieConsents, KlaroService } from '../cookies/klaro.service';
55
import { of } from 'rxjs';
66
import { environment } from '../../../environments/environment';
77
import { setDatadogRumStatusAction } from './datadog-rum.actions';
8+
import { BrowserDatadogRumService } from './browser-datadog-rum.service';
89

910
describe('DatadogRumService', () => {
10-
let service: DatadogRumService;
11+
let service: BrowserDatadogRumService;
1112
let store: MockStore;
1213
let klaroService: KlaroService;
1314
let memoizedSelector;
@@ -40,12 +41,12 @@ describe('DatadogRumService', () => {
4041
beforeEach(() => {
4142
TestBed.configureTestingModule({
4243
providers: [
43-
DatadogRumService,
44-
provideMockStore({initialState}),
45-
{provide: KlaroService, useValue: klaroServiceSpy},
44+
{ provide: DatadogRumService, useClass: BrowserDatadogRumService },
45+
provideMockStore({ initialState }),
46+
{ provide: KlaroService, useValue: klaroServiceSpy },
4647
]
4748
});
48-
service = TestBed.inject(DatadogRumService);
49+
service = TestBed.inject(DatadogRumService) as BrowserDatadogRumService;
4950
store = TestBed.inject(MockStore);
5051
memoizedSelector = store.overrideSelector(service.datadogRumStateSelector, initialState.datadogRum);
5152
klaroService = TestBed.inject(KlaroService);
Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,11 @@
11
import { Injectable } from '@angular/core';
2-
import { environment } from '../../../environments/environment';
3-
import { datadogRum } from '@datadog/browser-rum';
4-
import { CookieConsents, KlaroService } from '../cookies/klaro.service';
5-
import { BehaviorSubject, Observable } from 'rxjs';
6-
import { createSelector, Store } from '@ngrx/store';
7-
import { setDatadogRumStatusAction } from './datadog-rum.actions';
8-
import { DatadogRumState } from './datadog-rum.reducer';
9-
import { distinctUntilChanged, take } from 'rxjs/operators';
10-
import { coreSelector } from '../../core/core.selectors';
11-
import { CoreState } from '../../core/core-state.model';
122

13-
@Injectable({
14-
providedIn: 'root'
15-
})
16-
export class DatadogRumService {
3+
@Injectable()
4+
export abstract class DatadogRumService {
175

18-
consentsUpdates$: BehaviorSubject<CookieConsents>;
19-
datadogRumStateSelector = createSelector(coreSelector, (state: CoreState) => state.datadogRum);
20-
21-
constructor(
22-
private klaroService: KlaroService,
23-
private store: Store
24-
) {
25-
}
26-
27-
initDatadogRum() {
28-
this.klaroService.watchConsentUpdates();
29-
this.consentsUpdates$ = this.klaroService.consentsUpdates$;
30-
this.consentsUpdates$.subscribe(savedPreferences => {
31-
this.getDatadogRumState().subscribe((state) => {
32-
if (savedPreferences?.datadog &&
33-
environment.datadogRum?.clientToken && environment.datadogRum?.applicationId &&
34-
environment.datadogRum?.service && environment.datadogRum?.env) {
35-
if (!state.isInitialized) {
36-
this.store.dispatch(new setDatadogRumStatusAction({
37-
isInitialized: true,
38-
isRunning: true
39-
}));
40-
datadogRum.init(environment.datadogRum);
41-
} else if (!state.isRunning) {
42-
this.store.dispatch(new setDatadogRumStatusAction({
43-
isRunning: true
44-
}));
45-
datadogRum.startSessionReplayRecording();
46-
}
47-
} else {
48-
datadogRum.stopSessionReplayRecording();
49-
this.store.dispatch(new setDatadogRumStatusAction({
50-
isRunning: false
51-
}));
52-
}
53-
});
54-
});
55-
}
56-
57-
58-
getDatadogRumState(): Observable<DatadogRumState> {
59-
return this.store
60-
.select(this.datadogRumStateSelector)
61-
.pipe(
62-
distinctUntilChanged(),
63-
take(1),
64-
);
65-
}
6+
/**
7+
* Initializes the service
8+
*/
9+
abstract initDatadogRum();
6610
}
6711

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Injectable } from '@angular/core';
2+
import { DatadogRumService } from './datadog-rum.service';
3+
4+
@Injectable()
5+
export class ServerDatadogRumService extends DatadogRumService {
6+
7+
initDatadogRum() {
8+
return;
9+
}
10+
}
11+

src/modules/app/browser-app.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import { ReferrerService } from '../../app/core/services/referrer.service';
3535
import { BrowserReferrerService } from '../../app/core/services/browser.referrer.service';
3636
import { MathService } from '../../app/core/shared/math.service';
3737
import { ClientMathService } from '../../app/core/shared/client-math.service';
38+
import { DatadogRumService } from '../../app/shared/datadog-rum/datadog-rum.service';
39+
import { BrowserDatadogRumService } from '../../app/shared/datadog-rum/browser-datadog-rum.service';
3840

3941
export const REQ_KEY = makeStateKey<string>('req');
4042

@@ -87,6 +89,10 @@ export function getRequest(transferState: TransferState): any {
8789
provide: KlaroService,
8890
useClass: BrowserKlaroService
8991
},
92+
{
93+
provide: DatadogRumService,
94+
useClass: BrowserDatadogRumService
95+
},
9096
{
9197
provide: SubmissionService,
9298
useClass: SubmissionService

0 commit comments

Comments
 (0)