This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerender.js
81 lines (69 loc) · 2.62 KB
/
prerender.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
const headChildrenPriorities = ['meta', 'title', 'link'];
const filteredHeadChildren = Array.from(document.head.children)
.filter(el => headChildrenPriorities.includes(el.nodeName.toLowerCase()));
filteredHeadChildren.sort((a) => {
if (a.nodeName.toLowerCase() === 'title') {
return -1
} else if (a.nodeName.toLowerCase() === 'meta'
&& b.nodeName.toLowerCase() === 'link') {
return -1;
} else if (a.nodeName.toLowerCase() === 'link'
&& b.nodeName.toLowerCase() !== 'link') {
return 1;
} else {
return 0;
}
});
filteredHeadChildren.sort((a) => {
if (a.nodeName.toLowerCase() === 'meta'
&& a.getAttributeNames().includes('charset')) {
return -1;
} else if (a.nodeName.toLowerCase() === 'link'
&& !a.getAttributeNames().includes('rel')) {
return 1;
} else {
return 0;
}
});
while (filteredHeadChildren.length > 0) {
document.head.insertBefore(filteredHeadChildren.pop(), document.head.childNodes[0]);
}
let ssrId = 0;
let textNodes = new Map();
const setSsrV = function (el) {
if (el && el.classList.contains('hydrated')) {
el.setAttribute('ssrv', ssrId++);
}
for (const child of el.children) {
setSsrV(child);
}
};
for (const child of document.body.children) {
setSsrV(child);
}
for (const element of document.body.getElementsByTagName('*')) {
const parent = element.parentElement.closest('.hydrated');
const parentId = parent ? parent.getAttribute('ssrv') : null;
for (const child of element.childNodes) {
if (child.nodeType === 3 && !child['s-cr'] && !textNodes.has(child)) {
textNodes.set(child, parent);
}
}
if (parentId) {
const childIdx = Array.from(element.parentElement.children).findIndex((entry) => entry === element);
element.setAttribute('ssrc', [parentId, childIdx].join('.'));
}
}
Array.from(textNodes.keys()).forEach((node) => {
if (node.nodeValue && node.nodeValue.trim()) {
const parent = textNodes.get(node);
const parentId = parent ? parent.getAttribute('ssrv') : null;
const childIdx = Array.from(node.parentElement.childNodes).findIndex((entry) => entry === node);
if (parentId) {
node.parentElement.insertBefore(document.createComment(['s', parentId, childIdx].join('.')), node);
node.parentElement.insertBefore(document.createComment('/'), node.nextSibling);
node.parentElement.insertBefore(document.createTextNode(' '), node.nextSibling);
}
}
});
document.querySelector('html').setAttribute('ssr', new Date().toISOString());