Skip to content

Commit 049b0d0

Browse files
committed
fix
1 parent 8f45fc7 commit 049b0d0

File tree

1 file changed

+15
-10
lines changed
  • packages/svelte/src/internal/client/dom

1 file changed

+15
-10
lines changed
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
import { queue_micro_task } from './task';
2+
13
/**
24
* @param {Node} anchor
35
* @param {{ hash: string, code: string }} css
46
*/
57
export function append_styles(anchor, css) {
6-
var root = anchor.getRootNode();
8+
// Use `queue_micro_task` to ensure `anchor` is in the DOM, otherwise getRootNode() will yield wrong results
9+
queue_micro_task(() => {
10+
var root = anchor.getRootNode();
711

8-
var target = /** @type {ShadowRoot} */ (root).host
9-
? /** @type {ShadowRoot} */ (root)
10-
: /** @type {Document} */ (root).head ?? /** @type {Document} */ (root.ownerDocument).head;
12+
var target = /** @type {ShadowRoot} */ (root).host
13+
? /** @type {ShadowRoot} */ (root)
14+
: /** @type {Document} */ (root).head ?? /** @type {Document} */ (root.ownerDocument).head;
1115

12-
if (!target.querySelector('#' + css.hash)) {
13-
const style = document.createElement('style');
14-
style.id = css.hash;
15-
style.textContent = css.code;
16+
if (!target.querySelector('#' + css.hash)) {
17+
const style = document.createElement('style');
18+
style.id = css.hash;
19+
style.textContent = css.code;
1620

17-
target.appendChild(style);
18-
}
21+
target.appendChild(style);
22+
}
23+
});
1924
}

0 commit comments

Comments
 (0)