-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfirst.tsx
81 lines (70 loc) · 2.49 KB
/
first.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
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
/**
* The 'entry point' (in webpack terminology)
* of the First SPA.
*
* SSR has been enabled for this entry point.
* To disable SSR:
* - comment out import { renderToString } ...
* - replace ReactDOM.hydrate with ReactDOM.render (see comments below)
* - comment out the SSR block at the bottom
* - set the 'ssr' flag to false for this SPA in spa.config.js
*
* Note than only one SPA (and therefore one entry point) can have SSR enabled.
*/
import * as React from "react";
import * as ReactDOM from "react-dom";
import { Router, Route, Switch } from "react-router-dom";
import { ComponentA } from "../components/ComponentA";
import { Lighthouse } from "../components/Lighthouse";
import { Overview } from "../components/Overview";
import { NotFound } from "../components/NotFound";
import { NameLookup } from "../components/NameLookup";
import { ErrorBoundary } from "../components/ErrorBoundary";
import { renderToString } from "react-dom/server"; // used for SSR
import * as SPAs from "../../config/spa.config";
import { isServer, getHistory } from "../utils/postprocess/misc";
import "../css/app.css";
import "../css/app.less";
// If the first SPA is called 'first' then the regex
// will match '/first' and '/first.html';
const regexPath = new RegExp(`^/${SPAs.getRedirectName()}(\.html)?$`);
const First: React.FC = _props => {
const catchAll = () => {
const path = window.location.pathname.toLowerCase();
if (regexPath.test(path)) {
return <Overview/>
}
return <NotFound/>;
}
return (
<>
<Router history={getHistory()}>
<ErrorBoundary>
<div className="welcome">
<h2>Welcome to {SPAs.appTitle}</h2>
</div>
<Switch>
<Route exact path="/" component={Overview} />
<Route path="/a" component={ComponentA} />
<Route path="/lighthouse" component={Lighthouse} />
<Route path="/namelookup" component={NameLookup} />
<Route render={catchAll} />
</Switch>
</ErrorBoundary>
</Router>
</>
)
};
if (!isServer()) {
// ReactDOM.render( // .render(...) is used without SSR
ReactDOM.hydrate( // .hydrate(...) is used with SSR
<First />,
document.getElementById("app-root")
);
}
/****************** SSR block start ******************/
const asString = (): string => {
return renderToString(<First />)
}
export default asString;
/****************** SSR block end ******************/