GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — :zap: faster heads · nullstack/nullstack@94f166c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • client
    • client.js
    • hydrate.js
    • index.js
    • rerender.js
  • server
    • prerender.js
    • render.js
  • shared
    • generateTree.js
  • tests
    • jest-puppeteer.config.js
    • src
      • BodyFragment.njs
      • DynamicHead.njs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎client/client.js‎

Copy file name to clipboardExpand all lines: client/client.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ client.generateContext = generateContext
2121
client.renderQueue = null
2222
client.currentBody = {}
2323
client.nextBody = {}
24+
client.currentHead = []
25+
client.nextHead = []
2426

2527
client.update = async function update() {
2628
if (client.initialized) {
@@ -33,10 +35,6 @@ client.update = async function update() {
3335
client.renewalQueue = []
3436
client.nextVirtualDom = await generateTree(client.initializer(), scope)
3537
rerender()
36-
client.virtualDom = client.nextVirtualDom
37-
client.nextVirtualDom = null
38-
client.currentBody = client.nextBody
39-
client.nextBody = {}
4038
client.processLifecycleQueues()
4139
}, 16)
4240
}
Collapse file

‎client/hydrate.js‎

Copy file name to clipboard
+24-7Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { isFalse } from '../shared/nodes';
12
import { anchorableElement } from './anchorableNode';
3+
import client from './client';
24

3-
export default function hydrate(selector, node) {
4-
if (node.head) {
5-
node.element = document.getElementById(node.attributes.id)
6-
return
7-
}
5+
let pool = []
6+
7+
function hydrateBody(selector, node) {
88
if (node?.attributes?.html) {
99
anchorableElement(selector);
1010
}
@@ -13,7 +13,7 @@ export default function hydrate(selector, node) {
1313
if ((element.tagName === 'TEXTAREA' || element.tagName === 'textarea') && element.childNodes.length === 0) {
1414
element.appendChild(document.createTextNode(''));
1515
} else if (element.COMMENT_NODE === 8 && element.textContent === '#') {
16-
element.remove()
16+
pool.push(element.remove())
1717
}
1818
}
1919
if (!node.children) return
@@ -26,6 +26,23 @@ export default function hydrate(selector, node) {
2626
)
2727
throw new Error('Virtual DOM does not match the DOM.')
2828
}
29-
hydrate(selector.childNodes[i], node.children[i])
29+
hydrateBody(selector.childNodes[i], node.children[i])
30+
}
31+
}
32+
33+
function hydrateHead() {
34+
for (const node of client.nextHead) {
35+
if (isFalse(node)) {
36+
node.element = pool.pop() || document.createComment("")
37+
document.head.append(node.element)
38+
} else {
39+
node.element = document.getElementById(node.attributes.id)
40+
}
3041
}
42+
pool = null
43+
}
44+
45+
export default function hydrate(selector, node) {
46+
hydrateBody(selector, node)
47+
hydrateHead()
3148
}
Collapse file

‎client/index.js‎

Copy file name to clipboardExpand all lines: client/index.js
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@ export default class Nullstack {
6464
client.selector = body
6565
} else {
6666
client.virtualDom = await generateTree(client.initializer(), scope);
67-
client.currentBody = client.nextBody
6867
hydrate(client.selector, client.virtualDom)
68+
client.currentBody = client.nextBody
69+
client.currentHead = client.nextHead
70+
client.nextBody = {}
71+
client.nextHead = []
6972
context.environment = environment;
7073
scope.plugins = loadPlugins(scope);
7174
worker.online = navigator.onLine;
7275
typeof context.start === 'function' && await context.start(context);
7376
client.nextVirtualDom = await generateTree(client.initializer(), scope);
7477
rerender();
75-
client.virtualDom = client.nextVirtualDom;
76-
client.nextVirtualDom = null;
7778
}
7879
client.processLifecycleQueues();
7980
delete state.context;
Collapse file

‎client/rerender.js‎

Copy file name to clipboardExpand all lines: client/rerender.js
+37-66Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,6 @@ import generateTruthyString from '../shared/generateTruthyString';
88
const head = document.head
99
const body = document.body
1010

11-
function getHeadElement(node) {
12-
if (node.element) {
13-
return node.element
14-
}
15-
node.element = document.getElementById(node.attributes.id)
16-
return node.element
17-
}
18-
19-
function removeHeadElement(node) {
20-
const element = getHeadElement(node)
21-
if (element) {
22-
element.remove()
23-
}
24-
}
25-
2611
function updateAttributes(selector, currentAttributes, nextAttributes) {
2712
const attributeNames = Object.keys({ ...currentAttributes, ...nextAttributes });
2813
for (const name of attributeNames) {
@@ -83,6 +68,35 @@ function updateAttributes(selector, currentAttributes, nextAttributes) {
8368
}
8469
}
8570

71+
function updateHeadChild(current, next) {
72+
if (isUndefined(current) && !isUndefined(next)) {
73+
const nextSelector = render(next);
74+
head.append(nextSelector)
75+
return
76+
}
77+
if (!isUndefined(current) && isUndefined(next)) {
78+
current.element.remove()
79+
return
80+
}
81+
next.element = current.element
82+
if (isFalse(current) && isFalse(next)) {
83+
return
84+
}
85+
if (current.type !== next.type) {
86+
const nextSelector = render(next);
87+
current.element.replaceWith(nextSelector)
88+
return
89+
}
90+
updateAttributes(current.element, current.attributes, next.attributes)
91+
}
92+
93+
function updateHeadChildren(currentChildren, nextChildren) {
94+
const limit = Math.max(currentChildren.length, nextChildren.length);
95+
for (let i = 0; i < limit; i++) {
96+
updateHeadChild(currentChildren[i], nextChildren[i])
97+
}
98+
}
99+
86100
function _rerender(current, next) {
87101

88102
const selector = current.element
@@ -96,57 +110,6 @@ function _rerender(current, next) {
96110
return;
97111
}
98112

99-
if ((current.type === 'head' ^ next.type === 'head') && !isFalse(current) && !isFalse(next)) {
100-
const nextSelector = render(next);
101-
current.element.replaceWith(nextSelector);
102-
}
103-
104-
if (current.type !== 'head' && next.type === 'head') {
105-
const limit = next.children.length;
106-
for (let i = limit - 1; i > -1; i--) {
107-
const nextSelector = render(next.children[i]);
108-
head.appendChild(nextSelector)
109-
}
110-
return
111-
} else if (current.type === 'head' && next.type === 'head') {
112-
const limit = Math.max(current.children.length, next.children.length);
113-
for (let i = 0; i < limit; i++) {
114-
if (isUndefined(current.children[i]) && !isFalse(next.children[i])) {
115-
const nextSelector = render(next.children[i]);
116-
head.appendChild(nextSelector)
117-
} else if (isUndefined(next.children[i]) && !isFalse(current.children[i])) {
118-
removeHeadElement(current.children[i])
119-
} else if (!isFalse(current.children[i]) && !isFalse(next.children[i])) {
120-
if (current.children[i].type === next.children[i].type) {
121-
const selector = getHeadElement(current.children[i])
122-
if (selector) {
123-
next.children[i].element = selector
124-
updateAttributes(selector, current.children[i].attributes, next.children[i].attributes)
125-
} else {
126-
const nextSelector = render(next.children[i]);
127-
head.appendChild(nextSelector)
128-
}
129-
} else {
130-
removeHeadElement(current.children[i])
131-
const nextSelector = render(next.children[i]);
132-
head.appendChild(nextSelector)
133-
}
134-
} else if (isFalse(current.children[i]) && !isFalse(next.children[i])) {
135-
const nextSelector = render(next.children[i]);
136-
head.appendChild(nextSelector)
137-
} else if (current.children[i].type) {
138-
removeHeadElement(current.children[i])
139-
}
140-
}
141-
return
142-
} else if (current.type === 'head' && next.type !== 'head') {
143-
const limit = current.children.length;
144-
for (let i = limit - 1; i > -1; i--) {
145-
removeHeadElement(current.children[i])
146-
}
147-
return
148-
}
149-
150113
if (current.type !== next.type) {
151114
const nextSelector = render(next);
152115
selector.replaceWith(nextSelector);
@@ -186,6 +149,7 @@ function _rerender(current, next) {
186149
}
187150
}
188151

152+
// todo: optimize
189153
if (next.type === 'textarea') {
190154
selector.value = next.children[0].text;
191155
}
@@ -201,4 +165,11 @@ function _rerender(current, next) {
201165
export default function rerender() {
202166
_rerender(client.virtualDom, client.nextVirtualDom)
203167
updateAttributes(body, client.currentBody, client.nextBody)
168+
updateHeadChildren(client.currentHead, client.nextHead)
169+
client.virtualDom = client.nextVirtualDom
170+
client.nextVirtualDom = null
171+
client.currentBody = client.nextBody
172+
client.nextBody = {}
173+
client.currentHead = client.nextHead
174+
client.nextHead = []
204175
}
Collapse file

‎server/prerender.js‎

Copy file name to clipboardExpand all lines: server/prerender.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function prerender(request, response) {
3232
scope.context = context;
3333
scope.generateContext = generateContext(context);
3434
scope.nextBody = {}
35+
scope.nextHead = []
3536
scope.plugins = loadPlugins(scope);
3637

3738
try {
@@ -46,6 +47,7 @@ export async function prerender(request, response) {
4647
} finally {
4748
if (context.page.status !== 200) {
4849
scope.nextBody = {}
50+
scope.nextHead = []
4951
for (const key in context.router._routes) {
5052
delete context.router._routes[key];
5153
}
Collapse file

‎server/render.js‎

Copy file name to clipboardExpand all lines: server/render.js
+27-18Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,55 @@ function isSelfClosing(type) {
2121
return false;
2222
}
2323

24-
export default function render(node, scope, next) {
25-
24+
function renderBody(node, scope, next) {
2625
if (isFalse(node)) {
2726
return "<!---->";
2827
}
29-
3028
if (node.type === 'text') {
3129
const text = node.text === '' ? ' ' : sanitizeHtml(node.text.toString())
3230
return next && next.type === 'text' ? text + "<!--#-->" : text
3331
}
34-
3532
let element = `<${node.type}`;
36-
3733
element += renderAttributes(node.attributes)
38-
3934
if (isSelfClosing(node.type)) {
4035
element += '/>';
4136
} else {
4237
element += '>';
4338
if (node.attributes.html) {
4439
const source = node.attributes.html;
45-
if (node.type === 'head') {
46-
scope.head += source;
47-
} else {
48-
element += source;
49-
}
40+
element += source;
5041
} else if (node.type === 'textarea') {
5142
element += node.children[0].text;
5243
} else {
5344
for (let i = 0; i < node.children.length; i++) {
54-
const source = render(node.children[i], scope, node.children[i + 1]);
55-
if (node.type === 'head') {
56-
scope.head += source;
57-
} else {
58-
element += source;
59-
}
45+
element += renderBody(node.children[i], scope, node.children[i + 1]);
6046
}
6147
}
6248
element += `</${node.type}>`;
6349
}
50+
return element;
51+
}
6452

65-
return node.type === 'head' ? '<!---->' : element;
53+
function renderHead(scope) {
54+
const limit = scope.nextHead.length
55+
for (let i = 0; i < limit; i++) {
56+
const node = scope.nextHead[i]
57+
if (isFalse(node)) {
58+
continue;
59+
}
60+
scope.head += `<${node.type}`;
61+
scope.head += renderAttributes(node.attributes)
62+
if (isSelfClosing(node.type)) {
63+
scope.head += '/>';
64+
} else {
65+
scope.head += '>';
66+
scope.head += node.attributes.html
67+
scope.head += `</${node.type}>`
68+
}
69+
}
70+
}
71+
72+
export default function render(node, scope, next) {
73+
renderHead(scope)
74+
return renderBody(node, scope, next)
6675
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.