Skip to content
This repository was archived by the owner on Jun 16, 2020. It is now read-only.

Commit 66f8da1

Browse files
committed
Allow true to be passed into hg.app
Client-side reattachment was broken for server-rendered apps because of 8c26d68. Another fix was submitted in #207 but I think we should preserve the simplicity by using `hg.app` still. This makes it so that you can pass `true` as the value for the element which means "this was prerendered". Test included. Closes #79
1 parent 7b16d8a commit 66f8da1

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

examples/server-rendering/browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var app = App(JSONGlobals('state'));
2727
var targetElem = document.body.firstChild;
2828
var prevTree = virtualize(targetElem);
2929

30-
hg.app(null, app, App.render, {
30+
hg.app(true, app, App.render, {
3131
initialTree: prevTree,
3232
target: targetElem
3333
});

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ function app(elem, observ, render, opts) {
120120
patch: mercury.patch
121121
}, opts));
122122

123-
elem.appendChild(loop.target);
123+
// This allows `true` to be passed meaning that the app was
124+
// prerendered
125+
if (elem !== true) {
126+
elem.appendChild(loop.target);
127+
}
124128

125129
return observ(loop.update);
126130
}

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ require('./count.js');
3333
require('./shared-state.js');
3434
require('./bmi-counter.js');
3535
require('./time-travel.js');
36+
require('./ssr.js');

test/ssr.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
var mercury = require('../index.js');
4+
var h = mercury.h;
5+
var document = require('global/document');
6+
var test = require('tape');
7+
var virtualize = require('vdom-virtualize');
8+
var raf = require('raf');
9+
10+
test('Can reattach in the client', function t(assert) {
11+
var div = document.createElement('div');
12+
div.appendChild(document.createTextNode('Hello world'));
13+
document.body.appendChild(div);
14+
15+
function render(name) {
16+
return h('div', 'Hello ' + name);
17+
}
18+
19+
var state = mercury.value('venus');
20+
21+
mercury.app(true, state, render, {
22+
target: div,
23+
initialTree: virtualize(div)
24+
});
25+
state.set(state());
26+
27+
raf(function afterRender() {
28+
var tn = document.body.childNodes[0].childNodes[0];
29+
30+
assert.equal(tn.data, 'Hello venus', 'Element was updated');
31+
32+
document.body.removeChild(document.body.childNodes[0]);
33+
assert.end();
34+
});
35+
36+
});

0 commit comments

Comments
 (0)