4 ms·
It is not necesssary to execute JavaScript on the server-side, or to use React.js at all (although it is a pretty neat tool.) For instance in Backbone, you can
by Aqueous 13y ago
It is not necesssary to execute JavaScript on the server-side, or to use React.js at all (although it is a pretty neat tool.) For instance in Backbone, you can call setElement() on any view to replace its on-screen element with one that you've pre-rendered. If you're clever about templates this means that you can have views that both know how to build themselves up from zero, and can also attach to existing views on screen so you can save yourself that rendering time when you visit the page.
This could result in code duplication, but the end result is the same: a complete state of the application can be rendered server-side and then can evolve independently on the client side.
- thefreeman 13y agoHow are you going to make Backbone calls without Javascript? I believe the author's goal was to allow his single page web app to be properly indexed by search engines (which don't execute javascript) without having to change any of his code.
- valgaze 13y agoThat's not entirely accurate, crawlers/indexers don't execute JS code reliably but depending on your routing setup it can sorta work if you use a special meta tag, <meta name="fragment" content="!"> https://developers.google.com/webmasters/ajax-crawling/docs/specification https://developers.google.com/webmasters/ajax-crawling/docs/... http://www.slashdot.org/story/169505 http://www.slashdot.org/story/169505
- salman89 13y agoWhat he's saying is that the views you want to be SEO'd can be generated server side and bootstrapped into Backbone (hook backbone views into an element that is rendered server side). I currently do this too - I'll add that it is helpful to use a templating language that will work both server side and client side so you only write your templates once. What does become a pain is doing routers/controllers both client side and server side - end up writing some code more than once and in different languages. Harder to maintain, especially when changes are made.
- thefreeman 13y agoRight, and if you want to generate them server side, you either need to have separate templates for server side and client side, or you can execute the javascript on the server (which is what the OP decided to do). Noone is saying there aren't other solutions to the problem, but I think the OP's solution met his goals pretty well and I thought it was a neat concept.
- chii 13y ago> you either need to have separate templates for server side and client side there exists templating engines that run on both server and client - for example, google closure templates.
- salman89 13y agoYou can use cross-language templating options - personally I use mustache. No disagreements on multiple solutions - definitely a cool approach.
- ac2u 13y agoSo, in the past I achieved what the parent was talking about by using a templating engine that could be rendered natively by my server side language and by js. In this case it was moustache. With a little config glue my templates could be referenced from both the browser js (in which I called them in by backbone render method) and in rails (when they were rendered as partial calls in my views)
- laurencerowe 13y agoThe nice thing about using React server-side rendering is that you only have to to implement your rendering logic once and run it in both client and server. While you need to take a little care (client-only code goes in componentDidMount, only require client only libs in client-only scope) it's fairly painless. I have my Python app return json and pipe it through a node subprocess for the first page load.