-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (104 loc) · 2.99 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.ssr = exports.getTemplates = exports.views = void 0;
var _morphdom = _interopRequireDefault(require("morphdom"));
var _lodash = require("lodash");
var _serializeJavascript = _interopRequireDefault(
require("serialize-javascript")
);
var _jsStringEscape = _interopRequireDefault(require("js-string-escape"));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
var views = function views(store, container, templates) {
var parser = new DOMParser();
var render = function render() {
console.time("render");
var currentState = store.getState();
var html = container.render(currentState, templates);
var prev = document.getElementById("root");
var next = prev.cloneNode(false);
var fragment = parser.parseFromString(html, "text/html").body;
Object.keys(fragment.childNodes).forEach(function() {
next.appendChild(fragment.firstElementChild);
});
(0, _morphdom.default)(prev, next, {
onBeforeElUpdated: function onBeforeElUpdated(fromEl, toEl) {
if (
fromEl.isEqualNode(toEl) ||
(fromEl.classList && fromEl.classList.contains("norend"))
) {
return false;
}
}
});
console.timeEnd("render");
};
store.subscribe(
(0, _lodash.debounce)(render, 0, {
leading: false,
trailing: true
})
);
return {
render: render
};
};
exports.views = views;
var getTemplates = function getTemplates(views) {
var templates = {};
var getName = function getName(name) {
return name
.split("/")
.pop()
.slice(0, -5);
};
if (views.keys().length < 1) {
throw Error("Templates are missing!");
}
views.keys().forEach(function(view) {
var name = getName(view);
if (templates[name]) {
throw new Error("Duplicate name of template: " + view);
}
templates[name] = views(view);
});
if (templates["root"] === undefined) {
throw new Error("Container is missing!");
}
var container = templates["root"];
delete templates["root"];
return {
templates: templates,
container: container
};
};
exports.getTemplates = getTemplates;
var ssr = function ssr(placeholder, container, templates, version) {
var render = function render(store, req, res, error) {
if (error === void 0) {
error = false;
}
var currentState = store.getState();
var html = container.render(currentState, templates);
var preloadedState = (0, _jsStringEscape.default)(
(0, _serializeJavascript.default)(currentState, {
isJSON: true
})
);
res.header("Content-Type", "text/html; charset=utf-8");
if (error) {
res.status(404).send(placeholder(html, preloadedState, version));
} else {
res.send(placeholder(html, preloadedState, version));
}
};
return {
render: render
};
};
exports.ssr = ssr;
var _default = views;
exports.default = _default;