-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.bundle.ts
82 lines (71 loc) · 2.56 KB
/
server.bundle.ts
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
74
75
76
77
78
79
80
81
82
// 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';
import { JssRouteBuilderService } from './src/app/jss-route-builder.service';
// 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');
// this is the function expected by the JSS View Engine for "integrated mode"
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: {}
}
},
serverRoute: '',
viewBag: parsedViewBag,
};
if (parsedData) {
state.sitecore = parsedData.sitecore;
}
// parse the URL that's being handled by Sitecore so we can pass in the initial state to the app
const routeParser = new JssRouteBuilderService();
const jssRoute = routeParser.parseRouteUrl(path.split('/').filter(segment => segment));
state.serverRoute = jssRoute.serverRoute;
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),
// custom injection with the initial state that SSR should utilize
{ provide: 'JSS_SERVER_TO_SSR', useValue: state }
]
}).then(html => {
callback(null, {
html: html,
status: 200,
redirect: null,
});
});
}
function parseRouteUrl(url) {
const routeParser = new JssRouteBuilderService();
const jssRoute = routeParser.parseRouteUrl(url.split('/').filter(segment => segment));
return {
lang: jssRoute.language,
sitecoreRoute: jssRoute.serverRoute
};
}
module.exports = {
renderView,
parseRouteUrl
};