| 553 | let lastInsertedStyle: HTMLStyleElement | undefined |
| 554 | |
| 555 | export function updateStyle(id: string, content: string): void { |
| 556 | if (linkSheetsMap.has(id)) return |
| 557 | |
| 558 | let style = sheetsMap.get(id) |
| 559 | if (!style) { |
| 560 | style = document.createElement('style') |
| 561 | style.setAttribute('type', 'text/css') |
| 562 | style.setAttribute('data-vite-dev-id', id) |
| 563 | style.textContent = content |
| 564 | if (cspNonce) { |
| 565 | style.setAttribute('nonce', cspNonce) |
| 566 | } |
| 567 | |
| 568 | if (!lastInsertedStyle) { |
| 569 | document.head.appendChild(style) |
| 570 | |
| 571 | // reset lastInsertedStyle after async |
| 572 | // because dynamically imported css will be split into a different file |
| 573 | setTimeout(() => { |
| 574 | lastInsertedStyle = undefined |
| 575 | }, 0) |
| 576 | } else { |
| 577 | lastInsertedStyle.insertAdjacentElement('afterend', style) |
| 578 | } |
| 579 | lastInsertedStyle = style |
| 580 | } else { |
| 581 | style.textContent = content |
| 582 | } |
| 583 | sheetsMap.set(id, style) |
| 584 | } |
| 585 | |
| 586 | export function removeStyle(id: string): void { |
| 587 | if (linkSheetsMap.has(id)) { |