Comments (10)
@RnbWd there is a branch for server side rendering ( https://github.com/Raynos/mercury/tree/server-rendering ).
It's still a work in progress.
There are three things that need to be implemented
- remove hooks & widgets server-side since we can't turn them into HTML. Alternatively implement a
toHtmlString(vtree)
function. - test / use / implement https://github.com/marcelklehr/vdom-virtualize to turn a server side rendered HTML tree into a vtree.
- deal with the transient state problem.
Server side rendering
The way you do server side rendering is as follows
- Write a function
render
that takes a data object (plain JS object) and returns a vtree (probably by usingh()
- convert it to a HTML string properly (replace widgets with
<span>
) - server side render the HTML. Ensure you embed the data object used to
render()
on the server in the pay load - in the browser get the data object and call
render()
on the client to get aclientVtree
(one with widgets & hooks) - virtualize the server side rendered HTML into a
serverVtree
then justpatch(container, diff(serverVtree, clientVtree))
- now you are done.
We probably need to update main-loop
to be sensitive to these new concerns. Especially virtualize
The transient state problem
If you have something like h('input', { value: state.value, 'ev-change': state.events.change })
in your render()
and you server side render it, you run into a problem where a user may enter text in an input
before the change event handler is added (i.e. interacts with the page whilst the page is loading)
The main solution is calling render(state)
on the client side twice, the first time to build a vtree and parse out all event handlers, we then trigger the event handlers with the current state of the DOM elements if the state is non-empty, i.e. basically replaying DOM events that fired before javascript was loaded
The second time we render()
the state
should be different, i.e. updated to be correct because events have been replayed, we can then continue as normal.
If we don't do this then client side rendering will overwrite your current input element with an empty string.
from mercury.
@alexmingoia started working on https://github.com/alexmingoia/virtual-dom-stringify/ to solve the string problem
from mercury.
Excellent, I'll look into it and see if I can help with anything :)
I tried going through the React source last week to figure out how they do it.. no luck. Separating event handlers before converting to string would provide a basic implementation. The transient state problem is hard to solve, but if the js renders fast enough it might not be the worst. Thanks for helping out and working on this project, I think it has lots of potential and I'm using some of the modules for other projects already.
from mercury.
@RnbWd no worries, it's super cool to see you using observ-*
and react together in the wild :)
from mercury.
@RnbWd I merged a server side rendering example.
it does the virtualize & to html string parts.
it doesnt deal with the transient state problem, but that's a seperate "bonus" problem that can be solved seperately.
Any further work on this problem should be fixing bugs in vdom-virtualize and virtual-dom-stringify
from mercury.
@Raynos , Thanks for updating the example. IMO the transient state problem isn't a big deal (from the user perspective). How much can you do between the initial HTML render and when scripts load? I'd like to find time to help test / contribute to this project, I really like where its going :)
from mercury.
Just tried it out, looks really good!!
from mercury.
It's still a work in progress.
is it ready now? I've seen a render function here, is it the toHtmlString(vtree) you are talking about?
from mercury.
@cauburtin server-side rendering kind of works.
It's definitely possible, it needs a little bit of boilerplate in your app, you can see the example
from mercury.
Thanks, I'm going to deal with isomorphic code (necessary in that case -SEO)). I guess the difficulty is for events inside that tree? A tree without event handlers inside is maybe not hard to stringify
from mercury.
Related Issues (20)
- live component reloading HOT 6
- Mercury not exporting virtual-hyperscript SVG HOT 2
- Immutable props
- question: how to detect if a widget has been unmounted HOT 2
- Communication between child components
- Why static render methods? HOT 3
- Using vdom-to-html doesn't work (returns empty string) HOT 6
- Redux + mercury example? HOT 10
- lock down wiki?
- syntax of h HOT 2
- npm install fails HOT 1
- 14.1.0 break all VNode input rendering HOT 8
- newb question HOT 4
- Docs/Introduction: Simple Cycle example missing
- Synchronize tagged version and comment in mercury.js
- Rendering subitems of different types HOT 4
- What is the right way to pass data to click event handlers? HOT 1
- Get the previous version of state var HOT 3
- Splitting components out to their own render loop? HOT 2
- How to attach state to renderer? HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mercury.