-
Notifications
You must be signed in to change notification settings - Fork 16
/
renderer_worker.js
73 lines (58 loc) · 1.59 KB
/
renderer_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var path = require('path');
var co = require('co');
var consolidate = require('consolidate');
var lampion = require('lampion');
var outputPath = (process.env.NODE_ENV == 'production') ? path.join(__dirname, 'dists') : __dirname;
co(function *() {
var lApp = lampion({
worker: true,
appPath: __dirname,
localePath: path.join(outputPath, 'locales'),
});
yield lApp.configure();
var settings = lApp.settings;
// Library
var Utils = lApp.getLibrary('Utils');
// Initializing react app
var ReactApp = require(path.join(outputPath, 'build', 'server.js'));
yield ReactApp.init({
externalUrl: Utils.getExternalUrl()
});
var pageHandler = co.wrap(function *(task) {
try {
// Rendering page with current state and cookie to client-side
var page = yield ReactApp.render(task.data.path, task.data.curState, {
cookie: task.data.cookie
});
} catch(e) {
return console.log(e.stack);
}
var result = [ task.id ];
// Redirect
if (page.redirect) {
result.push('D');
result.push(page.redirect);
process.send(result.join(''));
return;
}
// Using service name by default
if (!page.state.Window.title) {
page.state.Window.title = settings.general.service.name;
}
// Rendering page
consolidate.pug('views/index.pug', {
cache: true,
title: page.state.Window.title,
content: page.content,
window: page.state.Window,
state: JSON.stringify(page.state)
}, function(err, html) {
result.push('C');
result.push(html);
process.send(result.join(''));
});
});
process.on('message', pageHandler);
}).catch(function(e) {
console.log(e.stack);
});