-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientHelper.js
53 lines (52 loc) · 1.5 KB
/
clientHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
export class SSRClientHelper{
/**
* Process the data injected by the SSRServerHelper
* @returns {any}
*/
static processData = () => {
const injectedData = document.getElementById('injected-data');
if (injectedData) {
console.log('SSR data detected, processing data..');
const data = injectedData.innerHTML;
if (data) {
const SSRObj = JSON.parse(decodeURIComponent(data));
if (injectedData.parentNode) {
injectedData.parentNode.removeChild(injectedData);
}
window.sessionStorage.setItem('SSRData', JSON.stringify(SSRObj));
return SSRObj;
}
}
}
/**
* Get an object of all the SSRed data
* @returns {*}
*/
static getMap = ()=> {
const SSRObj = window.sessionStorage.getItem('SSRData');
if (!SSRObj) {
return null;
}
return JSON.parse(SSRObj);
}
/**
* Get an SSRed data item with a given key
* @param key
* @returns {object}
*/
static getItem = key => {
const SSRObj = window.sessionStorage.getItem('SSRData');
if (!SSRObj) {
return null;
}
const dataObj = JSON.parse(SSRObj);
return dataObj[key]
};
/**
* Get the user passed by the server during SSR
* @returns {Object}
*/
static getUser = () => {
return SSRClientHelper.getItem('user')
}
}