Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hydrate server-rendered apps #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
# Ignores that should be in the global gitignore
/coverage
/doc

# Ignore npm stuff
/node_modules
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => [:spec, :spec_opal]

namespace :vdom do
desc 'Compile the virtual-dom library from npm'
task :compile do
sh 'npm install'
sh 'npm run compile'
end
end
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var diff = require('virtual-dom/diff')
var patch = require('virtual-dom/patch')
var h = require('virtual-dom/h')
var create = require('virtual-dom/create-element')
var VNode = require('virtual-dom/vnode/vnode')
var VText = require('virtual-dom/vnode/vtext')
var svg = require('virtual-dom/virtual-hyperscript/svg')
var virtualize = require('vdom-virtualize')

module.exports = {
diff,
patch,
h,
create,
VNode,
VText,
svg,
virtualize,
}
26 changes: 18 additions & 8 deletions opal/clearwater/virtual_dom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,26 @@ def initialize(root=Bowser.document.create_element('div'))
end

def render node
if rendered?
diff = VirtualDOM.diff @node, node
VirtualDOM.patch @tree, diff
@node = node
else
@node = node
@tree = create_element(node)
@root.inner_dom = @tree
if !rendered?
@rendered = true

# If we rendered the app on the server, we hydrate the rendered app
# rather than replacing it. This provides a faster and more memory-
# efficient first vdom render.
if `#@root.native.children.length > 0`
@tree = `#@root.native.children[0]`
@node = `virtualDom.virtualize(#@tree)`
else # Not server-rendered, generate from scratch.
@node = node
@tree = VirtualDOM.create_element(node)
@root.inner_dom = @tree
return
end
end

diff = VirtualDOM.diff @node, node
VirtualDOM.patch @tree, diff
@node = node
end

def create_element node
Expand Down
Loading