Skip to content

Commit ada95d7

Browse files
Merge pull request #107 from beijixiaohu/main
fix: improve CSS insertion logic to handle document loading state
2 parents a5b77c2 + 6b22d24 commit ada95d7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

vite.config.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,21 @@ export default defineConfig(({ command, mode }) => {
8080
code: `\
8181
function __insertCSSVueSonner(code) {
8282
if (!code || typeof document == 'undefined') return
83-
let head = document.head || document.getElementsByTagName('head')[0]
84-
let style = document.createElement('style')
85-
style.type = 'text/css'
86-
head.appendChild(style)
87-
;style.styleSheet ? (style.styleSheet.cssText = code) : style.appendChild(document.createTextNode(code))
83+
84+
function insertCSS() {
85+
let head = document.head || document.getElementsByTagName('head')[0]
86+
if (!head) return
87+
let style = document.createElement('style')
88+
style.type = 'text/css'
89+
head.appendChild(style)
90+
style.styleSheet ? (style.styleSheet.cssText = code) : style.appendChild(document.createTextNode(code))
91+
}
92+
93+
if (document.readyState === 'loading') {
94+
document.addEventListener('DOMContentLoaded', insertCSS)
95+
} else {
96+
insertCSS()
97+
}
8898
}\n
8999
__insertCSSVueSonner(${JSON.stringify(cssCodeStr)})
90100
\n ${code}`,

0 commit comments

Comments
 (0)