Skip to content

Commit 840b26e

Browse files
author
ahmetkuslular
committed
ADD History service
1 parent 6482ec9 commit 840b26e

File tree

6 files changed

+80
-5
lines changed

6 files changed

+80
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"file-loader": "1.1.11",
6161
"helmet": "3.21.3",
6262
"hiddie": "^1.0.0",
63+
"history": "^5.3.0",
6364
"husky": "^3.1.0",
6465
"identity-obj-proxy": "3.0.0",
6566
"intersection-observer": "0.7.0",

src/render.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const render = async (req, res) => {
2323
.split('/')
2424
.filter(part => part);
2525
const componentPath = `/${pathParts.join('/')}`;
26+
const isPreviewValue = isPreview(req.query);
2627

2728
const routeInfo = matchUrlInRouteConfigs(componentPath);
2829

@@ -38,7 +39,8 @@ const render = async (req, res) => {
3839
.replace('//', '/'),
3940
userAgent: Buffer.from(req.headers['user-agent'] || [], 'utf-8').toString('base64'),
4041
headers: JSON.parse(xss(JSON.stringify(req.headers))),
41-
isWithoutState: isWithoutStateValue
42+
isWithoutState: isWithoutStateValue,
43+
isPreview: isPreviewValue
4244
};
4345

4446
const component = new Component(routeInfo.path);

src/universal/partials/Welcome/partials.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Object.keys(components).forEach(path => {
1010
partials.push({
1111
name: info.fragmentName,
1212
url: path,
13-
status: info.status
13+
status: info?.fragment?.previewStatus || info.status
1414
});
1515
}
1616
});

src/universal/partials/withBaseComponent.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ClientApp from '../components/ClientApp';
55
import { WINDOW_GLOBAL_PARAMS } from '../utils/constants';
66
import { createComponentName } from '../utils/helper';
77
import voltranConfig from '../../../voltran.config';
8+
import HistoryService from '../service/HistoryService';
89

910
const getStaticProps = () => {
1011
const staticProps = {};
@@ -24,7 +25,12 @@ const withBaseComponent = (PageComponent, pathName) => {
2425

2526
if (process.env.BROWSER && window[prefix] && window[prefix][componentName.toUpperCase()]) {
2627
const fragments = window[prefix][componentName.toUpperCase()];
27-
const history = window[WINDOW_GLOBAL_PARAMS.HISTORY];
28+
29+
window[WINDOW_GLOBAL_PARAMS.VOLTRAN_HISTORY] =
30+
window[WINDOW_GLOBAL_PARAMS.VOLTRAN_HISTORY] ||
31+
new HistoryService(WINDOW_GLOBAL_PARAMS.VOLTRAN_HISTORY);
32+
const history = window[WINDOW_GLOBAL_PARAMS.VOLTRAN_HISTORY].getHistory();
33+
2834
const staticProps = getStaticProps();
2935

3036
Object.keys(fragments).forEach(id => {
@@ -37,7 +43,11 @@ const withBaseComponent = (PageComponent, pathName) => {
3743

3844
ReactDOM.hydrate(
3945
<ClientApp>
40-
<PageComponent {...staticProps} initialState={initialState} history={history} />
46+
<PageComponent
47+
{...staticProps}
48+
initialState={initialState}
49+
history={history}
50+
/>
4151
</ClientApp>,
4252
componentEl,
4353
() => {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createBrowserHistory } from 'history';
2+
import { WINDOW_GLOBAL_PARAMS } from '../utils/constants';
3+
4+
let instance;
5+
6+
const setReferrer = referrer => {
7+
if (process.env.BROWSER) {
8+
window.ref = referrer;
9+
}
10+
};
11+
12+
export default class HistoryService {
13+
constructor(historyKey) {
14+
if (instance) {
15+
return instance;
16+
}
17+
18+
this.current = '';
19+
this.previous = '';
20+
this.history = null;
21+
this.historyKey = historyKey;
22+
this._listenCallback = this._listenCallback.bind(this);
23+
24+
this._selectHistoryKey();
25+
26+
if (process.env.BROWSER) {
27+
this.current = window.location.href;
28+
this.history = window[this.historyKey];
29+
30+
if (this.historyKey === historyKey) {
31+
this.history.listen(this._listenCallback);
32+
}
33+
}
34+
35+
instance = this;
36+
}
37+
38+
_listenCallback(location) {
39+
if (!process.env.BROWSER) return;
40+
41+
this.previous = this.current;
42+
this.current = `${window.location.origin}${location.pathname}${location.search}${location.hash}`;
43+
setReferrer(this.previous);
44+
}
45+
46+
_selectHistoryKey() {
47+
if (!process.env.BROWSER) return;
48+
49+
// eslint-disable-next-line no-prototype-builtins
50+
if (!window.hasOwnProperty(WINDOW_GLOBAL_PARAMS.HISTORY)) {
51+
window[this.historyKey] = createBrowserHistory();
52+
} else {
53+
this.historyKey = WINDOW_GLOBAL_PARAMS.HISTORY;
54+
}
55+
}
56+
57+
getHistory() {
58+
return this.history;
59+
}
60+
}

src/universal/utils/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const appConfig = require('__APP_CONFIG__');
2+
const voltranConfig = require('../../../voltran.config');
23

34
const WINDOW_GLOBAL_PARAMS = {
4-
HISTORY: 'storefront.pwa.mobile.global.history'
5+
HISTORY: 'storefront.pwa.mobile.global.history',
6+
VOLTRAN_HISTORY: voltranConfig.historyKey || 'voltran.global.history'
57
};
68

79
const HTTP_STATUS_CODES = {

0 commit comments

Comments
 (0)