Skip to content

Commit

Permalink
fix(@nguniversal/common): error during critical CSS inlining for exte…
Browse files Browse the repository at this point in the history
…rnal stylesheets

The problem with the previous approach is that it assumes that a `link` tag processed by Critters will be preceded by a `style` tag that was inserted by Critters. This assumption might not necessarily be true if Critters is unable to resolve the content of the linked styles.

Closes: angular#3241
  • Loading branch information
alan-agius4 committed Aug 17, 2023
1 parent 5974f8e commit 4b4c1b7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion modules/common/tools/src/inline-css-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,15 @@ class CrittersExtended extends Critters {
this.conditionallyInsertCspLoadingScript(document, cspNonce);
}

link.prev?.setAttribute('nonce', cspNonce);
// Ideally we would hook in at the time Critters inserts the `style` tags, but there isn't
// a way of doing that at the moment so we fall back to doing it any time a `link` tag is
// inserted. We mitigate it by only iterating the direct children of the `<head>` which
// should be pretty shallow.
document.head.children.forEach((child) => {
if (child.tagName === 'style' && !child.hasAttribute('nonce')) {
child.setAttribute('nonce', cspNonce);
}
});
}

return returnValue;
Expand Down

0 comments on commit 4b4c1b7

Please sign in to comment.