-
Notifications
You must be signed in to change notification settings - Fork 11
/
InitBrowser.tsx
45 lines (40 loc) · 2 KB
/
InitBrowser.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
import ReactDOM from "react-dom";
import React from "react";
import CmsSite from './Components/CmsSite';
import AppConfig from './AppConfig';
import EpiContext from './Spa';
import ComponentPreLoader, { IComponentPreloadList } from "./Loaders/ComponentPreLoader";
import IServiceContainer from './Core/IServiceContainer';
import DefaultServiceContainer from './Core/DefaultServiceContainer';
import ServerContextType from './ServerSideRendering/ServerContext';
declare let __INITIAL_DATA__ : ServerContextType;
export function InitBrowser(config: AppConfig, containerId?: string, serviceContainer?: IServiceContainer) : void
{
try {
if (__INITIAL_DATA__?.status === 'loading') {
__INITIAL_DATA__.onReady = () => _doInitBrowser(config, containerId, serviceContainer);
return;
}
} catch (e) {
// Ignore on purpose
}
return _doInitBrowser(config, containerId, serviceContainer);
}
function _doInitBrowser(config: AppConfig, containerId?: string, serviceContainer?: IServiceContainer) : void
{
EpiContext.init(config, serviceContainer || new DefaultServiceContainer());
const container = document.getElementById(containerId ? containerId : "epi-page-container");
if (container && container.childElementCount > 0) {
const components : IComponentPreloadList = EpiContext.config().preLoadComponents || [];
if (EpiContext.isDebugActive()) console.info('Hydrating existing render, Stage 1. Preloading components ...', components);
const loader = EpiContext.componentLoader();
ComponentPreLoader.load(components, loader).finally(() => {
if (EpiContext.isDebugActive()) console.info('Hydrating existing render, Stage 2. Hydration ...');
ReactDOM.hydrate(<CmsSite context={ EpiContext } />, container);
});
} else {
if (EpiContext.isDebugActive()) console.info('Building new application');
ReactDOM.render(<CmsSite context={ EpiContext } />, container);
}
}
export default InitBrowser;