Skip to content

Commit 8f45fc7

Browse files
committed
fix
1 parent bcbdac7 commit 8f45fc7

File tree

1 file changed

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

1 file changed

+10
-40
lines changed
Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,19 @@
1-
import { DEV } from 'esm-env';
2-
import { effect } from '../reactivity/effects.js';
3-
4-
var css_counter = new Map();
5-
61
/**
72
* @param {Node} anchor
83
* @param {{ hash: string, code: string }} css
94
*/
105
export function append_styles(anchor, css) {
11-
const maybe_append_styles = () => {
12-
var root = anchor.getRootNode();
13-
14-
var target = /** @type {ShadowRoot} */ (root).host
15-
? /** @type {ShadowRoot} */ (root)
16-
: /** @type {Document} */ (root).head ?? /** @type {Document} */ (root.ownerDocument).head;
17-
18-
if (!target.querySelector('#' + css.hash)) {
19-
const style = document.createElement('style');
20-
style.id = css.hash;
21-
style.textContent = css.code;
22-
23-
target.appendChild(style);
24-
}
25-
};
26-
27-
// Use an effect to ensure `anchor` is in the DOM, otherwise getRootNode() will yield wrong results
28-
effect(() => {
29-
// In dev, always check the DOM, so that styles can be replaced with HMR
30-
if (DEV) {
31-
maybe_append_styles();
32-
return;
33-
}
34-
// Otherwise, for prod we can use the css object as a key and count the usage to skip the lookup
35-
var count = css_counter.get(css) ?? 0;
36-
37-
css_counter.set(css, count + 1);
6+
var root = anchor.getRootNode();
387

39-
if (count > 0) return;
8+
var target = /** @type {ShadowRoot} */ (root).host
9+
? /** @type {ShadowRoot} */ (root)
10+
: /** @type {Document} */ (root).head ?? /** @type {Document} */ (root.ownerDocument).head;
4011

41-
maybe_append_styles();
12+
if (!target.querySelector('#' + css.hash)) {
13+
const style = document.createElement('style');
14+
style.id = css.hash;
15+
style.textContent = css.code;
4216

43-
return () => {
44-
var count = css_counter.get(css) - 1;
45-
css_counter.set(css, count);
46-
if (count === 0) css_counter.delete(css);
47-
};
48-
});
17+
target.appendChild(style);
18+
}
4919
}

0 commit comments

Comments
 (0)