Skip to content

Commit b2f58fd

Browse files
author
Andrea Barbasso
committed
[UXP-114] split service into SSR and CSR
1 parent 08fd4bf commit b2f58fd

5 files changed

Lines changed: 142 additions & 70 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Injectable } from '@angular/core';
2+
import { Observable, ReplaySubject, Subject } from 'rxjs';
3+
import { environment } from 'src/environments/environment';
4+
import { MathJaxConfig, MathService } from './math.service';
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class ClientMathService extends MathService {
10+
11+
protected signal: Subject<boolean>;
12+
13+
protected mathJaxOptions = {
14+
tex: {
15+
inlineMath: [['$', '$'], ['\\(', '\\)']]
16+
},
17+
svg: {
18+
fontCache: 'global'
19+
},
20+
startup: {
21+
typeset: false
22+
}
23+
};
24+
25+
protected mathJax: MathJaxConfig = {
26+
source: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js',
27+
id: 'MathJaxScript'
28+
};
29+
protected mathJaxFallback: MathJaxConfig = {
30+
source: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-chtml.min.js',
31+
id: 'MathJaxBackupScript'
32+
};
33+
34+
constructor() {
35+
super();
36+
37+
this.signal = new ReplaySubject<boolean>();
38+
39+
void this.registerMathJaxAsync(this.mathJax)
40+
.then(() => this.signal.next(true))
41+
.catch(_ => {
42+
void this.registerMathJaxAsync(this.mathJaxFallback)
43+
.then(() => this.signal.next(true))
44+
.catch((error) => console.log(error));
45+
});
46+
}
47+
48+
protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
49+
if (environment.markdown.mathjax) {
50+
return new Promise<void>((resolve, reject) => {
51+
52+
const optionsScript: HTMLScriptElement = document.createElement('script');
53+
optionsScript.type = 'text/javascript';
54+
optionsScript.text = `MathJax = ${JSON.stringify(this.mathJaxOptions)};`;
55+
document.head.appendChild(optionsScript);
56+
57+
const script: HTMLScriptElement = document.createElement('script');
58+
script.id = config.id;
59+
script.type = 'text/javascript';
60+
script.src = config.source;
61+
script.crossOrigin = 'anonymous';
62+
script.async = true;
63+
script.onload = () => resolve();
64+
script.onerror = error => reject(error);
65+
document.head.appendChild(script);
66+
});
67+
}
68+
return Promise.resolve();
69+
}
70+
71+
ready(): Observable<boolean> {
72+
return this.signal;
73+
}
74+
75+
render(element: HTMLElement) {
76+
if (environment.markdown.mathjax) {
77+
(window as any).MathJax.typesetPromise([element]);
78+
}
79+
}
80+
}
Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,16 @@
1-
import { Injectable } from '@angular/core';
2-
import { Observable, ReplaySubject, Subject } from 'rxjs';
1+
import { Observable } from 'rxjs';
32

4-
interface MathJaxConfig {
3+
export interface MathJaxConfig {
54
source: string;
65
id: string;
76
}
87

9-
@Injectable({
10-
providedIn: 'root'
11-
})
12-
export class MathService {
8+
export abstract class MathService {
9+
protected abstract mathJaxOptions: any;
10+
protected abstract mathJax: MathJaxConfig;
11+
protected abstract mathJaxFallback: MathJaxConfig;
1312

14-
private signal: Subject<boolean>;
15-
16-
private mathJaxOptions = {
17-
tex: {
18-
inlineMath: [['$', '$'], ['\\(', '\\)']]
19-
},
20-
svg: {
21-
fontCache: 'global'
22-
},
23-
startup: {
24-
typeset: false
25-
}
26-
};
27-
28-
private mathJax: MathJaxConfig = {
29-
source: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js',
30-
id: 'MathJaxScript'
31-
};
32-
private mathJaxFallback: MathJaxConfig = {
33-
source: 'assets/mathjax/mml-chtml.js',
34-
id: 'MathJaxBackupScript'
35-
};
36-
37-
constructor() {
38-
39-
this.signal = new ReplaySubject<boolean>();
40-
41-
void this.registerMathJaxAsync(this.mathJax)
42-
.then(() => this.signal.next(true))
43-
.catch(_ => {
44-
void this.registerMathJaxAsync(this.mathJaxFallback)
45-
.then(() => this.signal.next(true))
46-
.catch((error) => console.log(error));
47-
});
48-
}
49-
50-
private async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
51-
return new Promise<void>((resolve, reject) => {
52-
53-
const optionsScript: HTMLScriptElement = document.createElement('script');
54-
optionsScript.type = 'text/javascript';
55-
optionsScript.text = `MathJax = ${JSON.stringify(this.mathJaxOptions)};`;
56-
document.head.appendChild(optionsScript);
57-
58-
const script: HTMLScriptElement = document.createElement('script');
59-
script.id = config.id;
60-
script.type = 'text/javascript';
61-
script.src = config.source;
62-
script.crossOrigin = 'anonymous';
63-
script.async = true;
64-
script.onload = () => resolve();
65-
script.onerror = error => reject(error);
66-
document.head.appendChild(script);
67-
});
68-
}
69-
70-
ready(): Observable<boolean> {
71-
return this.signal;
72-
}
73-
74-
render(element: HTMLElement) {
75-
(window as any).MathJax.typesetPromise([element]);
76-
}
13+
protected abstract registerMathJaxAsync(config: MathJaxConfig): Promise<any>;
14+
abstract ready(): Observable<boolean>;
15+
abstract render(element: HTMLElement): void;
7716
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Injectable } from '@angular/core';
2+
import { Observable, ReplaySubject, Subject } from 'rxjs';
3+
import { MathJaxConfig, MathService } from './math.service';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class ServerMathService extends MathService {
9+
10+
protected signal: Subject<boolean>;
11+
12+
protected mathJaxOptions = {};
13+
14+
protected mathJax: MathJaxConfig = {
15+
source: '',
16+
id: ''
17+
};
18+
protected mathJaxFallback: MathJaxConfig = {
19+
source: '',
20+
id: ''
21+
};
22+
23+
constructor() {
24+
super();
25+
26+
this.signal = new ReplaySubject<boolean>();
27+
this.signal.next(true);
28+
}
29+
30+
protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
31+
return Promise.resolve();
32+
}
33+
34+
ready(): Observable<boolean> {
35+
return this.signal;
36+
}
37+
38+
render(element: HTMLElement) {
39+
return;
40+
}
41+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import { BrowserAuthRequestService } from '../../app/core/auth/browser-auth-requ
3333
import { BrowserInitService } from './browser-init.service';
3434
import { ReferrerService } from '../../app/core/services/referrer.service';
3535
import { BrowserReferrerService } from '../../app/core/services/browser.referrer.service';
36+
import { MathService } from '../../app/core/shared/math.service';
37+
import { ClientMathService } from '../../app/core/shared/client-math.service';
3638

3739
export const REQ_KEY = makeStateKey<string>('req');
3840

@@ -116,6 +118,10 @@ export function getRequest(transferState: TransferState): any {
116118
{
117119
provide: LocationToken,
118120
useFactory: locationProvider,
121+
},
122+
{
123+
provide: MathService,
124+
useClass: ClientMathService
119125
}
120126
]
121127
})

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import { XhrFactory } from '@angular/common';
3737
import { ServerXhrService } from '../../app/core/services/server-xhr.service';
3838
import { ReferrerService } from '../../app/core/services/referrer.service';
3939
import { ServerReferrerService } from '../../app/core/services/server.referrer.service';
40+
import { MathService } from '../../app/core/shared/math.service';
41+
import { ServerMathService } from '../../app/core/shared/server-math.service';
4042

4143
export function createTranslateLoader(transferState: TransferState) {
4244
return new TranslateServerLoader(transferState, 'dist/server/assets/i18n/', '.json');
@@ -116,6 +118,10 @@ export function createTranslateLoader(transferState: TransferState) {
116118
provide: ReferrerService,
117119
useClass: ServerReferrerService,
118120
},
121+
{
122+
provide: MathService,
123+
useClass: ServerMathService
124+
}
119125
]
120126
})
121127
export class ServerAppModule {

0 commit comments

Comments
 (0)