forked from Sitecore/jss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.bundle.ts
More file actions
65 lines (55 loc) · 1.79 KB
/
server.bundle.ts
File metadata and controls
65 lines (55 loc) · 1.79 KB
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
// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';
import { join } from 'path';
import { readFileSync } from 'fs';
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();
// Our index.html we'll use as our template
const template = readFileSync(join(__dirname, 'browser', 'index.html')).toString();
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
function renderView (callback, path, data, viewBag) {
/*
Data from server is double-encoded since MS JSS does not allow control
over JSON serialization format.
*/
const parsedData = data instanceof Object ? data : JSON.parse(data);
const parsedViewBag = viewBag instanceof Object ? viewBag : JSON.parse(viewBag);
const state = {
sitecore: {
context: {
pageEditing: false
},
route: {
placeholders: {}
}
},
viewBag: {},
};
state.viewBag = parsedViewBag;
if (parsedData) {
state.sitecore = parsedData.sitecore;
}
renderModuleFactory(AppServerModuleNgFactory, {
document: template,
url: path,
// DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
extraProviders: [
provideModuleMap(LAZY_MODULE_MAP),
{ provide: 'JSS_SERVER_TO_SSR', useValue: state }
]
}).then(html => {
callback(null, {
html: html,
status: 200,
redirect: null,
});
});
}
module.exports = {
renderView
};