This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
/
ssr.tsx
68 lines (61 loc) · 1.92 KB
/
ssr.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
// Server-side HTML render
// Component to render the full HTML response in React
// ----------------------------------------------------------------------------
// IMPORTS
/* NPM */
import React from "react";
import { HelmetData } from "react-helmet";
// ----------------------------------------------------------------------------
// Types
export interface IHtmlProps {
css?: string;
helmet: HelmetData;
html: string;
scripts: string[];
styles?: Array<React.ReactElement<{}>>;
window?: {
[key: string]: object;
};
}
export default class Html extends React.PureComponent<IHtmlProps> {
public render() {
const { css, helmet, html, scripts, styles, window = {} } = this.props;
return (
<html
lang="en"
prefix="og: http://ogp.me/ns#"
{...helmet.htmlAttributes.toString()}
>
<head>
{helmet.title.toComponent()}
<meta charSet="utf-8" />
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta httpEquiv="Content-Language" content="en" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{helmet.meta.toComponent()}
{helmet.style.toComponent()}
{helmet.link.toComponent()}
{css && <link rel="stylesheet" href={css} />}
{styles}
{helmet.script.toComponent()}
{helmet.noscript.toComponent()}
</head>
<body {...helmet.bodyAttributes.toComponent()}>
<div id="root" dangerouslySetInnerHTML={{ __html: html }} />
<script
dangerouslySetInnerHTML={{
__html: Object.keys(window).reduce(
(out, key) =>
(out += `window.${key}=${JSON.stringify(window[key])};`),
""
)
}}
/>
</body>
{scripts.map(script => (
<script key={script} src={script} />
))}
</html>
);
}
}