@@ -34,33 +34,37 @@ export function recoverCSS(url: string) {
34
34
export function applyCSS ( url : string , { css, href } : { css ?: string , href ?: string } ) {
35
35
if ( ! inDeno ) {
36
36
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 ) => {
38
38
return el . getAttribute ( 'data-module-id' ) === url && el . hasAttribute ( 'ssr' )
39
39
} )
40
- if ( ssr ) {
40
+ if ( ssrEl ) {
41
41
// apply the css at next time
42
- ssr . removeAttribute ( 'ssr' )
42
+ ssrEl . removeAttribute ( 'ssr' )
43
43
} else {
44
44
const prevEls = Array . from ( document . head . children ) . filter ( ( el : any ) => {
45
45
return el . getAttribute ( 'data-module-id' ) === url
46
46
} )
47
+ const clean = ( ) => {
48
+ if ( prevEls . length > 0 ) {
49
+ prevEls . forEach ( el => document . head . removeChild ( el ) )
50
+ }
51
+ }
47
52
let el : any
48
53
if ( util . isFilledString ( css ) ) {
49
54
el = document . createElement ( 'style' )
50
55
el . type = 'text/css'
51
56
el . appendChild ( document . createTextNode ( css ) )
57
+ Promise . resolve ( ) . then ( clean )
52
58
} else if ( util . isFilledString ( href ) ) {
53
59
el = document . createElement ( 'link' )
54
60
el . rel = 'stylesheet'
55
61
el . href = href
62
+ el . onload = clean
56
63
} else {
57
64
throw new Error ( 'applyCSS: missing css' )
58
65
}
59
66
el . setAttribute ( 'data-module-id' , url )
60
67
document . head . appendChild ( el )
61
- if ( prevEls . length > 0 ) {
62
- prevEls . forEach ( el => document . head . removeChild ( el ) )
63
- }
64
68
}
65
69
}
66
70
}
0 commit comments