2
2
import PropTypes from 'prop-types'
3
3
import React , { PureComponent } from 'react'
4
4
import serialize from 'serialize-javascript'
5
+ import DOMPurify from 'dompurify'
5
6
6
7
// @twreporter
7
8
import webfonts from '@twreporter/react-components/lib/text/utils/webfonts'
@@ -26,6 +27,36 @@ export default class Html extends PureComponent {
26
27
styleElement : PropTypes . arrayOf ( PropTypes . element ) . isRequired ,
27
28
helmet : PropTypes . object . isRequired ,
28
29
}
30
+
31
+ // Add script loading function that will be called from script tag
32
+ static loadTypekit ( ) {
33
+ const config = {
34
+ kitId : 'vlk1qbe' ,
35
+ scriptTimeout : 3000 ,
36
+ async : true
37
+ } ;
38
+ const d = document ;
39
+ const h = d . documentElement ;
40
+ const t = setTimeout ( ( ) => {
41
+ h . className = h . className . replace ( / \b w f - l o a d i n g \b / g, "" ) + " wf-inactive" ;
42
+ } , config . scriptTimeout ) ;
43
+ const tk = d . createElement ( "script" ) ;
44
+ let f = false ;
45
+ const s = d . getElementsByTagName ( "script" ) [ 0 ] ;
46
+ h . className += " wf-loading" ;
47
+ tk . src = 'https://use.typekit.net/' + config . kitId + '.js' ;
48
+ tk . async = true ;
49
+ tk . onload = tk . onreadystatechange = function ( ) {
50
+ const a = this . readyState ;
51
+ if ( f || ( a && a !== "complete" && a !== "loaded" ) ) return ;
52
+ f = true ;
53
+ clearTimeout ( t ) ;
54
+ try {
55
+ Typekit . load ( config ) ;
56
+ } catch ( e ) { }
57
+ } ;
58
+ s . parentNode . insertBefore ( tk , s ) ;
59
+ }
29
60
render ( ) {
30
61
const {
31
62
contentMarkup,
@@ -110,34 +141,21 @@ export default class Html extends PureComponent {
110
141
{ styleElement }
111
142
</ head >
112
143
< body >
113
- < div id = "root" dangerouslySetInnerHTML = { { __html : contentMarkup } } />
144
+ < div id = "root" dangerouslySetInnerHTML = { { __html : DOMPurify . sanitize ( contentMarkup ) } } />
114
145
< script
115
146
defer
116
147
src = "https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.zh-Hant-TW"
117
148
/>
118
- < script
119
- dangerouslySetInnerHTML = { {
120
- __html : `window.__REDUX_STATE__=${ serialize ( store . getState ( ) ) } ;` ,
121
- } }
122
- charSet = "UTF-8"
123
- />
149
+ < script
150
+ id = "initial-state"
151
+ type = "application/json"
152
+ > { serialize ( store . getState ( ) , { isJSON : true } ) } </ script >
153
+ < script src = "/static/init-state.js" > </ script >
124
154
{ _ . map ( scripts , ( script , key ) => (
125
155
< script src = { script } key = { 'scripts' + key } charSet = "UTF-8" />
126
156
) ) }
127
157
{ 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
- />
158
+ < script defer > { `(${ Html . loadTypekit . toString ( ) } )();` } </ script >
141
159
</ body >
142
160
</ html >
143
161
)
0 commit comments