@@ -21,23 +21,33 @@ const CSP_MEDIA_ATTR = 'ngCspMedia';
21
21
22
22
/**
23
23
* Script text used to change the media value of the link tags.
24
+ *
25
+ * NOTE:
26
+ * We do not use `document.querySelectorAll('link').forEach((s) => s.addEventListener('load', ...)`
27
+ * because this does not always fire on Chome.
28
+ * See: https://github.com/angular/angular-cli/issues/26932 and https://crbug.com/1521256
24
29
*/
25
30
const LINK_LOAD_SCRIPT_CONTENT = [
26
- `(() => {` ,
27
- // Save the `children` in a variable since they're a live DOM node collection.
28
- // We iterate over the direct descendants, instead of going through a `querySelectorAll`,
29
- // because we know that the tags will be directly inside the `head`.
30
- ` const children = document.head.children;` ,
31
- // Declare `onLoad` outside the loop to avoid leaking memory.
32
- // Can't be an arrow function, because we need `this` to refer to the DOM node.
33
- ` function onLoad() {this.media = this.getAttribute('${ CSP_MEDIA_ATTR } ');}` ,
34
- // Has to use a plain for loop, because some browsers don't support
35
- // `forEach` on `children` which is a `HTMLCollection`.
36
- ` for (let i = 0; i < children.length; i++) {` ,
37
- ` const child = children[i];` ,
38
- ` child.hasAttribute('${ CSP_MEDIA_ATTR } ') && child.addEventListener('load', onLoad);` ,
39
- ` }` ,
40
- `})();` ,
31
+ '(() => {' ,
32
+ ` const CSP_MEDIA_ATTR = '${ CSP_MEDIA_ATTR } ';` ,
33
+ ' const documentElement = document.documentElement;' ,
34
+ ' const listener = (e) => {' ,
35
+ ' const target = e.target;' ,
36
+ ` if (target.tagName !== 'LINK' || !target.hasAttribute(CSP_MEDIA_ATTR)) {` ,
37
+ ' return;' ,
38
+ ' }' ,
39
+
40
+ ' target.media = target.getAttribute(CSP_MEDIA_ATTR);' ,
41
+ ' target.removeAttribute(CSP_MEDIA_ATTR);' ,
42
+
43
+ // Remove onload listener when there are no longer styles that need to be loaded.
44
+ ' if (!document.head.querySelector(`link[${CSP_MEDIA_ATTR}]`)) {' ,
45
+ ` documentElement.removeEventListener('load', listener);` ,
46
+ ' }' ,
47
+ ' };' ,
48
+
49
+ ` documentElement.addEventListener('load', listener, true);` ,
50
+ '})();' ,
41
51
] . join ( '\n' ) ;
42
52
43
53
export interface InlineCriticalCssProcessOptions {
@@ -62,6 +72,7 @@ interface PartialHTMLElement {
62
72
hasAttribute ( name : string ) : boolean ;
63
73
removeAttribute ( name : string ) : void ;
64
74
appendChild ( child : PartialHTMLElement ) : void ;
75
+ insertBefore ( newNode : PartialHTMLElement , referenceNode ?: PartialHTMLElement ) : void ;
65
76
remove ( ) : void ;
66
77
name : string ;
67
78
textContent : string ;
@@ -164,7 +175,7 @@ class CrittersExtended extends Critters {
164
175
// `addEventListener` to apply the media query instead.
165
176
link . removeAttribute ( 'onload' ) ;
166
177
link . setAttribute ( CSP_MEDIA_ATTR , crittersMedia [ 1 ] ) ;
167
- this . conditionallyInsertCspLoadingScript ( document , cspNonce ) ;
178
+ this . conditionallyInsertCspLoadingScript ( document , cspNonce , link ) ;
168
179
}
169
180
170
181
// Ideally we would hook in at the time Critters inserts the `style` tags, but there isn't
@@ -204,7 +215,11 @@ class CrittersExtended extends Critters {
204
215
* Inserts the `script` tag that swaps the critical CSS at runtime,
205
216
* if one hasn't been inserted into the document already.
206
217
*/
207
- private conditionallyInsertCspLoadingScript ( document : PartialDocument , nonce : string ) : void {
218
+ private conditionallyInsertCspLoadingScript (
219
+ document : PartialDocument ,
220
+ nonce : string ,
221
+ link : PartialHTMLElement ,
222
+ ) : void {
208
223
if ( this . addedCspScriptsDocuments . has ( document ) ) {
209
224
return ;
210
225
}
@@ -219,9 +234,9 @@ class CrittersExtended extends Critters {
219
234
const script = document . createElement ( 'script' ) ;
220
235
script . setAttribute ( 'nonce' , nonce ) ;
221
236
script . textContent = LINK_LOAD_SCRIPT_CONTENT ;
222
- // Append the script to the head since it needs to
223
- // run as early as possible, after the `link` tags.
224
- document . head . appendChild ( script ) ;
237
+ // Prepend the script to the head since it needs to
238
+ // run as early as possible, before the `link` tags.
239
+ document . head . insertBefore ( script , link ) ;
225
240
this . addedCspScriptsDocuments . add ( document ) ;
226
241
}
227
242
}
0 commit comments