22import PropTypes from 'prop-types'
33import React , { PureComponent } from 'react'
44import serialize from 'serialize-javascript'
5+ import DOMPurify from 'dompurify'
56
67// @twreporter
78import webfonts from '@twreporter/react-components/lib/text/utils/webfonts'
@@ -26,6 +27,36 @@ export default class Html extends PureComponent {
2627 styleElement : PropTypes . arrayOf ( PropTypes . element ) . isRequired ,
2728 helmet : PropTypes . object . isRequired ,
2829 }
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+ }
2960 render ( ) {
3061 const {
3162 contentMarkup,
@@ -110,34 +141,21 @@ export default class Html extends PureComponent {
110141 { styleElement }
111142 </ head >
112143 < body >
113- < div id = "root" dangerouslySetInnerHTML = { { __html : contentMarkup } } />
144+ < div id = "root" dangerouslySetInnerHTML = { { __html : DOMPurify . sanitize ( contentMarkup ) } } />
114145 < script
115146 defer
116147 src = "https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.zh-Hant-TW"
117148 />
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 >
124154 { _ . map ( scripts , ( script , key ) => (
125155 < script src = { script } key = { 'scripts' + key } charSet = "UTF-8" />
126156 ) ) }
127157 { 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 >
141159 </ body >
142160 </ html >
143161 )
0 commit comments