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
94 lines (82 loc) · 2.89 KB
/
server.bundle.ts
File metadata and controls
94 lines (82 loc) · 2.89 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// These are important and needed before anything else
import { readFileSync } from 'fs';
import { join } from 'path';
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { JssRouteBuilderService } from './src/app/routing/jss-route-builder.service';
import { environment } from './src/environments/environment';
import { AppServerModule, renderModule } from './src/main.server';
export * from './src/main.server';
const http = require('http');
const https = require('https');
// Our index.html we'll use as our template
const template = readFileSync(join(__dirname, 'browser', 'index.html')).toString();
// Setup Http/Https agents for keep-alive. Used in headless-proxy
const setUpDefaultAgents = (httpAgent, httpsAgent) => {
http.globalAgent = httpAgent;
https.globalAgent = httpsAgent;
};
// this is the function expected by the JSS View Engine for "integrated mode"
function renderView(callback, path, data, viewBag) {
try {
/*
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: {}
}
},
language: '',
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;
state.language = parsedViewBag.language || jssRoute.language;
const transferState = { ...state };
delete transferState.viewBag;
renderModule(AppServerModule, {
document: template,
url: path,
extraProviders: [
// custom injection with the initial state that SSR should utilize
{ provide: 'JSS_SERVER_LAYOUT_DATA', useValue: transferState },
{ provide: 'JSS_SERVER_VIEWBAG', useValue: state.viewBag },
]
})
.then(html => callback(null, { html }))
.catch(err => callback(err, null))
} catch (err) {
// need to ensure the callback is always invoked no matter what
// or else SSR will hang
callback(err, 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,
setUpDefaultAgents,
apiKey: environment.sitecoreApiKey,
appName: environment.jssAppName
};