Skip to content

Instantly share code, notes, and snippets.

@thehydroimpulse
Last active December 12, 2015 09:09
Show Gist options
  • Save thehydroimpulse/4749283 to your computer and use it in GitHub Desktop.
Save thehydroimpulse/4749283 to your computer and use it in GitHub Desktop.
Splitting ember.js compilation on both the server (Node.js) and client-side. This would solve search engine crawling, speed, and some others. I also gota simple server side implementation. Had to port ember-view to node.js. things are still somewhat unstable. Some issues on the server : * Lack of Router * This means Controllers and Views are not…
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Fast Boot</title>
</head>
<body>
<!-- Main application template -->
<script type="text/x-handlebars" data-template-name="application">
Application Template - {{name}}
{{outlet}}
</script>
<!-- Index View -->
<script type="text/x-handlebars" data-template-name="index">
Some random stuff in here....
</script>
<div id="wrapper">
<!-- Anything coming from the server will have a .server class attribute to each
view. -->
<div id="ember162" class="ember-view server">
Application Template -
<script id="metamorph-0-start" type="text/x-placeholder"></script>
<div id="ember168" class="ember-view">
Some random stuff in here....
</div>
<script id="metamorph-0-end" type="text/x-placeholder"></script>
</div>
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js"></script>
<script src="https://raw.github.com/wycats/handlebars.js/1.0.rc.2/dist/handlebars.js"></script>
<script src="https://raw.github.com/emberjs/ember.js/release-builds/ember-1.0.0-pre.4.js"></script>
</body>
</html>
var App = Ember.Application.create({
rootElement: '#wrapper',
LOG_TRANSITIONS: true
});
App.ApplicationController = Ember.Controller.extend({
name: 'John'
});
App.ApplicationView = Ember.View.extend({
template: Ember.Handlebars.compile('application - {{outlet}}'),
// Basically, the only magic that's needed:
willInsertElement: function() {
var old = $("body").find('#wrapper .ember-view.server')[0];
old.remove();
this._super();
}
});
App.IndexController = Ember.Controller.extend({
name: 'Joen'
});
App.IndexView = Ember.View.extend({
templateName: 'index'
});
App.Router = Ember.Router.extend();
App.Router.reopen({
location: 'history'
});
App.IndexRoute = Ember.Route.extend({
setupController: function(controller) {
// Set the IndexController's `title`
},
renderTemplate: function() {
this.render('index');
}
});
App.Router.map(function() {
this.route("index", {
path: "/"
});
});
window.App = App;
var fs, path;
path = require('path');
// Store the paths:
var _modules = {};
// This isn't the real implementation, we'll need to come back and replace this:
function define(module, deps, callback) {
_modules[module] = {
deps: deps,
cb: callback
};
}
// Same here:
function requireModule(module) {
return _modules[module].cb();
}
global.define = define;
global.requireModule = requireModule;
// The following is really rough. We'll need to clean it up:
//
// Require JSDOM:
var jsdom = require("jsdom");
// Create a basic DOM Document:
var document = jsdom.jsdom("<html><head></head><body><div id='#wrapper'></div></body></html>");
// Create a window object:
var window = jsdom.createWindow();
// Expose the document under the window object (emulating the client-side):
window.document = document;
// Globally expose the window:
global.window = window;
// Require JQuery:
require('./shared/jquery');
// Expose jQuery under global and window:
global.$ = window.$;
// Expose handlebars:
global.Handlebars = require('handlebars');
// Require all the ember stuff:
// These are not direct from npm, they are modified:
require('ember-metal-node');
require('ember-runtime-node');
require('ember-states-node');
require('ember-routing-node');
require('ember-application-node');
require('ember-views-node');
// Ember needs these helpers for the view/template system:
require('./shared/handlebar-helpers');
// Create a new application:
var App = Ember.Application.create({
init: function() {},
// Define the root element:
rootElement: '#wrapper'
});
// Create an application controller:
App.ApplicationController = Ember.Controller.extend({
name: '12222223'
});
// We need to expose a `server-view` class for each view
// so we can track them in the client context:
var View = Ember.View.extend({
classNames: ['server-view']
});
App.ApplicationView = View.extend({
template: Ember.Handlebars.compile('Hello World!!! {{nadme}}'),
didInsertElement: function() {
this._super();
// We can grab the current updated DOM here...
}
});
// Create and append the new view:
var view = App.ApplicationView.create().append();
// Initialize the application:
App.initialize();
// We need to use scheduleOnce and an 'afterRender' event to gather the updated DOM nodes.
// This is where we'd return the result to the client:
Ember.run.scheduleOnce('afterRender', this, function() {
console.log($("html").html());
});
@thehydroimpulse
Copy link
Author

On the server, it roughly takes 6-10ms to render the view onto the DOM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment