Skip to content

Commit 2441ace

Browse files
committed
Create html.js
1 parent 9667eec commit 2441ace

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

html.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
'use strict'
2+
import PropTypes from 'prop-types'
3+
import React, { PureComponent } from 'react'
4+
import serialize from 'serialize-javascript'
5+
6+
// @twreporter
7+
import webfonts from '@twreporter/react-components/lib/text/utils/webfonts'
8+
import {
9+
colorGrayscale,
10+
colorBrand,
11+
} from '@twreporter/core/lib/constants/color'
12+
13+
// lodash
14+
import map from 'lodash/map'
15+
const _ = {
16+
map,
17+
}
18+
19+
export default class Html extends PureComponent {
20+
static propTypes = {
21+
scripts: PropTypes.array.isRequired,
22+
scriptElement: PropTypes.arrayOf(PropTypes.element).isRequired,
23+
styles: PropTypes.array.isRequired,
24+
contentMarkup: PropTypes.string.isRequired,
25+
store: PropTypes.object.isRequired,
26+
styleElement: PropTypes.arrayOf(PropTypes.element).isRequired,
27+
helmet: PropTypes.object.isRequired,
28+
}
29+
render() {
30+
const {
31+
contentMarkup,
32+
scripts,
33+
scriptElement,
34+
store,
35+
styleElement,
36+
styles,
37+
helmet,
38+
} = this.props
39+
40+
return (
41+
<html lang="zh-TW">
42+
<head>
43+
<script
44+
async
45+
src="https://www.googleoptimize.com/optimize.js?id=OPT-NGNDMW8"
46+
/>
47+
{helmet.base.toComponent()}
48+
{helmet.title.toComponent()}
49+
{helmet.priority.toComponent()}
50+
{helmet.meta.toComponent()}
51+
{helmet.link.toComponent()}
52+
{helmet.script.toComponent()}
53+
54+
<meta charSet="utf-8" />
55+
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
56+
<meta httpEquiv="content-type" content="text/html; charSet=utf-8" />
57+
<meta httpEquiv="Cache-control" content="public" />
58+
<meta
59+
name="viewport"
60+
content="viewport-fit=cover, width=device-width, user-scalable=no, minimum-scale=1, initial-scale=1"
61+
/>
62+
<meta name="apple-mobile-web-app-capable" content="yes" />
63+
<meta name="theme-color" content={colorGrayscale.gray100} />
64+
<link
65+
rel="alternate"
66+
type="application/rss+xml"
67+
title="RSS 2.0"
68+
href="https://www.twreporter.org/a/rss2.xml"
69+
/>
70+
<link rel="manifest" href="/meta/manifest.json" />
71+
{/* Add to home screen for Safari on iOS */}
72+
<meta name="apple-mobile-web-app-capable" content="yes" />
73+
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
74+
<meta
75+
name="apple-mobile-web-app-title"
76+
content="報導者 The Reporter"
77+
/>
78+
<link
79+
rel="apple-touch-icon"
80+
href="https://www.twreporter.org/images/apple-touch-icon-152x152.png"
81+
/>
82+
{/* Title icon for windows */}
83+
<meta
84+
name="msapplication-TileImage"
85+
content="https://www.twreporter.org/images/icon-normal.png"
86+
/>
87+
<meta name="msapplication-TileColor" content={colorBrand.main} />
88+
89+
<link href="/asset/favicon.png" rel="shortcut icon" />
90+
<link rel="stylesheet" href="/asset/normalize.css" />
91+
{_.map(webfonts.fontGCSFiles, (fileSrc, key) => (
92+
<link
93+
rel="preload"
94+
href={fileSrc}
95+
key={'webfont' + key}
96+
as="font"
97+
crossOrigin="anonymous"
98+
/>
99+
))}
100+
{_.map(styles, (stylesheet, key) => (
101+
<link
102+
href={stylesheet}
103+
key={'stylesheet' + key}
104+
media="all"
105+
rel="stylesheet"
106+
type="text/css"
107+
charSet="UTF-8"
108+
/>
109+
))}
110+
{styleElement}
111+
</head>
112+
<body>
113+
<div id="root" dangerouslySetInnerHTML={{ __html: contentMarkup }} />
114+
<script
115+
defer
116+
src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.zh-Hant-TW"
117+
/>
118+
<script
119+
dangerouslySetInnerHTML={{
120+
__html: `window.__REDUX_STATE__=${serialize(store.getState())};`,
121+
}}
122+
charSet="UTF-8"
123+
/>
124+
{_.map(scripts, (script, key) => (
125+
<script src={script} key={'scripts' + key} charSet="UTF-8" />
126+
))}
127+
{scriptElement}
128+
<script
129+
dangerouslySetInnerHTML={{
130+
__html: `(function(d) {
131+
var config = {
132+
kitId: 'vlk1qbe',
133+
scriptTimeout: 3000,
134+
async: true
135+
},
136+
h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='https://use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
137+
})(document);
138+
`,
139+
}}
140+
/>
141+
</body>
142+
</html>
143+
)
144+
}
145+
}

0 commit comments

Comments
 (0)