Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit b04eecb

Browse files
committed
Improve style hmr
1 parent 58a9bda commit b04eecb

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

framework/core/style.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,37 @@ export function recoverCSS(url: string) {
3434
export function applyCSS(url: string, { css, href }: { css?: string, href?: string }) {
3535
if (!inDeno) {
3636
const { document } = window as any
37-
const ssr = Array.from<any>(document.head.children).find((el: any) => {
37+
const ssrEl = Array.from<any>(document.head.children).find((el: any) => {
3838
return el.getAttribute('data-module-id') === url && el.hasAttribute('ssr')
3939
})
40-
if (ssr) {
40+
if (ssrEl) {
4141
// apply the css at next time
42-
ssr.removeAttribute('ssr')
42+
ssrEl.removeAttribute('ssr')
4343
} else {
4444
const prevEls = Array.from(document.head.children).filter((el: any) => {
4545
return el.getAttribute('data-module-id') === url
4646
})
47+
const clean = () => {
48+
if (prevEls.length > 0) {
49+
prevEls.forEach(el => document.head.removeChild(el))
50+
}
51+
}
4752
let el: any
4853
if (util.isFilledString(css)) {
4954
el = document.createElement('style')
5055
el.type = 'text/css'
5156
el.appendChild(document.createTextNode(css))
57+
Promise.resolve().then(clean)
5258
} else if (util.isFilledString(href)) {
5359
el = document.createElement('link')
5460
el.rel = 'stylesheet'
5561
el.href = href
62+
el.onload = clean
5663
} else {
5764
throw new Error('applyCSS: missing css')
5865
}
5966
el.setAttribute('data-module-id', url)
6067
document.head.appendChild(el)
61-
if (prevEls.length > 0) {
62-
prevEls.forEach(el => document.head.removeChild(el))
63-
}
6468
}
6569
}
6670
}

0 commit comments

Comments
 (0)