Skip to content
This repository was archived by the owner on May 8, 2021. It is now read-only.

Commit e656579

Browse files
committed
Update for server-side rendering
1 parent 759c639 commit e656579

File tree

5 files changed

+84
-24
lines changed

5 files changed

+84
-24
lines changed

Gruntfile.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ module.exports = function(grunt) {
77
grunt.file.mkdir(buildDir);
88

99
grunt.initConfig({
10-
// create a static webserver
11-
connect: {
12-
server: {
13-
options: {
14-
hostname: hostname,
15-
base: buildDir,
16-
port: port
17-
}
18-
}
19-
},
2010
lumbar: {
2111
// performs an initial build so when tests
2212
// and initial open are run, code is built
@@ -34,6 +24,14 @@ module.exports = function(grunt) {
3424
output: buildDir
3525
}
3626
},
27+
'hapi-routes': {
28+
map: {
29+
options: {
30+
package: 'web',
31+
dest: buildDir + '/module-map.json'
32+
}
33+
}
34+
},
3735
// allows files to be opened when the
3836
// Thorax Inspector Chrome extension
3937
// is installed
@@ -50,7 +48,11 @@ module.exports = function(grunt) {
5048
}
5149
}
5250
});
53-
51+
52+
grunt.registerTask('hapi-server', function() {
53+
// Self running.
54+
require('./server');
55+
});
5456
grunt.registerTask('open-browser', function() {
5557
var open = require('open');
5658
open('http://' + hostname + ':' + port);
@@ -59,14 +61,16 @@ module.exports = function(grunt) {
5961
grunt.loadTasks('tasks');
6062
grunt.loadNpmTasks('thorax-inspector');
6163
grunt.loadNpmTasks('lumbar');
64+
grunt.loadNpmTasks('hula-hoop');
6265
grunt.loadNpmTasks('grunt-contrib-connect');
6366

6467
grunt.registerTask('default', [
6568
'ensure-installed',
6669
'thorax:inspector',
70+
'hapi-routes',
6771
'lumbar:init',
68-
'connect:server',
72+
'hapi-server',
6973
'open-browser',
7074
'lumbar:watch'
7175
]);
72-
};
76+
};

js/init.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,25 @@ Application.initBackboneLoader(Application, function(type, module) {
2525
// You have failed to load the module. Let the world know.
2626
});
2727

28-
$(function() {
28+
$(window).ready(function() {
29+
// Check to see if we have rendered content that we can try to restore
30+
var appEl = $('[data-view-name="application"]');
31+
if (appEl.length) {
32+
// Restore the application view explicitly
33+
Application.restore(appEl);
34+
} else {
35+
// We are starting with a blank page, render a new element
36+
$('body').append(Application.el);
37+
Application.render();
38+
}
39+
2940
// Application and other templates included by the base
3041
// Application may want to use the link and url helpers
3142
// which use hasPushstate, etc. so setup history, then
3243
// render, then dispatch
3344
Backbone.history.start({
34-
pushState: false,
35-
root: '/',
36-
silent: true
45+
pushState: true,
46+
root: '/'
3747
});
38-
// TODO: can remove after this is fixed:
39-
// https://github.com/walmartlabs/lumbar/issues/84
40-
Application.template = Thorax.templates.application;
41-
Application.appendTo('body');
42-
Backbone.history.loadUrl();
4348
});
4449

lumbar.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"thorax-loading",
1313
],
1414
"scripts": [
15-
{"src": "bower_components/bootstrap/js/bootstrap.js", "global": true},
15+
{"src": "bower_components/bootstrap/js/bootstrap.js", "global": true, "server": false},
1616
{"src": "lumbar-loader-backbone.js", "bower": "lumbar-loader"},
1717
"js/init.js",
1818
"js/model.js",
@@ -31,6 +31,9 @@
3131

3232
"templates": {
3333
"root": "templates/",
34+
"precompile": {
35+
"data": true
36+
},
3437
"auto-include": {
3538
"js/views/(.*)\\.(js|coffee)": [
3639
"templates/$1.handlebars",
@@ -39,10 +42,12 @@
3942
]
4043
},
4144
"knownHelpers": [
45+
// Place any helpers created in the app here to optimize their use
4246
],
4347
"js/init.js": [
4448
"templates/application.handlebars"
4549
]
4650
},
4751
"loadPrefix": "/r/",
52+
"server": true
4853
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"test": "node ./node_modules/grunt-cli/bin/grunt test"
1818
},
1919
"engines": {
20-
"node" : ">=0.8.0"
20+
"node": ">=0.8.0"
21+
},
22+
"dependencies": {
23+
"hula-hoop": "0.0.1",
24+
"hapi": "^5.0.0"
2125
}
2226
}

server/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var Hapi = require('hapi'),
2+
HulaHoop = require('hula-hoop');
3+
4+
var server = new Hapi.Server(8000);
5+
6+
var appName = 'throax-seed';
7+
8+
// Setup resource handling
9+
HulaHoop.api.resourceLoader.register(appName, [
10+
{name: 'main', version: '1.0.0', path: './build'}
11+
]);
12+
server.route([
13+
{
14+
path: '/r/{path*}',
15+
method: 'GET',
16+
handler: HulaHoop.endpoints.resources()
17+
}
18+
]);
19+
20+
// Setup the user endpoint routing
21+
var pageHandler = HulaHoop.endpoints.page(appName, {
22+
host: 'foo.com',
23+
resourceRoot: '/r/'
24+
});
25+
26+
server.route(
27+
HulaHoop.api.resourceLoader.routes().map(function(route) {
28+
return {
29+
path: route,
30+
method: 'GET',
31+
handler: pageHandler,
32+
config: {
33+
cache: {
34+
expiresIn: 5*60*1000,
35+
privacy: 'private'
36+
}
37+
}
38+
};
39+
})
40+
);
41+
42+
server.start();

0 commit comments

Comments
 (0)