Skip to content

Commit

Permalink
even more robust style injecting
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed May 24, 2023
1 parent 8a03db7 commit 0088523
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/core/src/styleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ export function ensureElHasStyles(el: HTMLElement): void {
registerStylesRoot(el.getRootNode() as ParentNode)
}

function registerStylesRoot(
rootNode: ParentNode,
parentEl: ParentNode = rootNode,
insertBefore: Node | null = parentEl.firstChild,
): void {
function registerStylesRoot(rootNode: ParentNode): void {
let styleEl: HTMLStyleElement = styleEls.get(rootNode)

if (!styleEl || !styleEl.isConnected) {
Expand All @@ -32,6 +28,11 @@ function registerStylesRoot(
styleEl.nonce = nonce
}

const parentEl = rootNode === document ? document.head : rootNode
const insertBefore = rootNode === document
? parentEl.querySelector('script,link[rel=stylesheet],link[as=style],style')
: parentEl.firstChild

parentEl.insertBefore(styleEl, insertBefore)
}

Expand Down Expand Up @@ -93,9 +94,5 @@ function queryNonceValue() {
// -------------------------------------------------------------------------------------------------

if (typeof document !== 'undefined') {
registerStylesRoot(
document,
document.head,
document.head.querySelector('script,link,style'), // if none, will put at end of <head>
)
registerStylesRoot(document)
}

0 comments on commit 0088523

Please sign in to comment.