A history of web frameworks
Why are we moving to the server?
- Benefits for users
- Benefits for developers
How you can use this with your YUI code today
- Or even any other JS framework!
A history of web frameworks
Why are we moving to the server?
How you can use this with your YUI code today
Highly abstracted server MVC frameworks gain popularity
Rendering is done on the server, and outputs HTML
Standardizing data access over HTTP
Example: YQL
Y.YQL('SELECT * FROM search.suggest WHERE query="pie";', function (data) { // Work with your data here });
NSDictionary *results = [yql query:@"SELECT * FROM search.suggest WHERE query='pie'"];
http://query.yahooapis.com/v1/public/yql?q='SELECT * FROM search.suggest WHERE query="pie"'
Client-side MV* frameworks gain popularity
Server outputs JSON, and the client renders the data into HTML
Performance
SEO Concerns
...We've been working to take back control of our front-end performance by moving the rendering to the server. This has allowed us to drop our initial page load times to 1/5th of what they were...
Render the first page load completely on the server
Incrementally update sections of the page through client-side rendering
Lots of code duplication if using two different languages
Our solution:
JavaScript on the server:
One language, client and server
Instead of coding in one language, we're actually coding in two. One is the subset [of] JavaScript that can be run in all browsers, and another is the set of JavaScript that can be run by Node.
HTTP Middleware | |||
---|---|---|---|
Personalization | |||
Route Handling | |||
Resource Fetching | |||
Formatting and Rendering | |||
UI Binding |
Express State
// `app.expose` for exposing data globally on every route app.expose({ api_key: '02348notreal2394879137872358bla' }, 'MY_APP.Flickr'); // `res.expose` for exposing data on a single route app.get('/photos', function (req, res) { res.expose(req.photos, 'MY_APP.users'); });
// Map routes to unique names with `express-map` app.map('/users/', 'user-list'); app.map('/users/:user', 'user-profile'); // Expose routes as serialized objects on the client app.expose(app.getRouteMap(), 'MY_APP.routes');
// `App.ROUTES` is available anywhere on the client console.log(MY_APP.routes); // Merge `App.ROUTES` with any client-only route data (not shown here) // and bootstrap it in your application. app.set('routes', MY_APP.routes);
var Weather = { /* Client and server code can both use the same 'fetch' method to get data */ fetch: function(location) { var query = 'SELECT * FROM weather.forecast WHERE location=' + location; return Y.YQLPromise(query).then(function (data) { /* Parse the data from the response here */ }); } };
app.get('/weather', function (req, res) { var weather = Weather.fetch().then(function (data) { res.expose(data, 'Weather.DATA'); res.render('weather', { weather: data }); }); });
// `Weather.DATA` is available anywhere on the client console.log(Weather.DATA); // Can be used to bootstrap a new model this.set('weather', new WeatherModel(Weather.DATA));
Compile string-based templates like Handlebars into YUI modules
Use the `Y.Template` API to use templates in one way regardless of engine
YUI.add('templates-foo', function (Y) { var engine = new Y.Template(Y.Handlebars), precompiled; // Generated from the Handlebars compiler. This is the evaluated result of // precompiling `foo.handlebars`. precompiled = /* precompiled template interpolated here here */ Y.Template.register('foo', engine.revive(precompiled)); }, '0.0.1', {requires: ['template-base', 'handlebars-base']});
Moving front-end to the server is better for users
Moving front-end to the server is better for developers
You can find me online in the following places: