Fix duplicate sx-cssx-live style tags
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 4m40s

Cache the style element reference in _cssx-style-el so flush-cssx-to-dom
never creates more than one. Previous code called dom-query on every
flush, which could miss the element during rapid successive calls,
creating duplicates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-14 17:16:13 +00:00
parent 41f4772ba7
commit 731484c093
2 changed files with 24 additions and 15 deletions

View File

@@ -427,19 +427,25 @@
;; injects any unflushed rules into a persistent <style> element in <head>.
;; Called after hydration (boot + post-swap) to cover all render paths.
(define _cssx-style-el nil)
(define flush-cssx-to-dom :effects [mutation io]
(fn ()
(let ((rules (collected "cssx")))
(when (not (empty? rules))
(let ((style (or (dom-query "#sx-cssx-live")
(let ((s (dom-create-element "style" nil)))
(dom-set-attr s "id" "sx-cssx-live")
(dom-set-attr s "data-cssx" "")
(dom-append-to-head s)
s))))
(dom-set-prop style "textContent"
(str (or (dom-get-prop style "textContent") "")
(join "" rules))))
;; Reuse cached element, or find/create exactly once
(when (nil? _cssx-style-el)
(let ((found (dom-query "#sx-cssx-live")))
(if found
(set! _cssx-style-el found)
(let ((s (dom-create-element "style" nil)))
(dom-set-attr s "id" "sx-cssx-live")
(dom-set-attr s "data-cssx" "")
(dom-append-to-head s)
(set! _cssx-style-el s)))))
(dom-set-prop _cssx-style-el "textContent"
(str (or (dom-get-prop _cssx-style-el "textContent") "")
(join "" rules)))
(clear-collected! "cssx")))))