@cocreate/server-side-render 1.1.19 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/package.json +3 -4
- package/src/index.js +77 -66
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [1.2.0](https://github.com/CoCreate-app/CoCreate-server-side-render/compare/v1.1.20...v1.2.0) (2022-12-04)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* replace mongodb with @cocreate/crud-server ([684c835](https://github.com/CoCreate-app/CoCreate-server-side-render/commit/684c8357e59bf7622367bc8e10c7f2388b60a87c))
|
|
7
|
+
|
|
8
|
+
## [1.1.20](https://github.com/CoCreate-app/CoCreate-server-side-render/compare/v1.1.19...v1.1.20) (2022-12-02)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* docs ([f4479f4](https://github.com/CoCreate-app/CoCreate-server-side-render/commit/f4479f4ee375fefc6017581cc8d8642b998b879b))
|
|
14
|
+
|
|
1
15
|
## [1.1.19](https://github.com/CoCreate-app/CoCreate-server-side-render/compare/v1.1.18...v1.1.19) (2022-12-02)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/server-side-render",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A simple server-side-render component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"server-side-render",
|
|
@@ -61,9 +61,8 @@
|
|
|
61
61
|
"webpack-log": "^3.0.1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@cocreate/docs": "^1.4.
|
|
65
|
-
"@cocreate/hosting": "^1.6.
|
|
66
|
-
"mongodb": "^4.4.0",
|
|
64
|
+
"@cocreate/docs": "^1.4.11",
|
|
65
|
+
"@cocreate/hosting": "^1.6.9",
|
|
67
66
|
"node-html-parser": "^5.2.0"
|
|
68
67
|
}
|
|
69
68
|
}
|
package/src/index.js
CHANGED
|
@@ -1,76 +1,87 @@
|
|
|
1
1
|
const { parse } = require("node-html-parser");
|
|
2
|
-
const { ObjectId } = require('mongodb');
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
class CoCreateServerSideRender {
|
|
4
|
+
constructor(crud) {
|
|
5
|
+
this.crud = crud;
|
|
6
|
+
this.renderedIgnoreEl = { INPUT: true, TEXTAREA: true, SELECT: true, LINK: true, IFRAME: true, "COCREATE-SELECT": true }
|
|
7
|
+
}
|
|
5
8
|
|
|
6
|
-
|
|
9
|
+
async html(html) {
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
let dep = [];
|
|
12
|
+
let dbCache = new Map();
|
|
13
|
+
|
|
14
|
+
async function render(html, lastKey) {
|
|
15
|
+
const dom = parse(html);
|
|
16
|
+
for (let el of dom.querySelectorAll(
|
|
17
|
+
"[collection][name][document_id]"
|
|
18
|
+
)) {
|
|
19
|
+
let meta = el.attributes;
|
|
20
|
+
|
|
21
|
+
if (renderedIgnoreEl[el.tagName])
|
|
22
|
+
continue;
|
|
23
|
+
|
|
24
|
+
if (el.tagName == "DIV" && !el.classList.contains('domEditor'))
|
|
25
|
+
continue;
|
|
26
|
+
|
|
27
|
+
if (el.classList.contains('domEditor') && el.closest('.template'))
|
|
28
|
+
continue;
|
|
20
29
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
continue;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
30
|
+
if (el.hasAttribute('actions'))
|
|
31
|
+
continue;
|
|
32
|
+
|
|
33
|
+
let _id = meta["document_id"],
|
|
34
|
+
collection = meta['collection'],
|
|
35
|
+
name = meta['name'];
|
|
36
|
+
let key = _id + collection + name;
|
|
37
|
+
if (!_id || !name || !collection) continue;
|
|
38
|
+
if (dep.includes(key))
|
|
39
|
+
throw new Error(
|
|
40
|
+
`infinite loop: ${lastKey} ${_id} ${collection} ${name} has been already rendered`
|
|
41
|
+
);
|
|
42
|
+
else
|
|
43
|
+
dep.push(key)
|
|
44
|
+
|
|
45
|
+
let cacheKey = _id + collection;
|
|
46
|
+
let record;
|
|
47
|
+
if (dbCache.has(cacheKey))
|
|
48
|
+
record = dbCache.get(cacheKey)
|
|
49
|
+
else {
|
|
50
|
+
record = await this.crud.readDocument({
|
|
51
|
+
collection,
|
|
52
|
+
document: {
|
|
53
|
+
_id
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
record = record.document[0]
|
|
57
|
+
dbCache.set(cacheKey, record)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!record || !record[name]) {
|
|
61
|
+
dep.pop();
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
let chunk = record[name];
|
|
65
|
+
if (!chunk) {
|
|
66
|
+
|
|
67
|
+
dep.pop();
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
let dom = await render(chunk);
|
|
71
|
+
|
|
72
|
+
el.setAttribute('rendered', '')
|
|
73
|
+
el.innerHTML = "";
|
|
74
|
+
el.appendChild(dom);
|
|
75
|
+
|
|
76
|
+
|
|
53
77
|
dep.pop();
|
|
54
|
-
continue;
|
|
55
78
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
dep.pop();
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
let dom = await render(chunk);
|
|
63
|
-
|
|
64
|
-
el.setAttribute('rendered', '')
|
|
65
|
-
el.innerHTML = "";
|
|
66
|
-
el.appendChild(dom);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
dep.pop();
|
|
79
|
+
|
|
80
|
+
return dom;
|
|
70
81
|
}
|
|
71
|
-
|
|
72
|
-
return
|
|
82
|
+
let result = (await render(html, 'root')).toString();
|
|
83
|
+
return result;
|
|
73
84
|
}
|
|
74
|
-
let result = (await render(html, 'root')).toString();
|
|
75
|
-
return result;
|
|
76
85
|
}
|
|
86
|
+
|
|
87
|
+
module.exports = CoCreateServerSideRender;
|