-
Notifications
You must be signed in to change notification settings - Fork 11
/
InitServer.tsx
48 lines (41 loc) · 1.66 KB
/
InitServer.tsx
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
// Set SSR
import getGlobal from './AppGlobal';
// Global Libraries && Poly-fills
import ReactDOMServer from 'react-dom/server';
import { Helmet } from 'react-helmet';
import React from 'react';
import { StaticRouterContext } from 'react-router';
// Episerver Libraries
import IServiceContainer from './Core/IServiceContainer';
import DefaultServiceContainer from './Core/DefaultServiceContainer';
import EpiSpaContext from './Spa';
import CmsSite from './Components/CmsSite';
import AppConfig from './AppConfig';
// Episerver SPA/PWA Server Side Rendering libs
import SSRResponse from './ServerSideRendering/Response';
export default function RenderServerSide(config: AppConfig, serviceContainer?: IServiceContainer): SSRResponse
{
// Update context
const ctx = getGlobal();
ctx.epi = ctx.epi || {};
ctx.epi.isServerSideRendering = true;
// Initialize Episerver Context, for Server Side Rendering
serviceContainer = serviceContainer || new DefaultServiceContainer();
config.enableSpinner = false;
config.noAjax = true;
config.enableDebug = true;
EpiSpaContext.init(config, serviceContainer, true);
const staticContext : StaticRouterContext = {};
const body = ReactDOMServer.renderToString(<CmsSite context={ EpiSpaContext } staticContext={ staticContext } />);
const meta = Helmet.renderStatic();
return {
Body: body,
HtmlAttributes: meta.htmlAttributes.toString(),
Title: meta.title.toString(),
Meta: meta.meta.toString(),
Link: meta.link.toString(),
Script: meta.script.toString(),
Style: meta.style.toString(),
BodyAttributes: meta.bodyAttributes.toString()
};
}