@@ -89,7 +89,7 @@ async function processHTML(
8989}
9090
9191class FontOptimizerMiddleware implements PostProcessMiddleware {
92- fontDefinitions : Array < string > = [ ]
92+ fontDefinitions : ( string | undefined ) [ ] [ ] = [ ]
9393 inspect (
9494 originalDom : HTMLElement ,
9595 _data : postProcessData ,
@@ -112,8 +112,10 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
112112 )
113113 . forEach ( ( element : HTMLElement ) => {
114114 const url = element . getAttribute ( 'data-href' )
115+ const nonce = element . getAttribute ( 'nonce' )
116+
115117 if ( url ) {
116- this . fontDefinitions . push ( url )
118+ this . fontDefinitions . push ( [ url , nonce ] )
117119 }
118120 } )
119121 }
@@ -127,7 +129,7 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
127129 return markup
128130 }
129131 for ( const key in this . fontDefinitions ) {
130- const url = this . fontDefinitions [ key ]
132+ const [ url , nonce ] = this . fontDefinitions [ key ]
131133 const fallBackLinkTag = `<link rel="stylesheet" href="${ url } "/>`
132134 if (
133135 result . indexOf ( `<style data-href="${ url } ">` ) > - 1 ||
@@ -136,16 +138,17 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
136138 // The font is already optimized and probably the response is cached
137139 continue
138140 }
139- const fontContent = options . getFontDefinition ( url )
141+ const fontContent = options . getFontDefinition ( url as string )
140142 if ( ! fontContent ) {
141143 /**
142144 * In case of unreachable font definitions, fallback to default link tag.
143145 */
144146 result = result . replace ( '</head>' , `${ fallBackLinkTag } </head>` )
145147 } else {
148+ const nonceStr = nonce ? ` nonce="${ nonce } "` : ''
146149 result = result . replace (
147150 '</head>' ,
148- `<style data-href="${ url } ">${ fontContent } </style></head>`
151+ `<style data-href="${ url } "${ nonceStr } >${ fontContent } </style></head>`
149152 )
150153 }
151154 }
0 commit comments